简体   繁体   English

如何在Angular JS中为下面的JSON数组应用过滤器

[英]How to apply Filter for the below JSON Array in Angular JS

I have a JSON array in the following structure 我有以下结构的JSON数组

translations ={
      "English" :[  
                {"Localizedtext":"Submit",
                 "TextCode":1},
                {"LocalizedText":"blah"
                 "TextCode":2 },
             {-------900 odd objects }]
     "SelectedLanguage": [
               "1":{"Localizedtext":"Submit",
                    "TextCode":1,
                    "TextID" :100},
               "2":{"Localizedtext":"development",
                    "TextCode":1,
                    "TextID" :101},
                "1008" :{"Localizedtext":"blah",
                         "TextCode":1,
                         "TextID" :102},
               "80,000":{"Localizedtext":"foo",
                         "TextCode":1,
                         "TextID" :103},
                 {-------900 odd objects }]
              }

My HTML page looks like this : 我的HTML页面如下所示:

<tr ng-repeat="Translation in translations.english|searchFilter : search">
       <td>{{Translation.TextCode}}</td>
      <td>{{Translation.LocalizedText }}
<div><textarea ng-model="translations.selectedLangauge[Translation.TextCode].LocalizedText" ng-change="getTranslations(Translation.TextCode)" class="text-area" rows="2">{{ translations.selectedLangauge[Translation.TextCode].LocalizedText }}
</textarea>
</div>
</td>

My custom filter which i have designed looks like this : 我设计的自定义过滤器如下所示:

     .filter('searchFilter', function () {
        return function (items, search) {
            if (!search){
                return items;
            }
            var result = [];
            angular.forEach(items, function (value, key) {
                //console.log(items);

                angular.forEach(value, function (value2,key2) {
                    //console.log(value2);
                        if (search===value2) {
                            result.push(value2);
                        }



                });

            });
            return result;


        }
    });

The filter that i have designed is applicalble only to translations.english array of the translation object .How to apply this to both the arrays of the object? 我设计的过滤器仅适用于翻译对象的translations.english数组。如何将其应用于对象的两个数组?

You have to concat the arrays before iterating over them. 您必须先遍历数组,然后再遍历它们。 Filter will be applied on the joined arrays. 过滤器将应用于连接的数组。

ng-repeat="Translation in 
    translations.english.concat( translations.SelectedLanguage )|searchFilter : search"

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

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