Bootstrap Multiselect update option list on flow

前端 未结 4 533
旧时难觅i
旧时难觅i 2020-12-29 12:35

I use bootstrap multi-select and I want to update options on flow with ajax

To populate on init my multiselect I do


                        
    
提交评论

  • 2020-12-29 12:57

    As an alternative to multiselect('dataprovider', data) you can build the list with jquery append exactly the way you did in your question. The only change you need to make is to delay the rebuild until after the ajax request is complete.

    var buildDrivers = $.getJSON('resources/orders/drivers.json', function(data) { 
       $.each(data, function(i, driver) {
          $('#toolbar select[name="drivers"]').append('<option>'+driver+'</option>');
       });
    });
    buildDrivers.complete(function() {
        $('.multiselect').multiselect('rebuild');
    });
    

    see http://api.jquery.com/jquery.getjson/ for documentation

    0 讨论(0)
  • 2020-12-29 13:12

    In the doc I can read :

    .multiselect('setOptions', options) Used to change configuration after initializing the multiselect. This may be useful in combination with .multiselect('rebuild').

    Maybe you can't change your widget data by your initial way. In a correct way you should use setOptions method.

    Else : With your way, maybe should you think about destroy your widget .multiselect('destroy') and create it again after.

    Update after comment :

    In the doc : ( you've linked )

    Provides data for building the select's options the following way:

    var data = [
        {label: "ACNP", value: "ACNP"},
        {label: "test", value: "test"}
    ];
    $("#multiselect").multiselect('dataprovider', data);
    

    So : When you get data from your ajax call, you have to create an array of objects ( it's the options in the select you want to have ) with the format like

    var data = 
    [
        {label: 'option1Label', value: 'option1Value'},
        {label: 'option2Label', value: 'option2Value'},
        ...
    ]
    

    When your objects array is created, then you just have to call the method

    $("#multiselect").multiselect('dataprovider', data);
    

    Where data is your array of objects.

    I hope I'm clear :/

    0 讨论(0)
  • 2020-12-29 13:13

    I think this is an easier way to add options on the fly (using ajax or any other listener) to an existing Bootstrap MultiSelect.

    Following is a simplified example to add options:

    function addOptionToMultiSelect(multiselectSelector, value, selected) {
        var data = [];
        $(multiselectSelector + ' option').each(function(){
            var value = $(this)[0].value;
            var selected = $(this)[0].selected;
            data.push({label: value, value: value, selected: selected});
        });
    
        // Add the new item
        data.push({label: value, value: value, selected: selected});
    
        $(multiselectSelector).multiselect('dataprovider', data);
    }
    

    For simplicity, I have assumed both the label and value are the same in an option. Note that the already selected options are taken care of by reading the selected attribute from the existing options. You can make it more sophisticated by tracking the disabled and other attributes.

    Sample:

    addOptionToMultiSelect('#multiselect-example', 'new-option', true);
    
    0 讨论(0)
  • 提交回复
    热议问题