With a function that returns a promise
You can also fetch the data from a server using a function that returns a promise.
The angular-datatables
provides the helper DTOptionsBuilder.withFnPromise(fnPromise)
and the helper DTColumnBuilder
that lets you build the datatables options for each column.
See the API for the complete list of methods of the helper.
angular.module('showcase.withPromise', ['datatables', 'ngResource']).controller('WithPromiseCtrl', WithPromiseCtrl);
function WithPromiseCtrl(DTOptionsBuilder, DTColumnBuilder, $resource) {
var vm = this;
vm.dtOptions = DTOptionsBuilder.fromFnPromise(function() {
return $resource('data.json').query().$promise;
}).withPaginationType('full_numbers');
vm.dtColumns = [
DTColumnBuilder.newColumn('id').withTitle('ID'),
DTColumnBuilder.newColumn('firstName').withTitle('First name'),
DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];
}
data.json
[{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}, {
"id": 870,
"firstName": "Foo",
"lastName": "Whateveryournameis"
}, {
"id": 590,
"firstName": "Toto",
"lastName": "Titi"
}, {
"id": 803,
"firstName": "Luke",
"lastName": "Kyle"
},
...
]