这里是淘汰赛新手。我试过使用 ko.utils.arrayFilter 但我使用它时它似乎没有更新。我正在使用与 arrayForEach 相同的方法,所以我不确定这里有什么问题。使用 arrayFilter 时如何获取要更新的列表?
JS:
function entry(name, category) {
this.name = ko.observable(name);
this.category = ko.observable(category);
}
function entriesModel() {
this.entries = ko.observableArray([]);
this.filter = function () {
ko.utils.arrayFilter(this.entries(), function (item) {
return item.category == 'SciFi';
});
};
this.sort = function () {
this.entries.sort(function (a, b) {
return a.category < b.category ? -1 : 1;
});
};
}
$(document).ready(function () {
$.getJSON("entries.php", function (data) {
entries(data);
});
ko.applyBindings(entriesModel());
});
HTML:
<ul data-bind="foreach: entries">
<li>
<p data-bind="text: name"></p>
<p data-bind="text: category"></p>
</li>
<button data-bind="click: filter">Filter</button>
<button data-bind="click: sort">Sort</button>
JSON:
[{"id":"1","name":"Iron Man","category":"SciFi"},{"id":"2","name":"Terminator","category":"SciFi"},{"id":"3","name":"The Pianist","category":"Drama"},{"id":"4","name":"The Hangover","category":"Comedy"}]