Angularjs ng-repeat自定义过滤器会破坏orderBy过滤器吗?

时间:2013-09-11 22:22:48

标签: javascript angularjs

原始数据:

[
{
    "SHFUserID": "400",
    "AlertID": "12",
    "TickerID": "4512",
    "Ticker": "GOOG",
    "Active": "1",
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "4512",
    "Ticker": "GOOG",
    "Active": null,
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "10190",
    "Ticker": "ABM",
    "Active": null,
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "712",
    "Ticker": "DPAX",
    "Active": null,
    "Status": "1"
}

uniqueTickers过滤器:

.filter('uniqueTickers', function() {
return function(tickers) {
    var tags = {};
    angular.forEach(tickers, function(obj) {

  if(!(obj.Ticker in tags)){
        tags[obj.Ticker] = {id: obj.TickerID, name:obj.Ticker};

    if(!tags[obj.Ticker].pending){
      tags[obj.Ticker].pending = 0;
    }
    if(!tags[obj.Ticker].settled){
      tags[obj.Ticker].settled = 0;
    }
    if(!tags[obj.Ticker].order){
      tags[obj.Ticker].order = 3;
    }
  }

  if(obj.Status === "1"){
    tags[obj.Ticker].pending = 1;
    if(tags[obj.Ticker].order > 2){
      tags[obj.Ticker].order = 2;
    }
  }
  if(obj.Status === "2"){
    tags[obj.Ticker].settled = 1;
    if(tags[obj.Ticker].order > 1){
      tags[obj.Ticker].order = 1;
    }
  };
    });
return tags;
};

结果数据:

{"id":"10190","name":"ABM","pending":0,"settled":0,"order":3}
{"id":"712","name":"DPAX","pending":1,"settled":0,"order":2}
{"id":"4512","name":"GOOG","pending":0,"settled":0,"order":3}

HTML:

Search: <input ng-model="query.Ticker">
  Sort by:
  <select ng-model="orderProp">
    <option value="name">Alphabetical</option>
    <option value="order">Alert Status</option>
  </select>
  <ul class="tickerList">
    <li ng-repeat="ticker in tickers | filter:query | uniqueTickers | orderBy:orderProp">
      <a href="#/ticker/{{ticker.id}}">{{ticker.name}}</a>
      <p>{{ticker}}</p>
    </li>
  </ul>

我尝试按“名称”或“顺序”按字母顺序进行过滤,但是在运行uniqueTickers过滤器后,orderBy过滤器不起作用。我不确定我需要向orderBy公开我需要做什么。

1 个答案:

答案 0 :(得分:1)

您目前正在向orderBy公开一个对象,这应该是一个数组。

尝试使用以下内容替换uniqueTickers过滤器末尾的return tags;

var arr = [];
for (var ticker in tags) {
    arr.push(tags[ticker]);
}
return arr;