Reputation: 7038
I'm using ngTable in a big project. What is the exact purpose of the getData function sent in the table params ?
vm.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function ($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
I guess that data
is an array of objects in my controller, and that data are not found into my DOM like dataTables does.
I have also seen a $data
object that make confusion.
Bonus : Why is this slice()
stuff needed ?
Upvotes: 3
Views: 4158
Reputation: 193271
The purpose of the getData
method is to retrieve the data and pass it to ngTable for rendering. It is supposed to fetch/load rows that should be displayed on the current page, according to selected filters, sorting, etc (you use params
object for this).
When the data is loaded, say with ajax request you are supposed to resolve promise object passed into getData function with prepared array of rows.
In the example above data
array is a local variable so there is no need to issue ajax request to load it (this is just for demo), so you can simply pass it to promise. Also additional slicing is performed in order to simulate paging, like if it came from the server already sliced.
Usually getData
would be used something like this:
getData: function ($defer, params) {
$http.get('/accounts/', {params: {page: params.page()}}).then(function(response) {
$defer.resolve(response.data);
});
}
Upvotes: 3