Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-Table sorting and searching not working when use $resource

Tags:

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.

like image 425
Raas Masood Avatar asked Dec 17 '16 04:12

Raas Masood


1 Answers

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 )

like image 110
Matuszew Avatar answered Sep 23 '22 16:09

Matuszew