Cannot read property 'sorting' of undefined

236 Views Asked by At

Hello I'm new in Angular js.I use Ng-Table module to add pegination,filter into table.But When I try to run following code it shows me sorting of undefined.However I find out that params was not defined that's why it give me error sort of undefined.

$http({
        url:"http://localhost:8080/",
        dataType:'json',
        method:'GET',
        data:'',
        headers:{
            'Content-type':'application/json'
        }
    }).then(function (response) {
        $scope.tableData = response.data;
        $scope.tableParams = new NgTableParams({
            page: 1,
            count: 5
        }, {
            total: $scope.tableData.length,

            getData: function ($defer, params) {
                console.log(params);
                $scope.data = params.sorting() ? $filter('orderBy')($scope.tableData, params.orderBy()) : $scope.tableData;

                $scope.data = params.filter() ? $filter('filter')($scope.data, params.filter()) : $scope.data;

                $scope.data = $scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count());

                $defer.resolve($scope.data);
            }
        });
    });

Question How I can make it work properly in angularjs1.6.1.js?

When I put the following code it works perfect but without filter without sorting

    $scope.tableParams = new NgTableParams({
            page:1,
            count:5
        }, {
            getData: function(params) {
                params.total($scope.tableData.length);
                return $scope.tableData.slice((params.page() - 1) * params.count(), params.page() * params.count());
            }
        }
    );
1

There are 1 best solutions below

2
Sajeetharan On

To use sorting in angularjs, you need to inject the $filter as a dependency

app.controller('TutorialController', function ($scope, $filter, ngTableParams) {