Commit a5120e3f authored by David Stutz's avatar David Stutz

Added rebuild method.

Added a rebuild method suggested in Issue #19. Additionally added
documentation for Knockout JS - see Issue #17.
parent 6241c2e8
...@@ -220,6 +220,32 @@ This method will destroy - unbind - the plugin on the given element(s). ...@@ -220,6 +220,32 @@ This method will destroy - unbind - the plugin on the given element(s).
Refresh the selected elements depending on the selected options within the select. Refresh the selected elements depending on the selected options within the select.
**.multiselect('rebuild')**
Rebuilds the whole dropdown menu. Selected options will still be selected.
## Knockout JS Support
Thanks to [Devristo](https://github.com/Devristo) this plugin supports [Knockout JS](http://knockoutjs.com/). For further discussion see [the pull requests](https://github.com/davidstutz/bootstrap-multiselect/pull/17).
**Define select input**
Note the multiselect: true binding!
<select class="multiSelect" data-bind="multiselect: true, options: Options, selectedOptions: SelectedOptions, optionsValue: $data" multiple="multiple"></select>
**Initialize Bootstrap-multiselect**
$(".multiSelect").multiselect();
**Apply Knockout view model**
As usual.
**Notes**
It is important to initialize the multiselect before applying the view model, since the custom binding code will hook into the onChange method to update the binding.
## Roadmap / Todo ## Roadmap / Todo
* This solution for multiple selects is not usable for mobile devices (especially with touchscreen). ALternatives: Using Popovers instead of Dropdowns or checking for mobile devices and displaying normal select field (one row) for mobile devices. * This solution for multiple selects is not usable for mobile devices (especially with touchscreen). ALternatives: Using Popovers instead of Dropdowns or checking for mobile devices and displaying normal select field (one row) for mobile devices.
......
...@@ -243,6 +243,21 @@ ...@@ -243,6 +243,21 @@
$('#example10-refresh').on('click', function() { $('#example10-refresh').on('click', function() {
$('#example10').multiselect('refresh'); $('#example10').multiselect('refresh');
}); });
$('#example12').multiselect({
buttonContainer: '<span />',
});
$('#example12-rebuild').on('click', function() {
$('#example12').multiselect('rebuild');
});
$('#example12-add').on('click', function() {
$('#example12').append('<option value="add1">Addition 1</option><option value="add2">Addition 2</option><option value="add3">Addition 3</option>');
});
$('#example12-delete').on('click', function() {
$('option[value="add1"]', $('#example12')).remove();
$('option[value="add2"]', $('#example12')).remove();
$('option[value="add3"]', $('#example12')).remove();
});
}); });
</script> </script>
<table class="table table-striped"> <table class="table table-striped">
...@@ -287,6 +302,27 @@ ...@@ -287,6 +302,27 @@
This method is used to refresh the checked checkboxes based on the currently selected options within the select. Click &apos;Select some options&apos; so select some of the options (meaning added the <code>selected</code> attribute to some of the options). Then click refresh. The plugin will update the checkboxes accordingly. This method is used to refresh the checked checkboxes based on the currently selected options within the select. Click &apos;Select some options&apos; so select some of the options (meaning added the <code>selected</code> attribute to some of the options). Then click refresh. The plugin will update the checkboxes accordingly.
</td> </td>
</tr> </tr>
<tr>
<td><code>.multiselect('rebuild')</code></td>
<td>
<div class="btn-group">
<select id="example12" multiple="multiple">
<option value="cheese">Cheese</option>
<option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option>
<option value="onions">Onions</option>
</select>
<button id="example12-add" class="btn">Add some options</button>
<button id="example12-delete" class="btn">Delete some options</button>
<button id="example12-rebuild" class="btn btn-primary">Rebuild</button>
</div>
</td>
<td>
Rebuilds the whole dropdown menu. All selected options will remain selected (if still existent!).
</td>
</tr>
</tbody> </tbody>
</table> </table>
<div class="page-header"> <div class="page-header">
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
"use strict"; // jshint ;_; "use strict"; // jshint ;_;
if(ko && ko.bindingHandlers && !ko.bindingHandlers.multiselect){ if(typeof ko != 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect){
ko.bindingHandlers.multiselect = { ko.bindingHandlers.multiselect = {
init: function (element) { init: function (element) {
var ms = $(element).data('multiselect'); var ms = $(element).data('multiselect');
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
this.select.attr('multiple', true); this.select.attr('multiple', true);
} }
this.addOptions(select, options); this.buildDrowdown(select, this.options);
this.select this.select
.hide() .hide()
...@@ -79,8 +79,7 @@ ...@@ -79,8 +79,7 @@
}; };
Multiselect.prototype = { Multiselect.prototype = {
addOptions: function(select, options){ buildDrowdown: function(select, options){
// Build the dropdown. // Build the dropdown.
$('option', this.select).each($.proxy(function(index, element) { $('option', this.select).each($.proxy(function(index, element) {
...@@ -121,7 +120,7 @@ ...@@ -121,7 +120,7 @@
else { else {
option.removeAttr('selected'); option.removeAttr('selected');
} }
console.log(option);
var options = $('option:selected', this.select); var options = $('option:selected', this.select);
$('button', this.container).html(this.options.buttonText(options)); $('button', this.container).html(this.options.buttonText(options));
...@@ -132,6 +131,7 @@ ...@@ -132,6 +131,7 @@
event.stopPropagation(); event.stopPropagation();
}); });
}, },
defaults: { defaults: {
// Default text function will either print 'None selected' in case no option is selected, // Default text function will either print 'None selected' in case no option is selected,
// or a list of the selected options up to a length of 3 selected options. // or a list of the selected options up to a length of 3 selected options.
...@@ -165,10 +165,6 @@ ...@@ -165,10 +165,6 @@
constructor: Multiselect, constructor: Multiselect,
reset: function() {
},
// Destroy - unbind - the plugin. // Destroy - unbind - the plugin.
destroy: function() { destroy: function() {
this.container.remove(); this.container.remove();
...@@ -177,9 +173,6 @@ ...@@ -177,9 +173,6 @@
// Refreshs the checked options based on the current state of the select. // Refreshs the checked options based on the current state of the select.
refresh: function() { refresh: function() {
$('ul', this.container).html('');
this.addOptions(this.select, this.options);
$('option', this.select).each($.proxy(function(index, element) { $('option', this.select).each($.proxy(function(index, element) {
if ($(element).is(':selected')) { if ($(element).is(':selected')) {
$('ul li input[value="' + $(element).val() + '"]', this.container).prop('checked', true); $('ul li input[value="' + $(element).val() + '"]', this.container).prop('checked', true);
...@@ -194,6 +187,11 @@ ...@@ -194,6 +187,11 @@
$('button', this.container).html(this.options.buttonText($('option:selected', this.select))); $('button', this.container).html(this.options.buttonText($('option:selected', this.select)));
}, },
rebuild: function() {
$('ul', this.container).html('');
this.buildDrowdown(this.select, this.options);
},
// Get options by merging defaults and given options. // Get options by merging defaults and given options.
getOptions: function(options) { getOptions: function(options) {
return $.extend({}, this.defaults, options); return $.extend({}, this.defaults, options);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment