Dynamic ngTable in array
See original GitHub issueHi, I’m facing some issues trying to show multiple ngTable declared into an array.
var dataDest = [[]];
$scope.tableParamsDest = [];
$scope.values = [[]];
for (var i = 0; i < 4; i++) {
dataDest[i] = [];
$scope.tableParamsDest.push(
new ngTableParams({
page : 1,
count : 10,
}, {
total : dataDest[i].length, // length of data
getData : function($defer, params) {
$defer.resolve($scope.values[i] = dataDest[i].slice((params.page() - 1)
* params.count(), params.page()
* params.count()));
}
}));
}
As you can see, each ngTable data is bound to dataDest[i]; these are filled later, calling a server routine when the user press on a button. I pass the index of the current table to the function that updates the dataDest[i], so I’m sure I update the right data of the right table, then i call the $scope.tableParamsDest[$index].reload();
The html looks like the following:
<div ng-repeat="template in templateList" class="container">
...
<table class="table" ng-table="tableParamsDest[$index]">
<tr ng-repeat="user in $data">
<td header="'ng-table/headers/checkbox.html'"><input type="checkbox" ng-model="checkboxesDest[$parent.$index].items[$index]"></td>
<td data-title="'Name'">{{user.name}}</td>
<td data-title="'Email'">{{user.email}}</td>
</tr>
</table>
<script type="text/ng-template" id="ng-table/headers/checkbox.html"><input type="checkbox" ng-model="checkboxes.checked" id="select_all" name="filter-checkbox" value="" /></script>
...
The first time I add items to dataDest[i] and reload the linked ngTable, all works properly. If I add some items to another table, dataDest[i] of this table are correctly updated, but after the reload I see within the second table the data added in the first table! What could be the problem? Thanks in advance.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6
Top GitHub Comments
Yes! I moved the tables creation in another function which takes an index as parameter and now everything works, thanks Christian. If I am correct, the issue is somehow related to this so question: http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
getData, being the inner function, was always bound to the same i value.
cool glad you found a solution