2017-04-07 50 views
0

我想在表頭中創建帶有過濾器的表。過濾器是每列中唯一值的列表。這裏被簡化代碼:

<table> 
    <tr> 
     <th class="dropdown" ng-repeat="field in data.columns"> 
     <span>{{field.title}}</span> 
     <ul class="dropdown-menu"> 
      <li ng-repeat="item in unique(data.items, field)"><checkbox> {{item.text}}</li> 
     </ul> 
     </th> 
    </tr> 
    <tr ng-repeat="item in data.items"></tr> 
</table> 

我使用相同的對象陣列,用於過濾器和表,但過濾器創建陣列的副本,並從被重複的副本的值除去。這是我的問題。

當我複製數組爲array.slice(0)刪除的值不在過濾器中,但也在表中(數組包含對象)。我的問題是在引用,所以我用deepcopy的作爲jQuery.extend(true, [], array)和角度拋出錯誤:

[$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations

我的數據是這樣的:

[{id: 1, title: 'AAA', something: [{id: 1, text: "A"}]}, {id: 2, title: 'AAAA', something: [{id: 2, text: "AA"}]}] 

問題是財產的東西(array.slice(0)不要複製和$ .extend副本,但角度得到錯誤)

感謝意見

+0

'angular.copy()'角度的方式。 – Jai

+1

我認爲如果您將某些操作陣列的邏輯移動到控制器,您的問題就會消失。嘗試將調用移動到唯一的(data.items,field)到控制器函數。如果您可以減少ng-repeat中的一個,這將顯着減少消化循環的次數。 – Matt

+0

另一個角度的方法可能是使用$ filter('filter')或爲ng-repeat創建一個自定義過濾器。 – Matt

回答

1

您還沒有發佈的代碼進行過濾,所以我必須一ssume發生了什麼事。我想你在複製之前不檢查列表中的所有元素是否都是唯一的。這導致過濾器更改數據每個摘要,觸發無限數量的摘要 - 因此錯誤。你的名單永遠不會穩定。

爲了解決這個問題,在過濾器的開頭添加檢查元素的唯一性,如果它們已經是唯一的(例如在第一次過濾之後)就返回輸入對象。這種模式會穩定下來。

0

這裏是我的代碼得到的對象數組的獨特價值:

function toUnique(a, property, innerProperty) { 
     var lastIndex = a.length; 
     if (lastIndex === 0 || lastIndex === undefined) 
      return a; 
     var copyarr = jQuery.extend(true, [], a); 
     if (property !== undefined) { 
      while (prevIndex = --lastIndex) { 
       while (prevIndex--) { 
        var obj1 = copyarr[lastIndex]; 
        var obj2 = copyarr[prevIndex]; 

        if (obj1 !== undefined) { 
         if (copyarr[lastIndex][property] instanceof Array && copyarr[prevIndex][property] instanceof Array) 
          unique(copyarr[lastIndex][property], copyarr[prevIndex][property], innerProperty); 
         else 
          obj1[property] !== obj2[property] || copyarr.splice(prevIndex, 1); 
        } 
       } 
      } 
      return copyarr; 
     } else { 
      while (prevIndex = --lastIndex) 
       while (prevIndex--) 
        copyarr[lastIndex] !== copyarr[prevIndex] || copyarr.splice(prevIndex, 1); 
      return copyarr; 
     } 
    } 

function unique(array1, array2, innerProperty) { 
    for (var i = 0; i < array1.length; i++) { 
     removeDuplicates(array1, i + 1, array1[i], innerProperty); 
     removeDuplicates(array2, 0, array1[i], innerProperty); 
    } 
    for (var i = 0; i < array2.length; i++) { 
     removeDuplicates(array2, i + 1, array2[i], innerProperty); 
    } 
} 

function removeDuplicates(arr, startPos, p, property) { 
    for (var i = startPos; i < arr.length;) { 
     if (p[property] == arr[i][property]) { 
      arr.splice(i, 1); 
     } else { 
      i++; 
     } 
    } 
} 

使用:

var a = [{id: 1, title: "AAAA"}, {id: 2, title: "BBBB"}, {id: 1, title: "AAAA"}] 
    unique(a, "title", null); //return {id: 1, title: "AAAA"}, {id: 2, title: "BBBB"} 

    var b = [{id: 1, title: "AAAA", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter"}, {id: 16, name: "John"}]}, 
{id: 1, title: "BBBB", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter}]}] 

    unique(b, "authors", "name") //return: [{id: 1, title: "AAAA", authors: [{id: 15, name: "John"}, {id: 25, name: "Peter}]}, 
{id: 1, title: "BBBB", authors: []}] 
+0

檢查if(lastIndex === 0 || lastIndex === undefined)後,檢查所有元素是否都是唯一的,如果是,則返回'a'。你也可以考慮使用類似'linq.js'或'lodash'的庫,其中已經實現了獨特的檢查。 – Episodex

相關問題