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

后端 未结 3 702
臣服心动
臣服心动 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 04:04

    As far as I know there's no method for adding multiple options through the API. You'll need to write a loop that uses the addOption() method. You'll need to get the control instance of selectize before trying to use the API. Take a look at the example below, from the Github examples:

    // Create the selectize instance as usual 
    var $select = $('#select-tools').selectize({
        maxItems: null,
        valueField: 'id',
        labelField: 'title',
        searchField: 'title',
        options: [
            {id: 1, title: 'Spectrometer', url: 'http://en.wikipedia.org/wiki/Spectrometers'},
            {id: 2, title: 'Star Chart', url: 'http://en.wikipedia.org/wiki/Star_chart'},
            {id: 3, title: 'Electrical Tape', url: 'http://en.wikipedia.org/wiki/Electrical_tape'}
        ],
        create: false
    });
    
    // Get the selectize control instance
    var control = $select[0].selectize;
    
    // Add the new option when a button is clicked
    // Remove the click event and put the addOption call in a loop
    $('#button-addoption').on('click', function() {
            control.addOption({
            id: 4,
            title: 'Something New',
            url: 'http://google.com'
        });
    });
    

    From the Github examples.

提交回复
热议问题