kennechu
kennechu

Reputation: 1422

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.

Upvotes: 1

Views: 1163

Answers (5)

Elfayer
Elfayer

Reputation: 4561

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.

Upvotes: 2

jithin
jithin

Reputation: 1482

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>

Upvotes: 1

Rogerio Soares
Rogerio Soares

Reputation: 1613

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.

Upvotes: 1

nashter
nashter

Reputation: 171

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;

Upvotes: 2

Sumama Waheed
Sumama Waheed

Reputation: 3609

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.

Upvotes: 5

Related Questions