Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Multiselect Plugin show 'all selected'

I am using David Stutz's bootstrap Multi-select plugin, I am having trouble displaying 'all selected'. Using the allSelectedText method works but it gets overridden when using 'allSelectedText' callback.

Using a previous question's answer I attempted to grab the number of children and check if they are all selected (see comments)

Note - This works if I remove the 'numberofOptions'

Using buttonText callback / allSelectedText method, here's a link to documentation - http://davidstutz.github.io/bootstrap-multiselect/

Your input is appreciated.

JS

   $(document).ready(function() {
        $('#multiselect').multiselect({
            //Following line is being overridden by the numberOfOptions
            allSelectedText: 'All Selected',
            buttonText: function(options, select) {
            //grab the number of childeren to later return 'all selected'
            var numberOfOptions = $(this).children('option').length;
                if (options.length === 0) {
                    return 'Select';
                }
                else if (options.length > 1) {
                    return '1+ Selected';
                }
              //This line is calculating number of options,
              //displaying 'AllSelected in the label'
                else if (options.length === numberOfOptions) {
                    return 'AllSelected';
                }
                 else {
                     var labels = [];
                     options.each(function() {
                         if ($(this).attr('label') !== undefined) {
                             labels.push($(this).attr('label'));
                         }
                         else {
                             labels.push($(this).html());
                         }
                     });
                     return labels.join(', ') + '';
                 }
            },

            buttonWidth: '100%',
            includeSelectAllOption: true,
        });
    });

HTML

<select id="multiselect" multiple="multiple">
        <option value="x">XYZ</option>
        <option value="x">XYZ</option>
        <option value="x">XYZ</option>
        <option value="x">XYZ</option>
        <option value="x">XYZ</option>              
 </select>

1 Answers

I fixed this by using the following, found in the documentation.

numberDisplayed : 1

   $(document).ready(function() {
        $('#multiselect').multiselect({
            allSelectedText: 'All', 
            numberDisplayed: 1,
            buttonWidth: '100%',
            includeSelectAllOption: true,
        });
    });

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!