0

I'm using ngTable for AngularJS and geting the data from a web api. The problem start when I tried to use the filters in the table.

If I do it this way, the table is filled with the data, but the filters don't works:

$scope.clientesTable = new NgTableParams({
            page: 1,
            count: 10
        }, {
                getData: function (params) {
                    var clientes = clientesService.getAll(baseUrl);
                    clientes.then(function (data) {
                        $scope.clientesTableData = data.data;
                    }, function (error) {
                        console.log("Error: " + error);
                    });
                }

            });

In the documentation the code is something like this:

$scope.clientesTable = new NgTableParams({
                page: 1,
                count: 10
            }, {
                    getData: function (params) {
                        var clientes = clientesService.getAll(baseUrl);
                        clientes.then(function (data) {
                              params.total(data.inlineCount);
                              return data.results;
                        }, function (error) {
                            console.log("Error: " + error);
                        });
                    }

                });

Doing it this way, I don't see the data in the frontend. If a look for data in clientesTable:

enter image description here

EDIT: Response from web api: enter image description here

5
  • Can you add a console.log after clientes.then(function (data) { ... Commented Oct 20, 2018 at 20:14
  • @Gerfried showing the data? Commented Oct 20, 2018 at 20:17
  • yes, do you receive any data from your service. Is the structure data.data? Commented Oct 20, 2018 at 21:29
  • yes, the data is okey. If i asigne data.data to a var in $scope, then i can see it in the front end. The problem is when i use return data.data just like it is in the documentation. Commented Oct 20, 2018 at 21:56
  • @GuidoCaffa Probably you have to write a return statement before clientes.then() Commented Oct 21, 2018 at 5:11

2 Answers 2

1

You have to provide return keyword before clientes.then() inside getData method.

Sign up to request clarification or add additional context in comments.

Comments

0

If I look at this answer https://stackoverflow.com/a/29629034/3177115 I see that NgTableParams is used differently. You need to use it with a promise which returns the data back into the NgTableParams object.

$scope.tableParams = new ngTableParams({
            page: 1, count: 5,
        }, {
            getData: function ($defer, params) {
                $http.get(testUrl, { params: params })
                     .then(function(res) {
                        $defer.resolve(res.data.items);
                    }, function(reason) {
                        $defer.reject();
                    }
                );
            },
        });

Please see the full example for details, I have simplified the example a bit for better understanding.

EDIT:

I think the API of ng-tables has changed - it does not expect a deferer any more. Looking now at your first code example again (which you say it works) I see why the filter is not working: You do not pass the filter information back to your service.

The ng-table docs say: http://ng-table.com/#/filtering/demo-filtering-basic

Got a getData function? In this case you'll need to apply the values from NgTableParams.filters() to the data you want to display in your table. This is typically the case when the data is being fetched from the server

I don't know your service, but can you add the filter params to the service call?

var clientes = clientesService.getAll(baseUrl, params._params)

Btw: the docs of ng-table are a nightmare - all samples inherit from each other and use directives and injects.

1 Comment

Doing it that way i have this error: TypeError: "params is undefined". I can't find why I have this error, I was using parameters in the code and I wasn't having this problem.

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.