javascript - AngularJS - Reducing the number of ng-repeats -
javascript - AngularJS - Reducing the number of ng-repeats -
i have register lists people alphabetically z... each person, mr example, has set of corresponding info histories
.
i have table simple ng-repeat
displays info 12 cells (representing 12months).
if 12 months/cells worth of info supplied, show data, if 5 months of info (anything less 12) provided, phone call service srvemptycells
calculates remaining cells , displays in darker colour.
the problem is, notice repeating ng-repeat:
emptycell in getemptycells
many many times impacting page performance given have on 100 users.
is there way save number of empty cells each particular user? , remove need ng-repeats
? directive improve things?
heres plunker: http://plnkr.co/edit/2ukdd1fvfygmjx9ojqvu?p=preview
html:
<table ng-repeat="data in mydata" class="my-table"> <caption> {{ data.name }} </caption> <tbody> <tr> <th>rate</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.rate}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> <tr> <th>effort</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.effort}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> <tr> <th>advance</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.advance}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> <tr> <th>previous</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.previous}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> <tr> <th>current</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.current}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> <tr> <th>code</th> <td ng-repeat="history in data.histories.slice(0, 12)" class="my-table-cell"> {{history.code}} </td> <td ng-repeat="emptycell in getemptycells(data.histories.slice(0, 12).length)" class="empty"></td> </tr> </tbody> </table>
js:
app.controller('mainctrl', function($scope, $http, factorygetjsonfile, srvemptycells) { $scope.name = 'world'; factorygetjsonfile.getmydata(function(data) { $scope.mydata = data.mydata.entries; }); $scope.getemptycells = srvemptycells.getemptycells; }); app.factory('srvemptycells', function() { homecoming { getemptycells: function(len) { var emptycells = []; for(var = 0; < 12 - len; i++){ emptycells.push(i); } homecoming emptycells; } }; });
you modify $scope.mydata
adding cached version of data.histories.slice(0, 12)
, getemptycells(data.histories.slice(0, 12).length)
, using ng-repeat
on instead. way you'd save repeating calls on , on again.
a improve way, if can modify response, service side rather on client. why homecoming more 12 histories if won't need them?
it hard know if prepare performance issue, there many ng-repeats per row, faster before @ least.
sample on caching client side;
factorygetjsonfile.getmydata(function(data) { $scope.mydata = data.mydata.entries; (int i=0; < $scope.mydata.length; i++) { var current = $scope.mydata[i]; current.nonemptycells = current.histories.slice(0, 12); current.emptycells = srvemptycells.getemptycells(current.nonemptycells.length); } });
then alter ng-repeats in html to:
<td ng-repeat="history in data.nonemptycells" class="my-table-cell">
and
<td ng-repeat="emptycell in data.emptycells" class="empty"></td>
updated plunkr: http://plnkr.co/edit/92s2rnnhbeyxzvsci2m8?p=preview
javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat
Comments
Post a Comment