Javascript Knockoutjs:如何通过参数过滤 observableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8077822/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:38:11  来源:igfitidea点击:

Knockoutjs: How to filter observableArray by param

javascriptknockout.js

提问by Lari13

Here is the code: http://jsfiddle.net/Gr3fT/1/

这是代码:http: //jsfiddle.net/Gr3fT/1/

How to filter personRolesby roleproperty?
Each list must have only own roleitems.

如何personRolesrole属性过滤?
每个列表必须只有自己的role项目。

Thanks.

谢谢。

回答by Lari13

This function works (based on http://knockoutjs.com/examples/animatedTransitions.html:)

此功能有效(基于http://knockoutjs.com/examples/animatedTransitions.html:)

get: function(role) 
{
    return ko.dependentObservable(function () 
    {
        return ko.utils.arrayFilter(this.personRoles(), function(item) 
        {
            return item.id == role;
        });                


    }, viewModel); 
}

回答by user3929314

It might help you.

它可能会帮助你。

This example basically used for filter observable array by passing input parameters at the key up event.

这个例子主要用于通过在 key up 事件中传递输入参数来过滤 observable 数组。

https://stackoverflow.com/a/25241564/3929314

https://stackoverflow.com/a/25241564/3929314