Knockout.js 使用tempate节点绑定时,使Knockout根据对我的列模型的更改重新绘制数据列
我的问题场景几乎与相同,但我正在绘制的表具有更复杂绑定的TD单元格,每个绑定对于被绑定的列都是唯一的。有时候,HTML是独一无二的 换句话说,使用Knockout.js 使用tempate节点绑定时,使Knockout根据对我的列模型的更改重新绘制数据列,knockout.js,knockout-3.0,Knockout.js,Knockout 3.0,我的问题场景几乎与相同,但我正在绘制的表具有更复杂绑定的TD单元格,每个绑定对于被绑定的列都是唯一的。有时候,HTML是独一无二的 换句话说,使用databind=foreach循环列并简单地嵌套执行text绑定的列是不够的 我可以使用模板{nodes:xxx}绑定在初始页面绘制时获得要呈现的表,在该绑定中我传入一个DOM节点数组。。像这样: <table> <thead> <tr>
databind=foreach
循环列并简单地嵌套执行text
绑定的列是不够的
我可以使用模板{nodes:xxx}
绑定在初始页面绘制时获得要呈现的表,在该绑定中我传入一个DOM节点数组。。像这样:
<table>
<thead>
<tr>
<!-- ko foreach: ColumnModel.VisibleColumns -->
<th data-bind="click:$root.ColumnModel.ColumnSortClick,text:ColName"></th>
<!-- /ko -->
</tr>
</thead>
<tbody>
<!-- ko template: { nodes: ColumnModel.getRowTmplNodes(), foreach: DataItems} -->
<!-- /ko -->
</tbody>
</table>
文档声明,传递给此变量的DOM节点不允许是observableArray。因此,您可以想象,当我允许用户更新列模型时,只有标题标签在中更改,但数据列不更新
我该怎么办?通过使用自定义敲除绑定解决 HTML:
<table>
<thead>
<tr>
<!-- ko foreach: ColumnModel.VisibleColumns -->
<th data-bind="click:$root.ColumnModel.Column_Click" style="cursor:pointer;"><span data-bind="visible:SortDirection,css:IconClass" style="font-size:small"></span><span data-bind="text:ColName"></span></th>
<!-- /ko -->
</tr>
</thead>
<tbody data-bind="foreach:{data:DataItems,as:'thg'}">
<tr data-bind="nodes: $root.ColumnModel.getRowTemplate()"></tr>
</tbody>
</table>
//THANK YOU, MICHAEL BEST:
ko.bindingHandlers.nodes = {
init: function () {
return {controlsDescendantBindings: true};
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var nodes = ko.unwrap(valueAccessor());
ko.virtualElements.setDomNodeChildren(element, nodes);
ko.applyBindingsToDescendants(bindingContext, element);
}
};
ko.virtualElements.allowedBindings.nodes = true;
好的,我找到了一种方法来实现这一点,并从使用
模板
绑定切换到自定义绑定处理程序,您可以将其作为答案发布并接受它。