简体   繁体   中英

Hide last 2 records from angular ng-repeat

I have a table with the ng-repeat property that populates data coming from a WS, I hide the last record because I dont need it with ng-hide="$last". Now there was an update to the WS and I need to hide the last 2 records but I dont know how to do this.

I have something like this.

<table>
  <tr ng-repeat="d in exportData.result" ng-hide="$last">
     ...code....
  </tr>
</table>

Any help on this.

Replace

d in exportData.result

To:

d in (exportData.result.slice(0, exportData.result.length-2))

Another way is to do the same thing in your controller.

I'd call a function from the ng-repeat :

<table>
  <tr ng-repeat="d in getTruncatedArray()">
     ...code....
  </tr>
</table>

In you Controller:

$scope.getTruncatedArray = function() {
    return exportData.result.slice(0, exportData.result.length - 2));
}

Explanation

I'd suggest you to call a method instead of writing your "filter" directly in the HTML. Calling a method will keep your HTML clean and it makes your "filter" easy to update/modify .

Why not use Angular's limitTo filter on your ngRepeat?

 <tr ng-repeat="d in exportData.result | limitTo:quantity">

And then in your controller update the quantity to reflect your new desired value

$scope.quantity = $scope.exportData.result.length - 2;

One another dirty way :)

<tr ng-repeat="d in exportData.result track by $index" ng-show="$index < exportData.result.length -2">
    ...code....
</tr>

Dont forget that ng-show/hide create dom elements, it only play with css visibility, filtering your array as @nashter suggest avoid that behaviour.

See fiddle

<div ng-controller="MyCtrl">
<div ng-repeat="line in lines">
<div class="preview" ng-hide="((lines.length-2)==$index)||($index==$last)">{{$index}}   </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM