2015-09-14 106 views
2

我正在嘗試使用對象數組進行過濾。但我沒有按照我的要求得到答案。如何從主要對象值過濾對象數組

對象我循環但有3個步驟的數組。我試圖過濾它們並在li元素中顯示,但無法正常工作。只有第一步對我有效。

數組爲:「姓名,子項目and Contracts`(所有嵌套在主對象的內部)

這裏是我的嘗試:

<div class="section filter1"> 
    <ul> 
    <li ng-repeat="value in values"> {{value.Name}}</li> 
    </ul> 
</div> 
<div class="section filter2"> 
    <ul> 
    <li ng-repeat="value in values | SubProjects"> {{$index}}</li> 
    </ul> 
</div> 
<div class="section filter3"> 
    <ul> 
    <li ng-repeat="value in values | SubProjects | Contracts"> {{value.Name}}</li> 
    </ul> 
</div> 

對象樣本:

{ 
    "Id": "1", 
    "Name": "Khalifa International Stadium", 
    "SubProjects": [ 
     { 
     "Contracts": [ 
      { 
      "CompletedPercentage": 0, 
      "Id": "583", 
      "Name": "LP/C21/145", 
      "Phase": null 
      }, 
      { 
      "CompletedPercentage": 0, 
      "Id": "529", 
      "Name": "KP/B21/134", 
      "Phase": null 
      }, 
      { 
      "CompletedPercentage": 0, 
      "Id": "575", 
      "Name": "LP/C21/142", 
      "Phase": null 
      } 
     ], 
     "Id": "2", 
     "Name": "Energy Center" 
     }, 

Live Demo

+1

不知道你所說的「價值觀價值|子項目」的意思是過濾器語法是不同的,反正檢查這兩個來源https://docs.angularjs.org/api/ng/filter/filter和https: //www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=angularjs%20filter%20nested%20array –

+0

我想從使用過濾器的嵌套數組中檢索對象,這是不可能的? – 3gwebtrain

+0

一切皆有可能!檢查相同問題的例子,嘗試一些事情,至少過濾主要數組。用問題創建jsFiddle,然後有人可以幫助你。 –

回答

1

您需要根據您的要求在這裏構建自定義過濾器。請參閱this示例,並讓我知道您是否面臨與您的案例有關的任何問題。有關文件,請撥打check。採用相同方法的更多simple示例。

<input type="text" ng-model="search"> 
    <ul ng-repeat="oneauth in authorisations[0]"> 
     <li ng-repeat="entry in oneauth | nameFilter:search">  
    {{entry.auth.name}}</li> 
    </ul> 


    app.filter('nameFilter', function(){ 
    return function(objects, criteria){ 
     var filterResult = new Array(); 
      if(!criteria) 
      return objects; 

     for(index in objects) { 
      if(objects[index].auth.name.indexOf(criteria) != -1) // filter by name only 
       filterResult.push(objects[index]); 
      } 
      console.log(filterResult); 

     return filterResult; 
    }  
});