Ng-Table tableParams
Asked Answered
B

4

7

I have am following the first example of ng-table (http://bazalt-cms.com/ng-table/example/1).

Everything seems to work except tableParams. As soon as I include it in the controller nothing is display in the page.

The difference between the example and my code is that I load data from a json service:

angular.module('mean.cars').controller('CarsController', ['$scope', '$stateParams', '$location', 'Global', 'Cars',
function ($scope, $stateParams, $location, Global, Cars, ngTableParams) {
    $scope.global = Global;
    var data = Cars.query();

$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    count: 10           // count per page
}, {
    total: data.length, // length of data
    getData: function($defer, params) {
        $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }
});

Cars.query(); is working well (tested it). So what am I missing? There is a javascript error: "undefined is not a function occuring" at the following line:

$scope.tableParams = new ngTableParams({
Barbarous answered 28/3, 2014 at 0:23 Comment(1)
I think this this tutorial will help you, it explains exactly what you are trying to do : 4dev.tech/index.php/2015/08/16/…Nyctaginaceous
D
21

I'm also new with Angular and also with ngTable plugin, but I think your problem is that you are not adding the reference for ngTable on your module.

angular.module('mean.cars', ['ngTable'])
       .controller('CarsController', ['$scope', 
                                      '$stateParams', 
                                      '$location', 
                                      'Global', 
                                      'Cars', 
                                      'ngTableParams', 
            function ($scope, $stateParams, $location, Global, Cars, ngTableParams) { ...

You missed the 'ngTable' on the module dependency, and also the 'ngTableParams' on your controller.

Hope this helps you.

Dissident answered 10/4, 2014 at 23:17 Comment(0)
S
5

I'm not sure where ngTableParams comes from, but you're not injecting it:

['$scope', '$stateParams', '$location', 'Global', 'Cars',
function ($scope, $stateParams, $location, Global, Cars, ngTableParams) {

Either that should be like this:

['$scope', '$stateParams', '$location', 'Global', 'Cars', 'ngTableParams',
function ($scope, $stateParams, $location, Global, Cars, ngTableParams) {

Or like this:

['$scope', '$stateParams', '$location', 'Global', 'Cars',
function ($scope, $stateParams, $location, Global, Cars) {    
Shipment answered 28/3, 2014 at 12:41 Comment(2)
I did try both solutions but nothing. With the first one I get a jshint error: 'ngTableParams is not defined'Barbarous
the first version worked for me, this is listing all field names as strings also ..Priestly
P
0

Try this, Replace your ngTableParams -> NgTableParams angular.module('mean.cars').controller('CarsController', ['$scope', '$stateParams', '$location', 'Global', 'Cars', function ($scope, $stateParams, $location, Global, Cars, NgTableParams) {

and

$scope.tableParams = new NgTableParams({ .It worked for me.

Pastime answered 21/7, 2017 at 9:16 Comment(0)
C
-2

run your service in getData

$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    count: 10           // count per page
}, {
    total: data.length, // length of data
    getData: function($defer, params) {
        data = Cars.query();
        $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }
});
Cervelat answered 25/4, 2015 at 0:6 Comment(1)
please explain to the OP whyMiddleweight

© 2022 - 2024 — McMap. All rights reserved.