-->

Ng-table get filtered data

2019-05-31 16:07发布

问题:

this is an extract of a code im working on using ng-table. My problem is really simple and supposedly it should be plain easy to overcome but im just unable to do it.

This is the extract :

$scope.loading++;
            clean();    
            $scope.environment = "SOMEENV";
                    $http({
                        method: 'GET',
                        url: 'http://SOMEIP:SOMEPORT/all?environment=SOMEENV' 
                        }).then(function successCallback(response) {
                                // this callback will be called asynchronously
                                // when the response is available

                                $scope.nodes = response.data;
                                $scope.chartdataservtype=countservtype(response.data);
                                $scope.tableParams  = new NgTableParams({}, { dataset: response.data.rows  });

... more code after that.

Lets says that later on, I want to get the FILTERED DATA, not only of the visible part of the table, but from the WHOLE TABLE. According to their GitHub repo, I should be able to do that so easily as writing :

var filteredData = $scope.tableParams.data;

But the problem is that this approach is giving me ONLY THE VISIBLE rows on the table. And I want the whole set of data.

There is some people saying I can customize the getData function of ng-table ( although on their repo other people is saying is not needed as it is "solved" ) but i dont know how to do that.

Can you guys help with this?

回答1:

You can access the filtered data including the ngTableEventsChannel service in your controller, and listen for filter changes:

function yourController($scope, NgTableParams, ngTableEventsChannel) {
    activate();

    function activate() {
        ngTableEventsChannel.onAfterDataFiltered(function(tableParams, filteredData){
            //DO SOMETHING
        });
     }
}

In that case, tableParams will be the NgTableParams instance that has changed. And filteredData will be what you want to access, your filtered data (yahoo!).

More info: http://ng-table.com/api-docs/classes/ngtableeventschannel.html