2014-12-19 118 views
1

我有我想要一個特定的方式進行排序的對象數組:角NG重複多排序

[ 
    { 
    name: 'Blackcurrant', 
    created_at: '3/2/2014' 
    },{ 
    name: 'Bilberry', 
    created_at: '1/2/2013' 
    },{ 
    name: 'Apple', 
    created_at: '11/5/2012' 
    },{ 
    name: 'Boysenberry', 
    created_at: '1/1/2014' 
    },{ 
    name: 'Coconut', 
    created_at: '3/4/2011' 
    },{ 
    name: 'Breadfruit', 
    created_at: '6/6/2013' 
    },{ 
    name: 'Currant', 
    created_at: '2/8/2014' 
    },{ 
    name: 'Avocado', 
    created_at: '5/13/2014' 
    },{ 
    name: 'Banana', 
    created_at: '7/16/2014' 
    },{ 
    name: 'Blueberry', 
    created_at: '1/18/2010' 
    },{ 
    name: 'Apricot', 
    created_at: '9/17/2014' 
    },{ 
    name: 'Date', 
    created_at: '2/22/2012' 
    },{ 
    name: 'Damson', 
    created_at: '10/10/2014' 
    },{ 
    name: 'Cantaloupe', 
    created_at: '8/13/2014' 
    },{ 
    name: 'Cranberry', 
    created_at: '4/25/2011' 
    },{ 
    name: 'Cucumber', 
    created_at: '7/6/2014' 
    },{ 
    name: 'Blackberry', 
    created_at: '2/10/2014' 
    },{ 
    name: 'Cloudberry', 
    created_at: '1/23/2014' 
    },{ 
    name: 'Cherry', 
    created_at: '7/11/2013' 
    },{ 
    name: 'Cherimoya', 
    created_at: '11/19/2014' 
    } 
] 

我需要能夠有它設置,以至於當我做一個ng-repeat,Coconut,Banana,Cantaloupe和Cherry在開始時按順序顯示,然後按照created_at日期排序。有可能使用orderBy和過濾器的組合嗎?

+0

推JSON值到你希望的順序數組。然後在視圖中引用數組而不是JSON。 –

+0

你需要order by你的自定義函數 –

+0

@TheHeadRush:我會做那樣的事情,但我需要原始的範圍,因爲它不斷更新,它只是更多的工作,以不斷更新新的JSON每次它的變化。 – m0ngr31

回答

0

這是我落得這樣做:

.filter('fruitSort', function() { 
    return function(fruits) { 
    var favoriteFruit = []; 
    var newFruit = []; 

    angular.forEach(fruits, function(fruit) { 
     if (fruit.name === 'Coconut' || fruit.name === 'Banana' || fruit.name === 'Cantaloupe' || fruit.name === 'Cherry') { 
     if (fruit.name === 'Coconut') { 
      favoriteFruit[0] = circle; 
     } else if (fruit.name === 'Banana') { 
      favoriteFruit[1] = circle; 
     } else if (fruit.name === 'Cantaloupe') { 
      favoriteFruit[2] = circle; 
     } else { 
      favoriteFruit[3] = circle; 
     } 
     } else { 
     newFruit.push(circle); 
     } 
    }); 

    newFruit.sort(function(a,b){return new Date(b.created_at).getTime() - new Date(a.created_at).getTime();}); 
    var sortedFruit = favoriteFruit.concat(newFruit); 

    return sortedFruit; 
    }; 
}) 
1

您可以用created_at屬性對ngRepeat元素進行排序,方法是使用orderBy過濾器設置您的重複。

<li ng-repeat="food in arrFoods | orderBy:created_at"> 
    <span>{{food.name}}</span> 
</li> 

是確保你有「椰子,香蕉,哈密瓜,櫻桃顯示出來首先,你需要創建自己的排序依據一定的價值的方法。對於我的例子中,我已經添加了財產你想在陣列的頂部顯示的元素,以前最喜歡的created_by被命令。

{ 
    name: 'Coconut', 
    created_at: '3/4/2011', 
    fave: true 
} 

然後將它應用到你的ngRepeat元素...

<li data-ng-repeat="food in arrFoods | orderBy:['fave', 'created_by']"> 

通過應用排序參數數組,我們優先將fave設置爲true的元素(不需要打擾將其他設置爲false),然後按created_by排序。

>>Working Plunkr Example

-mbp

+0

感謝您花時間回答。不幸的是,我不認爲我每次都會以相同的順序獲得'fave'水果。我會測試,但現在我的plunkr沒有加載。我結束了寫我自己的過濾器似乎工作。 – m0ngr31

+0

反正你會介意加註嗎?既然這是你的問題的工作答案? – ManBearPixel

+0

當然可以。感謝您爲我進行調查。 – m0ngr31