javascript - how to import multiple JSON data into table using ng-Tables -
i using ng-table angularjs , using single json file table data. want import multiple json files single table. able import single json file using code below.
thank in advance.
html
<table ng-table="tableparams" show-filter="true" class="table ng-table-responsive"> <tr ng-repeat="user in $data"> <td data-title="'id'" sortable="'id'"> {{user.id}} </td> <td data-title="'name'" sortable="'name'" filter="{ 'name': 'name' }"> {{user.name}} </td> <td data-title="'email'" sortable="'email'"> {{user.email}} </td> <td data-title="'address'" sortable="'address'"> {{user.address}} </td> <td data-title="'personalemail'" sortable="'personalemail'"> {{user.personalemail}} </td> <td data-title="'phoneno'" sortable="'phoneno'"> {{user.phoneno}} </td> <td data-title="'status'" sortable="'status'"> {{user.status}} </td> </tr> </table>
javascript
var app = angular.module('main', ['ngtable']).controller('democtrl', function($scope, ngtableparams, nameservice) { var data = nameservice.data; $scope.tableparams = new ngtableparams( { page: 1, // show first page count: 10, // count per page sorting: {name:'asc'} }, { total: 0, // length of data getdata: function($defer, params) { nameservice.getdata($defer,params,$scope.filter); } }); $scope.$watch("filter.$", function () { $scope.tableparams.reload(); }); }); app.service("nameservice", function($http, $filter){ function filterdata(data, filter){ return $filter('filter')(data, filter) } function orderdata(data, params){ return params.sorting() ? $filter('orderby')(data, params.orderby()) : filtereddata; } function slicedata(data, params){ return data.slice((params.page() - 1) * params.count(), params.page() * params.count()) } function transformdata(data,filter,params){ return slicedata( orderdata( filterdata(data,filter), params ), params); } var service = { cacheddata:[], getdata:function($defer, params, filter){ if(service.cacheddata.length>0){ console.log("using cached data") var filtereddata = filterdata(service.cacheddata,filter); var transformeddata = slicedata(orderdata(filtereddata,params),params); params.total(filtereddata.length) $defer.resolve(transformeddata); } else{ console.log("fetching data") $http.get("json/1.json").success(function(resp) { angular.copy(resp,service.cacheddata) params.total(resp.length) var filtereddata = $filter('filter')(resp, filter); var transformeddata = transformdata(resp,filter,params) $defer.resolve(transformeddata); }); } } }; return service; });
retrieve json content $q.all()
, merge arrays in callback. pass resulting array ng-table.
Comments
Post a Comment