i started with an existing example of ngTable an modified it a bit to use ngResource. using resource factory it populates the data but searching and sorting doesn't works.
http://codepen.io/anon/pen/yVGKMZ
Created a pen above.
var app = angular.module("myApp", ["ngTable", "ngResource", "ngTableDemoFakeBackend"]);
app.factory('orderResource', function ($resource) {
return $resource('https://jsonplaceholder.typicode.com/posts');
});
(function () {
app.controller("demoController", demoController);
demoController.$inject = ["NgTableParams", "$resource", "orderResource"];
function demoController(NgTableParams, $resource, orderResource) {
//debugger;
var ordersGet = orderResource.query({ });
var Api = $resource("/data");
this.tableParams = new NgTableParams({}, {
getData: function (params) {
// **** Section 1 *** sorting and filter does not work
return ordersGet.$promise.then(function (response) {
params.total(100);
return response;
// **** Section 1 ***
//****Section 2 *** this works fine
// return Api.get(params.url()).$promise.then(function(data) {
// params.total(data.inlineCount);
// return data.results;
//****Section 2 ***
});
}
});
}
})();
<div ng-app="myApp">
<div ng-controller="demoController as demo">
<table ng-table="demo.tableParams" class="table table-bordered table-striped table-condensed">
<tr ng-repeat="row in $data track by row.id">
<td data-title="'iss'" filter="{id: 'number'}" sortable="'id'">{{row.id}}</td>
</tr>
</table>
</div>
</div>
In the code if you comment section 1 and un comment section 2 you can observe it working. i also tried simple $http it didn't worked as well.
Well I think I've solved it - probably not the cleanest one but still...
var app = angular.module("myApp", ["ngTable", "ngResource", "ngTableDemoFakeBackend"]);
app.factory('orderResource', function ($resource) {
return $resource('https://jsonplaceholder.typicode.com/posts');
});
(function () {
app.controller("demoController", demoController);
demoController.$inject = ["NgTableParams", "$resource", "orderResource", '$filter'];
function demoController(NgTableParams, $resource, orderResource, $filter) {
//debugger;
var ordersGet = orderResource;
var Api = $resource("/data");
this.tableParams = new NgTableParams({}, {
getData: function (params) {
// **** Section 1 *** sorting and filter does not work
return orderResource.query().$promise.then(function (response) {
params.total(100);
return $filter('orderBy')($filter('filter')(response, params.filter()), params.orderBy())
// **** Section 1 ***
//****Section 2 *** this works fine
// return Api.get(params.url()).$promise.then(function(data) {
// params.total(data.inlineCount);
// return data.results;
//****Section 2 ***
});
}
});
}
})();
I've applied $filter service and I'm ordering table results by this filter ( sorting order I already have from component )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With