knockout.js - Table not updated when ko.observablearray changes -
i'm binding ko.observablearray table. each object (called 'group') in array bound table. each group holds (non-knockout) array of codes. each code bound row. each code has boolean, bound checkbox.
if none of booleans true in group, want checkboxes group enabled. if @ least 1 of booleans true in group, want false checkboxes group disabled, , true checkboxes enabled. when page loaded, works expected; however, when check or uncheck box, enabled/disabled status of boxes not change. looking @ debugger, observablearray being updated, data in table not.
page:
<table data-bind="foreach: {data: groups}"> <thead> <tr> <th colspan="3" data-bind="text: groupname"></th> </tr> </thead> <tbody data-bind="foreach: {data: codes}"> <tr> <td data-bind="text: codeid"></td> <td data-bind="text: desc"></td> <td> <input type="checkbox" data-bind="checked: prime, disable: $root.hasprime($parent) && !prime" /> </td> </tr> </tbody> </table>
javascript function tests if there true values in group:
var hasprime = function (gr) { (var = 0; < gr.codes.length; i++) { if (gr.codes[i].prime === true) { return true; } } return false; };
fiddle: http://jsfiddle.net/nslpu/2/
if want two-way binding you'll need make things observable. in case, prime
properties have observables:
var code1 = { 'codeid': 123, 'desc': 'code 1', 'prime': ko.observable(false) // <---- changed! };
this require corresponding changes in rest of code:
- plain js calls have invoke
prime
method current value - bindings "do stuff" (like negation, on input) need invoke observable value negated
see this fiddle update.
Comments
Post a Comment