Pavel Zdarov
Pavel Zdarov

Reputation: 2347

How to dynamically re-draw data in table. Angular ngTable

I have controller with just 1 function getList(). When I call it first time - everything is just fine and my table show proper data. But when I call this function again (with new params in order to get different data in response), I still have data in table from the first call. How to reload/redraw/refresh data?

    module.controller("IPsCtrl", function($scope, $filter, IPService, ngTableParams) {

    $scope.data_out = {
        'ip_pattern' : '%',
    };

    $scope.getList = function() {
        $scope.data=IPService.getList($scope.data_out,
            function(response) {

                $scope.tableParams = new ngTableParams({
                        page: 1,            // show first page
                        count: 10,          // count per page
                        filter: {
                             id: ''       // initial filter
                        },
                        sorting: {
                             id: 'asc'     // initial sorting
                        }
                    }, {
                      total: response.list.length, // length of data
                      getData: function($defer, params) {
                            // use build-in angular filter
                            var filteredData = params.filter() ?
                                      $filter('filter')(response.list, params.filter()) :
                                      response.list;
                            var orderedData = params.sorting() ?
                                      $filter('orderBy')(filteredData, params.orderBy()) :
                                      response.list;

                            params.total(orderedData.length); // set total for recalc pagination
                            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                    }
                });


            });
    };
});

Here is html view

<table ng-table="tableParams" show-filter="true" class="table">
                <tr ng-repeat="user in $data">
                     <td data-title="'Name'" sortable="'id'" filter="{ 'id': 'text' }">
                          {{user.id}}
                     </td>
                     <td data-title="'Age'" sortable="'value'" filter="{ 'value': 'text' }">
                          {{user.value}}
                     </td>
                </tr>
            </table>

Upvotes: 3

Views: 7037

Answers (1)

bajuwa
bajuwa

Reputation: 330

There is a provided method to do just that:

$scope.tableParams.reload();

Just call it after you update the data the table is displaying.


Edit:
First off, I think your current use of ngTable is incorrect. Instead of tying the table directly to the response data, I think the best thing to do would be to store your response data in a $scope variable, and then attach it to your ngTable. That way you only have to manipulate the $scope data and call reload.
From there it is simply a matter of updating the stored data then calling reload:

// Store your data in a dedicated variable
$scope.myData = response.list;

// Update ngTable to reflect the new data
if ($scope.tableParams) {
    $scope.tableParams.reload();
} else {
    // create your table as before, 
    // but tying it to $scope.myData 
    // instead of response.list
}

Upvotes: 6

Related Questions