Commit 00ebed1b authored by Devristo's avatar Devristo

Added KnockoutJS data-binding support

Added KnockoutJS support by using a custom 'multiselect' binding handler.

Example:

1. Define select input
-------------------------
<select class="multiSelect" data-bind="multiselect: true,options: Options, selectedOptions: SelectedOptions, optionsValue: $data" multiple="multiple"></select>

1. Initialize Bootstrap-multiselect
-------------------------------------
$(".multiSelect").multiselect();

3 .Apply view model
parent 0bafa7ce
...@@ -20,12 +20,41 @@ ...@@ -20,12 +20,41 @@
"use strict"; // jshint ;_; "use strict"; // jshint ;_;
if(ko && ko.bindingHandlers && !ko.bindingHandlers.multiselect){
ko.bindingHandlers.multiselect = {
init: function (element) {
var ms = $(element).data('multiselect');
if(!ms)
throw new Error("Bootstrap-multiselect's multiselect() has to be called on element before applying the Knockout View model!");
var prev = ms.options.onChange;
ms.options.onChange = function(option, checked){
// We dont want to refresh the multiselect since it would delete / recreate all items
$(element).data('blockRefresh', true);
// Force the binding to be updated by triggering the change event on the select element
$(element).trigger('change');
// Call any defined change handler
return prev(option, checked);
}
},
update: function (element) {
var blockRefresh = $(element).data('blockRefresh') || false;
if (!blockRefresh) { $(element).multiselect("refresh"); }
$.data(element, 'blockRefresh', false);
}
};
}
function Multiselect(select, options) { function Multiselect(select, options) {
this.options = this.getOptions(options); this.options = this.getOptions(options);
this.select = $(select); this.select = $(select);
this.container = $(this.options.buttonContainer) this.container = $(this.options.buttonContainer)
.append('<button type="button" style="width:' + this.options.buttonWidth + '" class="dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText($('option:selected', select)) + '</button>') .append('<button type="button" class="dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText($('option:selected', select)) + '</button>')
.append('<ul class="dropdown-menu"></ul>'); .append('<ul class="dropdown-menu"></ul>');
// Set max height of dropdown menu to activate auto scrollbar. // Set max height of dropdown menu to activate auto scrollbar.
...@@ -42,6 +71,17 @@ ...@@ -42,6 +71,17 @@
this.select.attr('multiple', true); this.select.attr('multiple', true);
} }
this.addOptions(select, options);
this.select
.hide()
.after(this.container);
};
Multiselect.prototype = {
addOptions: 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) {
if ($(element).is(':selected')) { if ($(element).is(':selected')) {
...@@ -61,9 +101,6 @@ ...@@ -61,9 +101,6 @@
} }
}, this)); }, this));
this.select.hide()
.after(this.container);
// Bind the change event on the dropdown elements. // Bind the change event on the dropdown elements.
$('ul li input[type="checkbox"]', this.container).on('change', $.proxy(function(event) { $('ul li input[type="checkbox"]', this.container).on('change', $.proxy(function(event) {
var checked = $(event.target).prop('checked') || false; var checked = $(event.target).prop('checked') || false;
...@@ -94,10 +131,7 @@ ...@@ -94,10 +131,7 @@
$('ul li a', this.container).on('click', function(event) { $('ul li a', this.container).on('click', function(event) {
event.stopPropagation(); event.stopPropagation();
}); });
}; },
Multiselect.prototype = {
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.
...@@ -143,6 +177,9 @@ ...@@ -143,6 +177,9 @@
// 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);
......
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