Reputation:
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>
Upvotes: 3
Views: 9568
Reputation:
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,
});
});
Upvotes: 5
Reputation: 912
Your code never reach
else if (options.length === numberOfOptions)
because
else if (options.length > 1)
already covers almost all possible remaining cases (with the exception of options.length === 1
).
Also, allSelectedText
is used by the default buttonText
callback. If you overwrite it and don't use the allSelectedText
text in your code, then that text doesn't show up.
Upvotes: 0