使用ng-repeat限制行数

时间:2015-08-21 14:57:07

标签: angularjs ng-repeat

我想在ng-repeat的表格中最多显示4行。如果有超过4行,我希望在第5行显示一个加号,单击该行时,将显示数据集中的其余行。我不确定从哪里开始。

table.table.table-striped.table-bordered
  thead
    tr
      th.spacer.col-md-8
        | Products: {{co.products.length}} Total - Click to preview
      th.col-md-2
        span.qty-ordered Qty Ordered
      th.col-md-2
        span.co-price Price
  tbody
    tr ng-repeat="prod in co.products"
        td.co-product-name.col-md-6
          a () {{prod.name}}
        td.col-md-3
          span () XX
        td.col-md-3
          span () {{prod.prices[0].price | currency}}

3 个答案:

答案 0 :(得分:7)

使用限制过滤器:

tr ng-repeat="prod in co.products | limitTo : limit"
    td.co-product-name.col-md-6
      a () {{prod.name}}
    td.col-md-3
      span () XX
    td.col-md-3
      span () {{prod.prices[0].price | currency}}
tr ng-show='limit' ng-click='limit = undefined'



# controller

$scope.limit = 4;

答案 1 :(得分:0)

我会使用ng-if这样做:

table.table.table-striped.table-bordered
  thead
    tr
      th.spacer.col-md-8
        | Products: {{co.products.length}} Total - Click to preview
      th.col-md-2
        span.qty-ordered Qty Ordered
      th.col-md-2
        span.co-price Price

  tbody ng-if="expanded"
    tr ng-repeat="prod in co.products"
        td.co-product-name.col-md-6
          a () {{prod.name}}
        td.col-md-3
          span () XX
        td.col-md-3
          span () {{prod.prices[0].price | currency}}

  tbody ng-if="!expanded"
    tr ng-repeat="prod in co.products.slice(0,4)"
        td.co-product-name.col-md-6
          a () {{prod.name}}
        td.col-md-3
          span () XX
        td.col-md-3
          span () {{prod.prices[0].price | currency}}
    tr ng-click="expanded = true" show me more...

答案 2 :(得分:0)

您可以使用提供的$index属性。

逻辑将在控制器和模板中。

控制器中的

$scope.notPlus = true;
模板中的

tr ng-repeat="prod in co.products" ng-if={{ notPlus and $index < 4}}

并且在将noPlus值翻转为false

的链接下方
a ng-click={{notPlus = !notPlus}}

这将符合DRY原则并使您的代码更易于管理

table.table.table-striped.table-bordered
  thead
    tr
      th.spacer.col-md-8
        | Products: {{co.products.length}} Total - a ng-click={{notPlus = !notPlus}} Click to preview
      th.col-md-2
        span.qty-ordered Qty Ordered
      th.col-md-2
        span.co-price Price
  tbody
    tr ng-repeat="prod in co.products" ng-if={{ notPlus and $index < 4}}
        td.co-product-name.col-md-6
          a () {{prod.name}}
        td.col-md-3
          span () XX
        td.col-md-3
          span () {{prod.prices[0].price | currency}}