[英]Knockout changing data-bind based on user selection
I have some knockout code that consists of a list object and item object. 我有一些由列表对象和项目对象组成的剔除代码。 There could be multiple lists and each list has an array of objects.
可能有多个列表,每个列表都有一个对象数组。 I made this fiddle to demonstrate what I am trying to explain.
我做了这个小提琴来演示我要解释的内容。 What I want to be able to do is have a select box where the user can select a list and then the items from that list will be loaded.
我想要做的是有一个选择框,用户可以在其中选择一个列表,然后将加载该列表中的项目。 Currently I have my data-bind set up like this
data-bind="foreach: lists[0].items"
. 目前,我已经像这样设置了
data-bind="foreach: lists[0].items"
。 The index of the list is hard codded into the bind. 列表的索引很难绑定到绑定中。 Is there some way that I could dynamically assign the index of the list I wish to use?
有什么方法可以动态分配我想使用的列表的索引?
Just put an observable instead of 0
: 只需放置一个可观察的值而不是
0
:
data-bind="foreach: lists[selectedIndex()].items"
With selectedIndex
: self.selectedIndex = ko.observable(0);
与
selectedIndex
: self.selectedIndex = ko.observable(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.