Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (57 sloc) 1.62 KB
<html>
<head></head>
<body>
<template id="checkAll">
<form>
<input type="checkbox" name="checkall" data-bind="checked: checkall" />
<ul>
<!-- ko foreach: items -->
<li><input type="checkbox" name="selectedItems" data-bind="checked: $parent.selectedItems, attr: { value: value }" /></li>
<!-- /ko -->
</ul>
</form>
</template>
<checkall params="items: items, selectedItems: selectedItems"></checkall>
<button type="button" data-bind="click: nextClick">Next</button>
<script src="knockout.js"></script>
<script>
function MainViewModel() {
var self = this;
self.items = [
{ id: 1, value: 1 },
{ id: 2, value: 2 },
{ id: 3, value: 3 },
{ id: 4, value: 4 },
{ id: 5, value: 5 }
];
self.selectedItems = ko.observableArray();
self.nextClick = function() {
alert(self.selectedItems());
};
};
function CheckAllViewModel(params) {
var self = this;
self.items = params.items;
self.selectedItems = params.selectedItems;
self.checkall = ko.computed({
read: function() {
return self.items.length == self.selectedItems().length;
},
write: function(newValue) {
self.selectedItems([]);
if (newValue) {
for (var i = 0; i < self.items.length; i++)
self.selectedItems.push(self.items[i].value.toString());
}
}
});
};
ko.components.register('checkall', {
viewModel: CheckAllViewModel,
template: { element: 'checkAll' }
});
var viewModel = new MainViewModel();
ko.applyBindings(viewModel);
</script>
</body>
</html>