我使用敲除功能将JSON对象映射到用户控件,我有一个复选框列表, 它们看起来像
<input type="checkbox" data-bind="checked: IsEnabled1" />我有JsonObject
var viewModel = { IsEnabled1 :ko.observable(true), IsEnabled2 :ko.observable(true), IsEnabled3 :ko.observable(false) }; ... ko.applyBindings(viewModel);我想添加将被选中/取消选中所有其他复选框的全局复选框,我在JavaScript端进行了此更改,但是全局复选框更新了UI部分,但它们来自单独复选框的数据未映射到JSON对象. /p>
全局复选框
$("#GeneralTable thead tr th:first input:checkbox").click(function () { var checkedStatus = this.checked; $("#GeneralTable tbody tr td:first-child input:checkbox").each(function () { this.checked = checkedStatus; }); });在此代码之后,我的JSON对象包含与UI不相关的数据.
如何从JS端更改复选框后更新所有JSON?
解决方案请检查示例: jsfiddle/MenukaIshan/5gaLjh2c/
我认为这正是您所需要的.所有项目均具有IsChecked可观察的属性. ViewModel包含计算得出的可观察(可读和可写)形式,用于检查或取消选中所有项目.
I using knockout for mapping JSON obejct to user control, I have a list of single checkboxes, They look like
<input type="checkbox" data-bind="checked: IsEnabled1" />I Have JsonObject
var viewModel = { IsEnabled1 :ko.observable(true), IsEnabled2 :ko.observable(true), IsEnabled3 :ko.observable(false) }; ... ko.applyBindings(viewModel);And I want to add global check box that will be check/uncheck all other, I made this changes on JavaScript side but global check box update UI part but they data from separate check boxes doesn't mapping to JSON object .
Global checkbox
$("#GeneralTable thead tr th:first input:checkbox").click(function () { var checkedStatus = this.checked; $("#GeneralTable tbody tr td:first-child input:checkbox").each(function () { this.checked = checkedStatus; }); });after this code my JSON object contain data that not related to UI.
How to update all JSON after change check boxes from JS side ?
解决方案Please check the example: jsfiddle/MenukaIshan/5gaLjh2c/
I think it is exactly what you need. All items have IsChecked observable property. ViewModel contains computed observable (readable and writeable) to check or uncheck all items.