2016-04-16 97 views
5

我已經使用ng-Table創建了一個應用程序,應用程序工作正常,它使用ng-Table生成了表。我面臨的問題是表格排序不起作用。我的代碼如下ng表排序工作不正常

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> 

和我的js代碼:

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); 
    } 
}); 
}); 

這有什麼錯?

回答

3

這將起作用,您需要爲您的數據添加$ filter。

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); 
    } 
}); 
}); 
+0

非常感謝.....現在,它的工作! –

1

試試這個

<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

試試這個。在ngTable文檔中使用這種方式將數據綁定到表。

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

我已經試過這種方式,但它仍然不行..... –

+0

哦不,它的工作原理!我錯了 –