How to preselect Options in a Select

How to preselect Options in a Select

arichardsonarichardson Posts: 16Questions: 3Answers: 0

Hey Alan.

I have a Datatable setup and Editor, and everything is working splendidly EXCEPT for one thing.

I have one field defined as a SELECT with MULTIPLE, but normally (meaning when DT is browsing the records) the column displays a COLUMN DELIMITED LIST.

When I click on the cell, it switches into EDIT mode, and displays the MULTIPLE SELECT populated with a list of users.

However, I cannot for the life of me figure out how to preselect the same users in the select that are in the list that makeup the value of the cell.

HELP!

EDITOR definition:

editor = new $.fn.dataTable.Editor({
            ajax: "drlAndTitle_test.cfc?method=dtAction",
            table: "#drltitletable",
            idSrc: "ELINID",
            fields: [ {
                label: "REVIEWERS:",
                name: "REVIEWERS",
                type: "select",
                multiple: true
            }
        });

The following code is what I use to preload the SELECT:

    $.ajax({
        type: "GET",
        datatype: 'json',
        url: "drlAndTitle_test.cfc?method=getMemberListByRole&ReturnFormat=json",
        data: {PROJECTSPECIFIC: ps,t:2},
        success: function(newOptions){
            var t = JSON.parse(newOptions);
            editor.field( 'REVIEWERS' ).update( t.DATA );   
        },
        fail: function(response){
            alert(response);
        }
    });

How do I convert the CELL value (of a comma delimited list) into PRESELECTED values when the SELECT is displayed for EDIT MODE?

Example of the CELL value:

ABSMITH, ACBEQARI, ACDANIEL, ACLANDSBURG

Perhaps I am just being think headed... and there is a way to have Editor just select the values that are present in the list... I just don't see it.

Thanks for your help!

Replies

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Are the values for the options in the select list different from the labels? e.g. do you have something like:

    { label: 'ABSMITH', value: 1 }
    

    as the options in the select list? If so, the key here is that you need to tell Editor to use the values (e.g. 1 from the above option) while DataTables needs to be told to use the labels for the display.

    Could you show me the JSON data that is used to load the table and also the DataTables and Editor configurations that you are using please?

    Thanks,
    Allan

  • arichardsonarichardson Posts: 16Questions: 3Answers: 0
    edited December 2015

    Here is the AJAX response . It's just the LABEL rather than a LABEL and its VALUE.

    {
        "COLUMNS": ["MEMBNAME"],
        "DATA": [
            ["ACHUFFMAN"],
            ["AMLJONE"],
            ["ASSARSOUR"],
            ["BARODRIGUEZ"],
            ["BCCUSHWAY"],
            ["BESTONE"],
            ["BJSMASZCZ"],
            ["CDMITCHELL"],
            ["CEBLACKBURN"],
            ["CEHAWKINS"],
            ["CMFREY"],
            ["CTGAULT"],
            ["CWKENYON"],
            ["DASHOPE"],
            ["DGHARRIS"],
            ["DHSEAMAN"],
            ["DJHACKEN"],
            ["DLHOELSCHER"],
            ["DLMURRAY"],
            ["DRSHAFER"],
            ["DSKLUIS"],
            ["DTSAPONE"],
            ["DWSHAVER"],
            ["ECDILLMAN"],
            ["ECGANTT"],
            ["EJDANKEWICZ"],
            ["ELMANSFIELD"],
            ["ESSTRASEL"],
            ["FJAFRASIABI"],
            ["GABROBJORG"],
            ["GAPAWLOWSKI"],
            ["GHOAKLEY"],
            ["GMLEE"],
            ["GUMIRO"],
            ["JACHRIST"],
            ["JAKNIGHTS"],
            ["JATAUBEL"],
            ["JCMITCHELL"],
            ["JDSTRICKLAND"],
            ["JLSCHAFFER"],
            ["JMGRASSMAN"],
            ["JMROCHE"],
            ["JMTHOMAS"],
            ["JPGLAESER"],
            ["JRSCHARRER"],
            ["LEADREVIEWERB"],
            ["LEADREVIEWRA"],
            ["LENELSON"],
            ["LILINARES"],
            ["LLROBINSON"],
            ["LLTOTH"],
            ["LRWILLIAMS"],
            ["LWHICKS"],
            ["MAZITAGLIO"],
            ["MCWALLACE"],
            ["MERAMIREZ"],
            ["MFSOUZA"],
            ["MJBIEBERICH"],
            ["MLTARDY"],
            ["MPLEESE"],
            ["MRTEE"],
            ["MSCLARKE"],
            ["PAQUILL"],
            ["PEANSOFF"],
            ["PHCRONEY"],
            ["PKGAUTHIER"],
            ["PLGILLIAM"],
            ["PLMCCORMICK"],
            ["RBDELIZO"],
            ["RCSCHAUBEL"],
            ["RWMIDDLETON"],
            ["SAEARLE"],
            ["SAMOLLOYFLORES"],
            ["SAREDDIX"],
            ["SCJOHNSON"],
            ["SDSCHROEDER"],
            ["SHCLARK"],
            ["SHPENN"],
            ["SJHICKS"],
            ["SKMEHTA"],
            ["SLKANTZ"],
            ["SMHEFLIN"],
            ["SPMCELHINNEY"],
            ["TJSMOOT"],
            ["TRMCCAMMON"],
            ["TRWILLIAMS"],
            ["WCDECKER"],
            ["WHAUSTIN"],
            ["WPMACK"],
            ["WSDAVISON"],
            ["WTPACKARD"]
        ]
    }
    
  • arichardsonarichardson Posts: 16Questions: 3Answers: 0

    And here is all of the configuration for setting up the DT and EDITOR:

    var thecolumns = [];
            ps = $('#PROJECTSPECIFIC').val();
            thecolumns = [{
                "data": "ELINID",
                "visible": true
            }, {
                "data": "ELINNUMBER",
                "visible": true,
                "title": "DRL NO"
            }, {
                "data": "ELINTITLE",
                "sortable": true,
                "title": "DRL TITLE"
            }, {
                "data": "LEADREVIEWER",
                "sortable": false,
                "title": "LEAD REVIEWER"
            }, {
                "data": "ALTLEADREVIEWER",
                "sortable": false,
                "title": "ALT LEAD REVIEWER"
            }, {
                "data": "REVIEWERS",
                "sortable": false,
                "title": "REVIEWERS",           
            }, {
                "data": "ECATEGORY",
                "sortable": false,
                "title": "SWBS Element/Group"
            }];
            
            editor = new $.fn.dataTable.Editor({
                ajax: "drlAndTitle_test.cfc?method=dtAction",
                table: "#drltitletable",
                idSrc: "ELINID",
                fields: [{
                    label: "DRL TITLE:",
                    name: "ELINTITLE"
                }, {
                    label: "LEAD REVIEWER",
                    name: "LEADREVIEWER",
                    placeholder: 'Choose a LEAD REVIEWER:',
                    type: "select"
                }, {
                    label: "ALT LEAD REVIEWER",
                    name: "ALTLEADREVIEWER",
                    placeholder: 'Choose an ALT LEAD REVIEWER:',
                    type: "select"
                }, {
                    label: "REVIEWERS:",
                    name: "REVIEWERS",
                    type: "select",
                    multiple: true
                }, {
                    label: "CATEGORY:",
                    name: "ECATEGORY",
                    placeholder: 'Choose a CATEGORY:',
                    type: "select"
                }, {
                    label: "ELIN NO:",
                    name: "ELINNUMBER"
                }]
            });
            
            var table = $('#drltitletable').DataTable({
                serverSide: true, // Enable server side processing
                order: [1, "asc"], // default sort by column
                destroy: true,
                ajax: {
                    "url": "drlAndTitle_test.cfc?method=getData&ReturnFormat=json",
                    "type": "POST",
                    "data": {
                        PROJECTSPECIFIC: ps
                    },
                },
                columns: thecolumns,
                //paging: true,
                search: true,
                processing: true,
                autoFill: {
                    columns: ':not(:first-child)',
                    editor: editor
                },      
                select: {
                    style: 'os',
                    selector: 'td:first-child',
                    blurable: true
                }
            });
        // Inline editing submit button
        $('#drltitletable').on( 'click', 'tbody td:not(:first-child)', function (e) {
            editor.inline( this, {
                buttons: { label: '>', fn: function () { 
                    //alert('clickin it real yo');
                    this.submit(); 
                } }
            } );
        } );
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Thanks for the information. It looks like the issue might be related to the fact that each item in the options array for the multiple select is an array with a single value - for example would it be possible to change ["ACHUFFMAN"] to simply be "ACHUFFMAN" - i.e. have an array of strings rather than an array of array of strings?

    What does the REVIEWERS property in the JSON data that the DataTable sees look like?

    Allan

  • arichardsonarichardson Posts: 16Questions: 3Answers: 0
    edited December 2015

    Well, let me play with it. I am using ColdFusion server side, and that is the way it converts a ColdFusion STRUCT object into JSON.

  • arichardsonarichardson Posts: 16Questions: 3Answers: 0

    This is what the data that is used to populate the DT looks like:

    {"recordsFiltered":844,"recordsTotal":844,"draw":1,"data":[{"ECATEGORYNO":1,"ELINNUMBER":"A001","ELINTITLE":"TESTING1W","REVIEWERS":"ABSMITH, ACBEQARI, ACDANIEL, ACLANDSBURG","ELINID":1041,"ECATEGORY":"010 - COMBAT CAPABILITIES (OFFENSIVE AND DEFENSIVE)                ","ESWBSCATID":2,"LEADREVIEWER":"ACHUFFMAN","ALTLEADREVIEWER":"BCCUSHWAY"},{"ECATEGORYNO":1,"ELINNUMBER":"A001-02","ELINTITLE":"TESTING MORE","REVIEWERS":"","ELINID":1042,"ECATEGORY":"014 - SURFACE WEAPONS VS AIR TARGETS                               ","ESWBSCATID":6,"LEADREVIEWER":"LEADREVIEWERB","ALTLEADREVIEWER":"FJAFRASIABI"},{"ECATEGORYNO":1,"ELINNUMBER":"A1001-1","ELINTITLE":"RB TES1","REVIEWERS":"ABJACKSON, ABKEENE, LWMCLAUGHLIN","ELINID":1043,"ECATEGORY":"024 - MINE AND MINE COUNTERMEASURES WARFARE                        ","ESWBSCATID":14,"LEADREVIEWER":"AMLJONE","ALTLEADREVIEWER":"AMLJONE"},{"ECATEGORYNO":" 0","ELINNUMBER":"A1001-2","ELINTITLE":"RB TEST","REVIEWERS":"","ELINID":1045,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"ACHUFFMAN","ALTLEADREVIEWER":"ACHUFFMAN"},{"ECATEGORYNO":" 0","ELINNUMBER":"A1001-3","ELINTITLE":"RB TEST","REVIEWERS":"","ELINID":1046,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"ACHUFFMAN","ALTLEADREVIEWER":"ACHUFFMAN"},{"ECATEGORYNO":" 0","ELINNUMBER":"A1001-4","ELINTITLE":"RB TE","REVIEWERS":"ABBRANTLEY, ABJACKSON","ELINID":1044,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"CEBLACKBURN","ALTLEADREVIEWER":"ACHUFFMAN"},{"ECATEGORYNO":" 0","ELINNUMBER":"B001","ELINTITLE":"MANAGEMENT PLANS SAVE TESTING","REVIEWERS":"","ELINID":1,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"AAMCDONALD","ALTLEADREVIEWER":"AAMCDONALD"},{"ECATEGORYNO":"","ELINNUMBER":"B002","ELINTITLE":"SCHEDULES POST POST EDIT","REVIEWERS":"","ELINID":2,"ECATEGORY":"","ESWBSCATID":" 0","LEADREVIEWER":"SHCLARK","ALTLEADREVIEWER":"CEHAWKINS"},{"ECATEGORYNO":" 0","ELINNUMBER":"B002-01","ELINTITLE":"MASTER SCHEDULE","REVIEWERS":"","ELINID":3,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"SHCLARK","ALTLEADREVIEWER":"ESSTRASEL"},{"ECATEGORYNO":" 0","ELINNUMBER":"B002-02","ELINTITLE":"DETAILED DESIGN SCHEDULE","REVIEWERS":"","ELINID":4,"ECATEGORY":"000 - GENERAL GUIDANCE AND ADMINISTRATION","ESWBSCATID":1,"LEADREVIEWER":"SHCLARK","ALTLEADREVIEWER":"CEHAWKINS"}],"projectid":3}
    
  • arichardsonarichardson Posts: 16Questions: 3Answers: 0

    OK, I have converted my code to create the following:

    ["ACHUFFMAN","AMLJONE","ASSARSOUR","BARODRIGUEZ","BCCUSHWAY","BESTONE","BJSMASZCZ","CDMITCHELL","CEBLACKBURN","CEHAWKINS","CMFREY","CTGAULT","CWKENYON","DASHOPE","DGHARRIS","DHSEAMAN","DJHACKEN","DLHOELSCHER","DLMURRAY","DRSHAFER","DSKLUIS","DTSAPONE","DWSHAVER","ECDILLMAN","ECGANTT","EJDANKEWICZ","ELMANSFIELD","ESSTRASEL","FJAFRASIABI","GABROBJORG","GAPAWLOWSKI","GHOAKLEY","GMLEE","GUMIRO","JACHRIST","JAKNIGHTS","JATAUBEL","JCMITCHELL","JDSTRICKLAND","JLSCHAFFER","JMGRASSMAN","JMROCHE","JMTHOMAS","JPGLAESER","JRSCHARRER","LEADREVIEWERB","LEADREVIEWRA","LENELSON","LILINARES","LLROBINSON","LLTOTH","LRWILLIAMS","LWHICKS","MAZITAGLIO","MCWALLACE","MERAMIREZ","MFSOUZA","MJBIEBERICH","MLTARDY","MPLEESE","MRTEE","MSCLARKE","PAQUILL","PEANSOFF","PHCRONEY","PKGAUTHIER","PLGILLIAM","PLMCCORMICK","RBDELIZO","RCSCHAUBEL","RWMIDDLETON","SAEARLE","SAMOLLOYFLORES","SAREDDIX","SCJOHNSON","SDSCHROEDER","SHCLARK","SHPENN","SJHICKS","SKMEHTA","SLKANTZ","SMHEFLIN","SPMCELHINNEY","TJSMOOT","TRMCCAMMON","TRWILLIAMS","WCDECKER","WHAUSTIN","WPMACK","WSDAVISON","WTPACKARD"]
    

    However, DTs doesn't seem to like that format either, since now it's not even populating my dropdowns at all. I suppose I could hard code the return format a specific way. What does DT look for in the return?

  • arichardsonarichardson Posts: 16Questions: 3Answers: 0
    edited December 2015

    Well, I did tweak my JS a bit, and now its populating the dropdowns as it was before, using the AJAX format in the comment above.

    However, it's still not preselecting any of the options.

    Here is the updated function used populate the REVIEWERS dropdown:

    $.ajax({
        type: "GET",
        datatype: 'json',
        url: "drlAndTitle_test.cfc?method=getMemberListByRole&ReturnFormat=json",
        data: {PROJECTSPECIFIC: ps,t:2},
        success: function(newOptions){
            var t = JSON.parse(newOptions);
            editor.field( 'REVIEWERS' ).update( t );  
        },
        fail: function(response){
            alert(response);
        }
    });
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    "REVIEWERS":"ABSMITH, ACBEQARI, ACDANIEL, ACLANDSBURG"

    These are comma+space delimited - we need to tell Editor this fact so it can split the string as required. At the moment it is trying to find a value that matches that full string!

    You can do that with the separator option of the select field type. In this case you have a comma+space so it would be:

    separator: ', '
    

    Regards,
    Allan

  • arichardsonarichardson Posts: 16Questions: 3Answers: 0

    Wow! That did it!

    Thanks so much! You saved me a lot of time trying to figure that out.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Good to hear that worked! Thanks for letting me know.

    Regards,
    Allan

This discussion has been closed.