Ng-Table sorting doesn't work well

I created an application using ng-Table, the application works fine, which created a table using ng-Table. The problem I am facing is that table sorting is not working. My code is below

HTML:

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

and my js code:

  var app = angular.module('myApp', ['ngTable']). controller('mycontroller', function($scope, NgTableParams) { var data = [{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(data); } }); }); 

NOTHING IS WRONG?

+5
source share
4 answers

This will work, you need to add $ filter for your data.

 var app = angular.module('myApp', ['ngTable']). controller('mycontroller', function($scope, NgTableParams,$filter) { var data = [{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) { data = $filter('orderBy')(data, params.orderBy()); $defer.resolve(data); //$defer.resolve(data); } }); }); 
+3
source

try it

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

OR

  <table class="table"> <tr> <th>Name</th> <th>Age</th> </tr> <tr ng-repeat="user in $data | orderBy:'-age'"> <td>{{user.name}}</td> <td>{{user.age}}</td> </tr> </table> 
+1
source

Have you tried the smart table. This is an Angularjs module that easily displays data in a table with a set of built-in functions such as filtering, sorting, etc.:

http://lorenzofox3.imtqy.com/smart-table-website/#section-intro

It is easy to use and looks great!

+1
source

try it. in an ngTable document using this method to bind data to a table.

 $scope.tableParams = new NgTableParams({ sorting: { name: 'asc' } }, { dataset: data }); 
+1
source

Source: https://habr.com/ru/post/1247267/


All Articles