I have an array of objects as:
[
{ID: "1234", FName: "Steve", LName: "Adams", Status: "New",CreatedBy: "sadams"},
{ID: "5648", FName: "Jon", LName: "Lewis", Status: "New",CreatedBy: "jlewis"},
{ID: "9872", FName: "Hor", LName: "Mt", Status: "Open",CreatedBy: "hmt"},
{ID: "1212", FName: "Allison", LName: "Pan", Status: "New",CreatedBy: "apan"},
...
...so on
]
This data is returned from my API and can be in hundreds.
Now I am using simple HTML table without any third party sources to show this data on the screen and I am using ng-repeat in angularjs to bind this data to the table. Because of so much data I dont want to load entire data at once on the screen. I just want to load first 50 rows and then have a link as "Show next 50". Clicking on which the next 50 rows will be pushed to the array and so on.
This is what I have currently
$http.get('https://myApi').success(function(data) {
var arr = [];
//This holds all the data returned
$scope.dataFromApi = data;
for (var j = 0; j < 50; j++) {
//This holds the first 50 rows
arr.push(data[j]);
}
//This is binding back to the UI
$scope.tableData = arr;
}
$scope.buttonClick = function () {
//How can I get the next 50 rows from $scope.dataFromApi and add it to $scope.tableData
}
How can I get the next 50 rows everytime on the button click and add to the existing array.
Please note that I am not looking for any other third party sources at this moment or any paginations methods.
---Updated---
For anyone else looking for this. I got it resolved as:
Creating a custom filter (as I am using angularjs 1.3.4) as:
app.filter('myLimitTo', function() {
return function(input, limit, begin) {
return input.slice(begin, begin + limit);
};
});
In my html:
<tr ng-repeat="r in datarows | myLimitTo:limit:start">
Controller:
$scope.start = 0;
$scope.limit = 10;
$scope.next = function () {
incrementLimit(true)
}
$scope.prev = function () {
incrementLimit(false)
}
function incrementLimit(up) {
if (up) {
($scope.start <= ($scope.data.rows.length - $scope.limit)) ? $scope.start += 10 : $scope.start = 0;
} else {
$scope.start > 10 ? $scope.start -= 10 : $scope.start = 0;
}
}