我的模特很喜欢:
self.filteredItems = ko.computed(function () {
var filter = this.filter().toLowerCase();
if (!filter) {
return this.sites();
} else {
return ko.utils.arrayFilter(this.sites(), function (item) {
return ko.utils.stringStartsWith(item.Name().toLowerCase(), filter);
});
}
}, self);
我将其用于网页上的搜索,而不是stringStartsWith,而是使用某种.contains,因此我得到的结果是我的搜索词包含在字符串中的任何位置,而不仅仅是在开头。
我想这肯定是一个很普通的要求,但找不到任何明显的东西。
有什么建议吗?
最佳答案
您可以简单地使用 string.indexOf
method来检查“字符串包含”:
self.filteredItems = ko.computed(function () {
var filter = this.filter().toLowerCase();
if (!filter) {
return this.sites();
} else {
return ko.utils.arrayFilter(this.sites(), function (item) {
return item.Name().toLowerCase().indexOf(filter) !== -1;
});
}
}, self);
关于asp.net-mvc-4 - 使用包含而不是stringStartsWith knockout js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557789/