Created
August 2, 2016 22:46
-
-
Save amschaal/f63da3d2eb8632dba5ac9fb215bbdf85 to your computer and use it in GitHub Desktop.
Make it easy to set up a server side NgTable with Django Rest Framework API backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('DRFNgTable',['ngTable']) | |
| //Usage: $scope.tableParams = DRFNgTableParams('/api/users/',{sorting: { last_name: "asc" }}); | |
| .factory('DRFNgTableParams', ['NgTableParams','$http', function(NgTableParams,$http) { | |
| return function(url,ngparams,resource) { | |
| var params = { | |
| // page: 1, // show first page | |
| // filter:{foo:'bar'}, //filter stuff | |
| count: 10 // count per page | |
| } | |
| angular.merge(params,ngparams); | |
| return new NgTableParams(params, { | |
| filterDelay: 0, | |
| getData: function(params) { | |
| var url_params = params.url(); | |
| var query_params = {page:url_params.page,page_size:url_params.count,ordering:params.orderBy().join(',').replace('+','')}; | |
| angular.extend(query_params, params.filter()); | |
| // ajax request to api | |
| return $http.get(url,{params:query_params}).then(function(response){ | |
| params.total(response.data.count); | |
| if (resource) | |
| return response.data.results.map(function(obj){return new resource(obj);}); | |
| else | |
| return response.data.results; | |
| }); | |
| } | |
| }); | |
| }; | |
| }]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment