The exact purpose of the getData () function in ngTable

I am using ngTable in a large project. What is the purpose of the getData function sent in the table parameters?

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 suppose it data

is an array of objects in my controller and this data is not found in my DOM like dataTables. I've also seen an object $data

that creates confusion.

Bonus: why do you need this one slice()

?

+3


source to share


1 answer


The purpose of the method getData

is to retrieve data and pass it to ngTable for rendering. It is supposed to load / load the rows to be displayed on the current page, according to the selected filters, sorting, etc. (You use an object to do this params

).

When the data is loaded, say with an ajax request, you must resolve the promise object passed to the getData function with an array of strings prepared.

In the example above, data

array is a local variable, so there is no need to invoke an ajax request to load it (this is just for demonstration), so you can just pass that in to promise. In addition, to simulate paging, additional slicing is performed, for example, if it appeared on an already sliced ​​server.



It getData

will usually be used something like this:

getData: function ($defer, params) {
    $http.get('/accounts/', {params: {page: params.page()}}).then(function(response) {
        $defer.resolve(response.data);
    });
}

      

+3


source







All Articles