Dependent fields in use with type:"datatable"

Dependent fields in use with type:"datatable"

mp2000mp2000 Posts: 23Questions: 1Answers: 0

Hello,

i want to use the function "dependent()" on a field with the option "type: datatable".
Contacts should be updated after selecting a company.
if i select a company the contacts that belongs to that company will be loaded. This works.

Problem 1: But the the previously selected contacts will not be selected again

{
            label: "Kontakt:",
            name: "CMDB_Ci_Contact_View[].cmdb_asset_id",
            type: "datatable",
            multiple: true,
            config: {},
       
        }, 
 initComplete: function(settings, json) {
            //Dependent
            editor.dependent('CMDB_Asset.xrm_company_id', 'ajax/cmdb/assets/computer/dependent_updateContactByCompany.php');


as a workaround i tested it without the function "dependent()" and updated the contacts with a search. This works.
But the results will not be ordered by the previously selected contacts..
Problem 2: Maybe someone has an idea how to show the previously selected contacts at the top of the table.

editor.field('CMDB_Asset.xrm_company_id').input().on('change', function() {
        var cur_value = $('option:selected', this).text();
        var table2 = $('#DataTables_Table_0').DataTable();
        table2.search(cur_value).draw()
    });

editor.on('open', function() {
         var cur_value = editor.field('CMDB_Asset.xrm_company_id').input();
         var cur_value = $('option:selected', cur_value).text();
         var table2 = $('#DataTables_Table_0').DataTable();
        table2.search(cur_value).draw()
    });

Replies

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    But the the previously selected contacts will not be selected again

    That typically would happen if the value being selected isn't in the table (usually a case of the label and value being mixed). Can you give me a link to your page showing the issue?

    Maybe someone has an idea how to show the previously selected contacts at the top of the table.

    There isn't an option to have the table sorted based on row selection at this time I'm afraid. It might be possible using a plug-in, but it might prove tricky due to the row caching and the fact that sorting is based on column data. A hidden column that returns the row's selected state would need to be used. I suspect that would get quite confusing when selecting and unselecting rows though.

    Allan

  • mp2000mp2000 Posts: 23Questions: 1Answers: 0

    Hello Allan,

    the application is on my local intranet.

    but i have some information, i hope this will help

    table.php

    ->join(
                Mjoin::inst('CMDB_Ci_Contact_View') //many-to-many
                    ->link('CMDB_Asset.id', 'CMDB_Asset_Computer_to_CMDB_Ci_Contact.cmdb_asset_computer_cmdb_asset_id')
                    ->link('CMDB_Ci_Contact_View.cmdb_asset_id', 'CMDB_Asset_Computer_to_CMDB_Ci_Contact.cmdb_ci_contact_cmdb_asset_id')
                    ->fields(
                        Field::inst('cmdb_asset_id') //value
                            ->validator('Validate::required')
                            ->options(Options::inst()
                                    ->table('CMDB_Ci_Contact_View')
                                    ->value('cmdb_asset_id')
                                    ->label('NameDropdown')
                                    ->order('Fullname asc')
                            ),
                        Field::inst('NameDropdown') //label
                    )
            )
    
    {
                label: "Kontakt:",
                name: "CMDB_Ci_Contact_View[].cmdb_asset_id",
                /*
                opts: {
                    multiple: true,
                    placeholder: 'auswählen'
                },
                type: "select",
                */
                type: "datatable",
                multiple: true,
                config: {},
                //multiEditable: false
            },
    
    initComplete: function(settings, json) {
        //Dependent
        editor.dependent('CMDB_Asset.xrm_company_id', 'ajax/cmdb/assets/computer/dependent_updateContactByCompany.php');
        editor.on('open', function(e, type) {})
    }
    
    

    dependent.php

    $contacts = $dbIntranet
            ->select('CMDB_Ci_Contact_View', ['cmdb_asset_id as value', 'NameDropdown as label'], ['xrm_company_id' => $_REQUEST['values']['CMDB_Asset.xrm_company_id']], 'Fullname')
            ->fetchAll();
    
        echo json_encode([
            'options' => [
                'CMDB_Ci_Contact_View[].cmdb_asset_id' => $contacts,
            ],
        ]);
    
  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Can you show me the CMDB_Asset.xrm_company_id configuration please? Could you also use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is.

    Thanks,
    Allan

  • mp2000mp2000 Posts: 23Questions: 1Answers: 0
    edited May 6

    Sorry for my late response,

    unfortunately i can not upload the sensitive data

     $data = Editor::inst($dbIntranet, 'CMDB_Asset')
            ->debug(false)
            ->fields(
    
    
    Field::inst('CMDB_Asset.xrm_company_id')
                    ->options(Options::inst()
                            ->table('XRM_Company_View')
                            ->value('id')
                            ->label('NameShort')
                            ->order('Name asc')
                    )
                    ->validator('Validate::dbValues')
                    ->validator('Validate::notEmpty'),
    
    ->leftJoin('XRM_Company_View', 'XRM_Company_View.id', '=', 'CMDB_Asset.xrm_company_id')
    

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Can you show me a row of the data that is being loaded for the DataTable please (the data array in the response JSON)?

    Thanks,
    Allan

Sign In or Register to comment.