级联下拉列表,彼此无关

时间:2013-03-05 13:29:17

标签: knockout.js

我有2个下拉列表,但彼此之间没有任何关系。我想在第二个下拉列表中隐藏或禁用元素,具体取决于第一个下拉列表的选择。

这里是小提琴link,你可以看到我选择输入一个下拉列表时,它会在其他下拉菜单中显示相关的汽车。 在小提琴中你可以看到数据声明如下

filters: ["None", "Old", "New", "Super"],
items: [{ name: "Corvette", type: "Old"},
       { name: "Charger", type: "Old"},
       { name: "Prius", type: "New"},
       { name: "Magnum", type: "New"},
       { name: "McLaren", type: "Super"},
       { name: "Saleen", type: "Super"}]

您将看到这两个数组之间存在关联。但在我的实际情况中,彼此之间没有任何关系,如下所示

// Mydata will be like
 filters: ["None", "Old", "New", "Super"],
 items: [{ name: "Corvette", type: "Old"},
       { name: "Charger"},
       { name: "Prius"},
       { name: "Magnum"},
       { name: "McLaren"},
       { name: "Saleen"}]

我如何在淘汰赛中实现这一目标?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您只想更改filteredItems计算,以便它在您的数组上执行slice,如:

self.filteredItems = ko.computed(function() {
    var filter = self.filter();
    if (!filter || filter == "None") {
        return self.items();
    } else {
        return self.items.slice(0, 4);
    }
});

示例:http://jsfiddle.net/rniemeyer/3y4YP/