How to dynamically set the 'multiple' parameter in a Select2 box?

How to dynamically set the 'multiple' parameter in a Select2 box?

ttsullivanttsullivan Posts: 10Questions: 5Answers: 1

Allan, my application includes a chain of select2 boxes (5 are dependent). For the final box in the chain, depending upon the option selected, I need to show either a single value select box or a multi-value select box. In order to accommodate either situation Is there a way to dynamically change the field's opts: "multiple" parameter to either true or false?

Answers

  • rf1234rf1234 Posts: 2,807Questions: 85Answers: 406
    edited July 2020

    I am not Allan but I am pretty sure there isn't. I couldn't even change the options for a selectize field after dt initialization. (It works with select fields though.)

    But there is always a work around:
    - Define the field as a "regular" field in your Editor instance so that it is a known field after initialization (doesn't have to be declared as a select2 field at that time). If you don't define it initially you may run into all sorts of problems if your are using the field in other data tables events. So it doesn't hurt to define the field as a dummy initially.
    - on "open" (or whatever event is suitable) clear the field and add it again the way you need it as single edit or multi edit.

    https://editor.datatables.net/reference/api/clear()

    https://editor.datatables.net/reference/api/add()

    Here is an example. I need to read the options individually for each row selected. (Editor uses the same options for all records of the data table which wasn't suitable in my case.) With selectize you can't update the options dynamically after initialization. Hence the work around:

    $.ajax({
        type: "POST",
        url: 'actions.php?action=getCtrLabelOptions',
        data: {
    //create: we submit zero to avoid departments, categories etc. being kept from selected records
            ctr_id: 0
        },
        dataType: "json",
        success: function (data) {   
            ctrLabelOptions = data;
            ctrEditor.clear("ctr_label[].id"); 
            ctrEditor.add( {
                label: lang === 'de' ? 'Labels:' : 'Labels:',
                name:  "ctr_label[].id", 
                type: "selectize", 
                options: ctrLabelOptions,
                opts: {
                    create: true,
                    createFilter: function(val) {
                      return ( isNaN(val) || val.indexOf('.') > -1 || val.indexOf('-') > -1 ) ? val : false;
                    },
                    maxItems: null,
                    openOnFocus: true,
                    allowEmptyOption: false,
                    placeholder: lang === 'de' ? 
                        "Bitte Labels wählen oder hinzufügen" : 
                        "Please select labels or add some",                
                    render: {
                        option_create: function(data, escape) {
                            var add = lang === 'de' ? "Neu: " : "Add ";      
                            return '<div class="create">' + add + '<strong>'
                                   + escape(data.input) + '</strong>&hellip;</div>';
                        }
                      }
                    }
            }, "ctr_govdept[].id" );
            $('div.DTE_Body div.DTE_Body_Content div.DTE_Field').addClass("two-cols");
        }
    });
    

    And this is the definition of the field in the Editor instance (yes, label is redundant here but I kept it for better legibility):

    }, {
        label: lang === 'de' ? 'Labels:' : 'Labels:',
        name:  "ctr_label[].id"   //removed and added dynamically
    
This discussion has been closed.