Javascript knockoutJS对每个绑定的动态更改
我有数组的列表,比如表1,表2。。。表10 我想单击表编号,然后在面板中显示该表的列表项 下面是代码片段 HTML,唯一的问题是这一行。我想在单击列表表数时动态更改表数并刷新Javascript knockoutJS对每个绑定的动态更改,javascript,html,knockout.js,Javascript,Html,Knockout.js,我有数组的列表,比如表1,表2。。。表10 我想单击表编号,然后在面板中显示该表的列表项 下面是代码片段 HTML,唯一的问题是这一行。我想在单击列表表数时动态更改表数并刷新 <div data-bind="foreach: table[number].lines"> // <-- this line <p> <span data-bind="text: name"></span>
<div data-bind="foreach: table[number].lines"> // <-- this line
<p>
<span data-bind="text: name"></span>,
<span data-bind="text: qty"></span> @
<span data-bind="text: price"></span> =
<span data-bind="text: extendedPrice"></span>
</p>
</div>
应用KO
ko.applyBindings(table, $('#tablePos').get(0));
我不想使用更多的部分绑定。因为我在这个页面中使用了太多的绑定
谢谢大家您应该让currentTable在ViewModel中可见
var currentTable = ko.observable(table[0]);
并将其绑定到当前表
<div data-bind="foreach: currentTable.lines">
您可以将表保存在observableArray中,并通过索引检索它们
self.SelectedIndex = ko.observable(0); // save index of selected table
self.List = ko.observableArray([]); // list to save your tables
// Retrieve your selected table by ko.computed
self.SelectedList = ko.computed(function() {
return self.List()[self.SelectedIndex()];
});
// ... init your tables or sth below
这就是我解决你的问题。当我显示数据时有点复杂,所以不会注意到它们。如何设置
number
?此外,如果可能的话,您应该尝试重新创建此问题。您尚未向我们展示viewmodel的外观,因此很难看到发生了什么。谢谢,我知道这种方法。但我必须再做一个部分绑定和一个模型。有可能不使用这种方法吗?或者任何其他解决方案?您的ViewModel中可以有多个obserable,或者-您可以应用绑定到具有不同ViewModel的不同DOM元素,您可以进一步指定我可以帮助:),对于问题,如果有任何其他方法-这是如何使用knockout的,我不知道有任何其他方法可以使用knockout
currentTable(table[2]);
function InitViewModel() {
function ViewModelFunction() {
this.currentTable = ko.observable(table[0]);
... more observables
}
window.ViewModel = new ViewModelFunction();
ko.applyBindings(window.ViewModel);
}
$(document).ready(function () {
InitViewModel();
});
var table = new Array();
table[0] = new tableClass('one');
table[1] = new tableClass('two');
table[2] = new tableClass('three');
table[3] = new tableClass('four');
function onSomeEvent(number) {
window.ViewModel.currentTable(table[number]);
}
...
as many bindings as you want to observables in the ViewModel
...
self.SelectedIndex = ko.observable(0); // save index of selected table
self.List = ko.observableArray([]); // list to save your tables
// Retrieve your selected table by ko.computed
self.SelectedList = ko.computed(function() {
return self.List()[self.SelectedIndex()];
});
// ... init your tables or sth below