如何在Angular JS中为以下JSON数组应用Filter

时间:2017-02-02 05:56:21

标签: javascript angularjs

我在以下结构中有一个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 }]
              }

我的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>

我设计的自定义过滤器如下所示:

     .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;


        }
    });

我设计的过滤器仅适用于翻译对象的translations.english数组。如何将其应用于对象的数组?

1 个答案:

答案 0 :(得分:0)

在迭代数组之前必须连接数组。过滤器将应用于连接的阵列。

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