ng-table sorting not working
Asked Answered
H

3

8

I have created an application using ng-table, the application is working fine which had generated table using ng-table. The problem which i am facing is that the table sorting is not working. My code is as given below

Working Demo

html

<table ng-table="tableParams" class="table">
        <tr ng-repeat="user in myValues">
            <td data-title="'Name'" sortable="'name'">
                {{user.name}}
            </td>
            <td data-title="'Age'" sortable="'age'">
                {{user.age}}
            </td>
        </tr>
</table>

script

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, $filter, ngTableParams) {
    $scope.myValues = [{name: "Moroni", age: 50},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}, 
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}];

    $scope.tableParams = new ngTableParams({
        sorting: {
            name: 'asc'     
        }
    }, {
        getData: function($defer, params) {
            $defer.resolve($filter('orderBy')($scope.myValues, params.orderBy()));
        }
    });
});
Housekeeper answered 7/10, 2014 at 13:39 Comment(0)
C
20
$defer.resolve($filter('orderBy')($scope.myValues, params.orderBy()));

will create a new sorted array but will not change $scope.myValues.

So either, you set $scope.myValues to the sorted array each time:

$scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
$defer.resolve($scope.myValues);

Or use $data in ng-repeatinstead of myValues:

<tr ng-repeat="user in $data">
Cashman answered 7/10, 2014 at 14:15 Comment(1)
I think that the use of $data to fetch items is not obviously a solution to update the array data. I use this, and I still have to do a manual filter into my getData function in order to see my sort applied. thx for the tipVigil
V
5

In your HTML you need to update the myValues to be $data.

<tr ng-repeat="user in $data">

Plunker

Visser answered 7/10, 2014 at 14:14 Comment(1)
so if there are two tables then how will i tell which data to be takenHousekeeper
A
3

You're writing to $scope.myValues, and using that in the ng-repeat directive - but you're sorting the data only in getData() on the table params object.

getData() doesn't ever change $scope.myValues, it only uses it to return a sorted array. What you really want to do is:

  • Don't make the full dataset available on the scope, but store it in a variable inside the controller:

var data = [{name: "Moroni", age: 50}, ...]

$defer.resolve($filter('orderBy')(data, params.orderBy()));

  • Use $data inside the HTML code, because this is what accesses getData():

<tr ng-repeat="user in $data">

Anadromous answered 7/10, 2014 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.