Hi Alisson,
Nice plugin. I didn't have anything better to do this weekend so I
played around and hooked up Knockout. Good opportunity for me to
learn a little ko goodness.
Since the widget makes changes to the original select list via
javascript (no user events to raise), we need to hook into the widgets
custom events. The UI MultiSelect Widget provides all the events out
of the box to do what we need. We wire up the "click", "checkAll",
and "uncheckAll" events.
HTML: >>>>>>>>>>>
<h2>UI MultiSelect Widget -- Test</h2>
<div class="readout">
<h3>What's in the model?</h3>
<table>
<tr>
<td class="label">Multi-selected options:</td>
<td data-bind="text: selectedValues"></td>
</tr>
</table>
</div>
<div>
<select id="example" name="example" multiple="multiple">
</select>
</div>
<<<<<<<<<<<<
JS: >>>>>>>>>>>
var viewModel = {
optionValues : ['Alpha', 'Beta', 'Charlie', 'Delta', 'Eagle'],
selectedValues : ko.observableArray(['Alpha'])
};
$('#example').attr('data-bind','options: optionValues,
selectedOptions: selectedValues');
ko.applyBindings(viewModel);
$('#example').multiselect({
click: function(event, ui){
/*
event: the original event object
ui.value: value of the checkbox
ui.text: text of the checkbox
ui.checked: whether or not the input was checked
or unchecked (boolean)
*/
if(ui.checked &&
viewModel.selectedValues.indexOf(ui.value) < 0){
viewModel.selectedValues.push(ui.value);
}
else if(!ui.checked) {
viewModel.selectedValues.pop(ui.value);
}
},
checkAll: function(){
$('#example').multiselect('getChecked').each(function(){
if(viewModel.selectedValues.indexOf(this.value) < 0)
{
viewModel.selectedValues.push(this.value);
}
});
},
uncheckAll: function(){
viewModel.selectedValues([]);
}
});
<<<<<<<<<<<
Hope the information helps! The code has not been thoroughly tested.
If you find a better way, please let me know. :)