How do I set the selectize.js options list programmatically?

后端 未结 3 697
臣服心动
臣服心动 2021-02-01 03:46

I know how to set the optionList on Initiliaztion but how do I set it programmatically?

I have an inviteList array:

$(\"#s         


        
3条回答
  •  無奈伤痛
    2021-02-01 03:59

    You can use load method to set options via programmatic API. You may also want to call clear and clearOptions methods to reset selected values and old options.

    Here is how to put it all together:

    var selectize = $("#select-invite")[0].selectize;
    selectize.clear();
    selectize.clearOptions();
    selectize.load(function(callback) {
        callback(inviteList);
    });
    

    Please note that inviteList should be an array of objects that have properties configured in valueField and labelField options when select was initialized. For example, here is how inviteList should look like with default valueField and labelField options:

    var inviteList = [
        {
            text: 'Option One',
            value: 1
        },
        {
            text: 'Option Two',
            value: 2
        }
    ];
    

提交回复
热议问题