ordering ng-repeat by several properties

时间:2016-04-25 09:38:51

标签: angularjs variables filter properties ng-repeat

I have a table to indicate results of certain teams, each team has two values - PTS (points) and GD (goal difference).

Data looks something like this:

A: PTS=3, GD=+3,
B: PTS=2, GD=+1,
C: PTS=2, GD=-1,
D: PTS=0, GD=-1

I want display them via ng-repeat, in order of PTS first and foremost (as above). But if any team is equal with another on points, it will need to be decided by GD (goal difference). Finally if even this another sort them, I want it to fall back to alphabetical ordering.

I know how to use orderBy, and then can just use the teamName propery (A, B, C) etc. to order them.

I am guessing it is something like creating a custom orderBy filter something like this:

orderBy="{{pts || gd || teamName}}"

I'm not sure of the syntax though, or where to put this code. Can I attach the above logic to one variable and then call that in orderBy? Or is it a filter?

Any help with this would be appreciated.

1 个答案:

答案 0 :(得分:0)

要将orderBy与多个字段一起使用,您必须在数组中添加此字段,如['pst','gd','teamName']

现在数据是:

      A: PTS=3, GD=+3,
      B: PTS=2, GD=+1,
      C: PTS=2, GD=-1,
      D: PTS=0, GD=-1

如果考虑“A”,“B”,“C”和& “D”是团队名称。

  • teamName - > 升序订单
  • gd - > 升序订单
  • pts - > 降序订单

然后orderBy语法是:

        orderBy:['-pst', 'gd', 'teamName']

***“ - pst”用于按降序显示pst元素