maikelm
maikelm

Reputation: 403

ngtable paging and sorting

I have the following problem using ngtable. When I make a http get request and I get data, everything works fine with ngtable, but it gives me no the possibility to order. But if I make a new http get request (without reload page). Then show me the table with the new data, shows me correctly the amount of data of the new request, but does not show paging and not the option to select the number of rows per page. I tryed reload table params, without result.

My code:

    $scope.httpget = function(uri)
    {
        $scope.data=[];
        $http.get(uri).then(
            function(result){
              $scope.data = result.data;
              $scope.tableParams = new ngTableParams({
                        page: 1,
                        count: 10,
                        filter: {
                            message: ''
                        },
                        sorting: {
                            timestamp: 'asc'
                        }
                    },
                    {
                        getData: function($defer, params) {
                            var orderedData = params.sorting() ?
                                $filter('orderBy')($scope.data, params.orderBy()) :
                                $scope.data;

                            params.total(orderedData.length);
                            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                        }
                    });
            }
        );
    }
<table ng-table='tableParams'>
    <tr ng-repeat='d in $data | filter:search'>
         <td data-title="'Collected Timestamp'" sortable="'timestamp'"> {{d.timestamp}} </td>
         <td data-title="'Tag Name'", sortable="'tag_name'"> {{d.tag_name}} </td>
         <td data-title="'Value'", sortable="'value'"> {{d.value}} </td>
    <tr>
</table>	

Result ok after first http get enter image description here

Result bad after second http get (no paging)

enter image description here

Upvotes: 1

Views: 7164

Answers (2)

Vibhor Dube
Vibhor Dube

Reputation: 5071

This happened with me as well. The problem I diagnosed was repeated initialization of ng-table instance. You need to place the initialization of ng-table into separate function.(in my case, it was init())

In your code, it should be something like this:

    $scope.data = [];

    init();
    function init() {
        $scope.tableParams = new ngTableParams({
            page: 1,
            count: 10,
            filter: {
                message: ''
            },
            sorting: {
                timestamp: 'asc'
            }
        },{
            getData: function ($defer, params) {
                var orderedData = params.sorting() ?
                    $filter('orderBy')($scope.data, params.orderBy()) :
                    $scope.data;

                params.total(orderedData.length);
                $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
            }
       });
    };
$scope.httpget = function (uri) {
    $http.get(uri).then(function (result) {
        $scope.data = result.data;
        $scope.tableParams.total($scope.data.length);
        $scope.tableParams.reload();
    }
  );
}

Hope this helps.

Upvotes: 3

bobleujr
bobleujr

Reputation: 1176

Have you used

$scope.tableParams.total(data.length)

and

$scope.tableParams.reload();

at any point? How exactly do you 'do another get request'? Try uploading your code if it doesn't help

Upvotes: 4

Related Questions