css - Check all my checkboxes in my table with knockout -
I have a table with the checkbox in the first column I also have a checkbox in the title, on clicking my table All check boxes should be checked in
& lt; Table class = "defaultGrid" & gt; & Lt; Thead & gt; & Lt; TR & gt; & Lt; Th & gt; & Lt; Input type = "checkbox" & gt; & Lt; / Th & gt; & Lt; Th & gt; Id & lt; / Th & gt; & Lt; Th & gt; Name & lt; / Th & gt; & Lt; / TR & gt; & Lt; / Thead & gt; & Lt; Tbody data-bind = "foreach: model.Things" & gt; & Lt; Tr data-bind = "css: {selected: isleded}" & gt; & Lt; Td> & Lt; Input type = "checkbox" data-bind = "checked: selected" /> gt; & Lt; / Td> & Lt; Td data-bind = "text: id" & gt; & Lt; / Td> & Lt; Td data-bind = "text: name" & gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / Tbody & gt; & Lt; / Table & gt;
You should have a function that goes through all the items in the things collection and click IsSelected right or wrong. Then you can tie this function to the "Main" checkbox:
& lt; Input type = "checkbox" data-bind = "click: select all" /> An example implementation will look like this:
self.selectAll = function (data, events) {ko.utils.arrayForEach (self.model. (), Function (item) {if (event.target.checked) items. Selected (true); other item selected (wrong);}); Back true; // to trigger browser default extractor} demo
Or you can get the IsAllSelected property on your Vododal and checked binding and IsAllSelected Make the selection argument in the event of change (or you can also make a useable calculable use). I think that in the end you will need something like this if you want to support scenarios like: Select all - & gt; Unchecking each row with the hand should uncheck the header row etc.
One IsAllSelected computed property self.IsAllSelected = ko. Compute (read: function () {var isAllSelected = true; ko.utils.arrayForEach (self.model.Things ()), function (item) {All are selected = all selected & item are selected () }); The return is all selected;}, write: work (value) {ko.utils.arrayForEach (self.model.Things ()), function (item) {if (value) items. Selected (true); additional items. Selected (wrong);})}}});
Comments
Post a Comment