3

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>

1 Answer 1

6

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.