简体   繁体   English

如何使用 angular ui-select 过滤两个字段中的数据?

[英]How can I filter data in two fields using angular ui-select?

I have an angular app and using ui-select, on the page there two input fields with multiselectors, both of them dropdown lists.我有一个 angular 应用程序并使用 ui-select,在页面上有两个带有多选器的输入字段,它们都是下拉列表。 Everything works fine.一切正常。 When I choose an option in first field (in this case its programming language) the second field (frameworks that belongs to particular programming language) must be filtrated and show only list of correct frameworks.当我在第一个字段(在本例中为它的编程语言)中选择一个选项时,必须过滤第二个字段(属于特定编程语言的框架)并仅显示正确框架的列表。

WORKING CODE:工作代码:

<div class="col-md-2 col-md-offset-2">
                    <ui-select  multiple ng-model="newdeveloper.langs">
                        <ui-select-match placeholder="Select skills">[[ $item.lang_name ]]</ui-select-match>
                        <ui-select-choices repeat="item in (allSkillList.langs | filter: $select.search) track by item.id">
                            <span ng-bind="item.lang_name"></span>
                        </ui-select-choices>
                    </ui-select>
                </div>
                <div class="col-md-2">
                    <ui-select  multiple ng-model="newdeveloper.frameworks">
                        <ui-select-match placeholder="Select frame">[[ $item.frame_name ]]</ui-select-match>
                        <ui-select-choices repeat="item in (allSkillList.frameworks | filter: $select.search) track by item.id">
                            <span ng-bind="item.frame_name"></span>
                        </ui-select-choices>
                    </ui-select>
                </div>

JSON WITH DATA:带数据的 JSON:

{
"frameworks": [
    {
        "id": 1,
        "frame_name": "Django",
        "frame_lang": 1
    },
    {
        "id": 2,
        "frame_name": "jQuery",
        "frame_lang": 2
    },
    {
        "id": 3,
        "frame_name": "Spring",
        "frame_lang": 3
    }
],
 "langs": [
    {
        "id": 1,
        "lang_name": "Python"
    },
    {
        "id": 2,
        "lang_name": "JavaScript"
    },
    {
        "id": 3,
        "lang_name": "Java"
    },
 ]

} }

"frameworks.frame_lang" must match with "langs.id" in order to make filter work properly. “frameworks.frame_lang”必须与“langs.id”匹配才能使过滤器正常工作。

THE QUESTIONS :问题

How can I resolve this problem?我该如何解决这个问题? Should I use some custom filter?我应该使用一些自定义过滤器吗?

Thank you!谢谢!

You have to create a custom filter filterByLang and then apply it to frameworks repeat.您必须创建一个自定义过滤器filterByLang ,然后将其应用于框架重复。

angular.module('demoApp').filter('filterByLang',function(){
    return function(frameworks,langs){
        var filtered = [];
        if(langs && langs.length){
            angular.forEach(frameworks,function(framework){
                angular.forEach(langs,function(lang){
                    if(framework.frame_lang == lang){
                        filtered.push(framework);
                    }
                })    
            });
        }
        return filtered;
    };
});

Inside html update your second dropdown code by.在 html 中更新您的第二个下拉代码。

... ...

<ui-select-choices repeat="item in (allSkillList.frameworks | filterByLang: newdeveloper.langs | filter: $select.search) track by item.id">

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM