Javascript ng表排序不工作
我已经使用ng table创建了一个应用程序,该应用程序运行良好,它使用ng table生成了一个表。我面临的问题是表格排序不起作用。我的代码如下所示 htmlJavascript ng表排序不工作,javascript,angularjs,sorting,ngtable,Javascript,Angularjs,Sorting,Ngtable,我已经使用ng table创建了一个应用程序,该应用程序运行良好,它使用ng table生成了一个表。我面临的问题是表格排序不起作用。我的代码如下所示 html <table ng-table="tableParams" class="table"> <tr ng-repeat="user in myValues"> <td data-title="'Name'" sortable="'name'">
<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>
在HTML中,需要将MyValue更新为$data
<tr ng-repeat="user in $data">
您正在写入
$scope.myValues
,并在ng repeat
指令中使用它-但是您只在表params对象上的getData()
中对数据进行排序
getData()
永远不会更改$scope.myValues
,它只用于返回排序数组。你真正想做的是:
- 不要使整个数据集在作用域上可用,而是将其存储在控制器内的变量中:
var data=[{name:“Moroni”,年龄:50},…]
$defer.resolve($filter('orderBy')(data,params.orderBy())代码>
- 在HTML代码中使用
$data
,因为这是访问getData()
:
将创建新的排序数组,但不会更改$scope.myValues
因此,您可以将$scope.myValues
设置为每次排序的数组:
$scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
$defer.resolve($scope.myValues);
或者在ng repeat
中使用$data
而不是myValues
:
<tr ng-repeat="user in $data">
因此,如果有两个表,那么我将如何判断要获取哪些数据我认为使用$data获取项目显然不是更新数组数据的解决方案。我使用这个函数,我仍然需要对getData函数进行手动筛选,以查看我的排序是否适用。谢谢你的提示
$scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
$defer.resolve($scope.myValues);
<tr ng-repeat="user in $data">