javascript Knockout.Js 数组过滤器语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19942641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Knockout.Js Array filter syntax
提问by aibarra
Just getting into javascript and knockout.js. I've found a bunch of examples of what I'm trying to accomplish. And I feel like there is a small syntax error I may be overlooking. I'm trying to filter a set already returned (this.tasks) from a server via ajax/json. I have that working fine. What I would like to do, is have users be able to switch between complete and incomplete tasks.
刚刚进入javascript和knockout.js。我找到了一堆我正在努力完成的例子。我觉得我可能忽略了一个小的语法错误。我正在尝试通过 ajax/json过滤已经从服务器返回的集合(this.tasks)。我有那个工作正常。我想做的是让用户能够在完成和未完成的任务之间切换。
I switched the code to just run the foreach loop on tasksFiltered. "this.done" is either true or false.
我将代码切换为只在 tasksFiltered 上运行 foreach 循环。“this.done”要么是真的,要么是假的。
Task template
任务模板
var taskModel = function(id, title, description, done){
var self = this;
this.id = ko.observable(id);
this.title = ko.observable(title);
this.description = ko.observable(description);
this.done = ko.observable(done);
this.showEdit = ko.observable(false);
this.titleUpdate = ko.observable(false);
this.descriptionUpdate = ko.observable(false);
};
Page Model
页面模型
var pageModelTasks = function(){
var self = this;
this.task_title = ko.observable("");
this.task_description = ko.observable("");
this.task_title_focus = ko.observable(true);
this.tasks = ko.observableArray([]);
this.tasksFiltered = ko.computed(function() {
return ko.utils.arrayFilter(this.tasks, function(Task) {
return Task.done == true;
});
});
// CRUD functions excluded
};
this doesn't work.
这不起作用。
回答by Vinney Kelly
Two minor corrections to your code. First, as @XGreen mentioned, you need to pass the array value, notthe observableArray instance, as the first parameter of the arrayFilter
function. Lastly, because Task.done
is observable, you need to invoke the member to get the value. Here's the modified code:
对您的代码进行了两个小更正。首先,正如@XGreen 所提到的,您需要传递数组值,而不是observableArray 实例,作为arrayFilter
函数的第一个参数。最后,因为Task.done
是可观察的,所以需要调用成员来获取值。这是修改后的代码:
this.tasksFiltered = ko.computed(function() {
return ko.utils.arrayFilter(this.tasks(), function(Task) {
return Task.done() === true;
});
});
回答by Ivan Rodriguez
The second solution has a problem with the method ko.utils.stringStartsWith.
第二种解决方案在 ko.utils.stringStartsWith 方法上有问题。
ko.utils.stringStartsWith missing in release file.
发布文件中缺少 ko.utils.stringStartsWith。
You can use this code:
您可以使用此代码:
var stringStartsWith = function (string, startsWith) {
string = string || "";
if (startsWith.length > string.length)
return false;
return string.substring(0, startsWith.length) === startsWith;
};
回答by Anil Singh
It might help you
它可能会帮助你
if (myList().length > 0) {
var text= this.filter().toLowerCase();
return ko.utils.arrayFilter(myList(), function (item) {
if (item.Label.toLowerCase().indexOf(text) > -1 || item.Desc.toLowerCase().indexOf(text) > -1) {
return true;
}
else {
return false;
}
});
}