过滤ng-repeat更多次

时间:2015-06-11 07:18:53

标签: angularjs

我正在创建一个管理餐馆订单的应用程序。

我从$ http创建菜单,所以我有这个列表:

<div class="row vertical" style="background-image: url(/gest/images/etichette/ANTIPASTI.png);border-color: #0CF">
          <div class="card piatti col s2" ng-repeat="anti in antis | filter:{tipo:'ANTIPASTI'}">
<div class="card-content"> <span class="card-title truncate red darken-3">{{anti.piatto}}</span> </div>
            <div class="card-action"> <a href="#" ng-repeat="n in [1, 2, 3, 4, 5]" ng-click="insert(n,com,anti.id,anti.tipo,marcia)">{{n}}</a></div>
          </div>
        </div>

“行垂直”类的div包含一次启动器,然后是面食,然后是牛肉ecc。 所以我每次都使用ng-repeat,并按tipo过滤。 我的问题是:有没有办法让ng-repeat只有一次显示所有菜单(orrrer之前是开胃菜,然后是面食,牛肉ecc)?

我有这个数据(是餐馆菜单):

  • piatto:这道菜的名字
  • tipo:菜的类别(如面食,牛肉,鱼,开胃菜等)

我只用一次重复显示所有订购的菜肴:

starters, pasta, beef, fish, dessert etc. 

每次我都会创建一个新行

2 个答案:

答案 0 :(得分:3)

根据我的理解,您已经在antis上拥有了所有日期,并且您只想按类型过滤它,您想按特定类型订购OrderIt吗?

例如,这个fiddle会按名称排序,但您也可以提供一个数组,其中包含以您喜欢的方式检索每种类型的函数,您可以阅读它here

但基本上你会做

anti in antis | orderBy:'+tipo'

anti in antis | orderBy: [ function(){}, function(){} ]

编辑:

正如@yarons所提到的,您还可以链接字符串以进一步过滤。我已更新Fiddle,因此现在过滤器为anti in antis | orderBy:['+tipo', '+piato']",表示tipo首先按字母排序升序+指示)之后,piato也会按字母顺序递增。

如果你想定义一个不同于字母顺序的顺序,我认为你可以使用tipo的一种ENUM,如:

var tipoENUM = {};
tipoENUM['ANIPASTI'] = 0;
tipoENUM['PASTA'] = 1;
tipoENUM['PIZZA'] = 2;
tipoENUM['BEEF'] = 3;
tipoENUM['DESERT'] = 4;

因此,您可以避免使用字符串作为订单,请参阅以下fiddle示例。

编辑2:

好的,所以如果你通过HTTP请求收到数据,最好是创建一个订单功能来帮助你,检查这个updated fiddle,如下所示:

// The enum would be defined as before but:
$scope.orderTipo = function (dish) {
    return tipoENUM[dish.tipo];
}

在HTMl上你会做:

ng-repeat="anti in antis | orderBy:[orderTipo, '+piato']"

答案 1 :(得分:0)

好的,你的例子很完美,但每次我都会重复#14; tipo&#34;然后是亲戚&#34; piato&#34;在列表....这样的事情:

开胃 - 布鲁斯凯塔 - suppli - fritto

PRIMI - caqrbonara - amatriciana

有可能吗?