Javascript AngularJs中的网格排序
我对AngularJs很陌生。 我使用ng repeat获得表中的数据。 现在,我正在尝试对表列进行排序。它没有发生。 请给我建议Javascript AngularJs中的网格排序,javascript,html,angularjs,Javascript,Html,Angularjs,我对AngularJs很陌生。 我使用ng repeat获得表中的数据。 现在,我正在尝试对表列进行排序。它没有发生。 请给我建议 <html ng-app="authorsApp"> <div ng-controller="myAuthors"> <table class="table table-striped table-hover"> <tr> <th n
<html ng-app="authorsApp">
<div ng-controller="myAuthors">
<table class="table table-striped table-hover">
<tr>
<th ng-click="sort{'name'}">
Name
<span class="glyphicon glyphicon-sort" ng-show="sortKey == 'name'" ng-class="{'glyphicon-chevron-up':reverse, 'glyphicon-chevron-down':!reverse}"></span>
</th>
<th ng-click="sort{'department'}">
Deparment
<span class="glyphicon glyphicon-sort" ng-show="sortKey == 'department'" ng-class="{'glyphicon-chevron-up':reverse, 'glyphicon-chevron-down':!reverse}"></span>
</th>
</tr>
<tr ng-repeat="auther in authors | filter: search | orderBy:sortKey:reverse">
<td>{{auther.name}}</td>
<td>{{auther.department}}</td>
</tr>
</table>
</div>
<script src="Scripts/angular.js"></script>
<script src="Assets/js/authors-01.js"></script>
</html>
json文件如下
var app = angular.module("authorsApp", []);
app.controller("myAuthors", function ($scope, $http) {
$scope.authors = [];
$http.get('Assets/js/authors-01.json').then(function (response) {
$scope.authors = response.data;
});
$scope.sort = function (keyname) {
$scope.sortKey = keyname;
$scope.reverse = !$scope.reverse;
}
});
[
{
"name": "Manoj",
"department": "Design"
},
{
"name": "Srikant",
"department": "Business"
}
]
提前感谢。在视图中,如注释中所设置的,您应该调用
sort()
,而不是sort{}
代码示例:
angular
.module(“authorsApp”,[])
.controller(“myAuthors”,函数($scope){
//代码示例的作者。。。
$scope.authors=[{“name”:“Manoj”,“department”:“Design”},{“name”:“Srikant”,“department”:“Business”}];
$scope.sort=函数(键名){
$scope.sortKey=keyname;
$scope.reverse=!$scope.reverse;
}
});代码>
th{光标:指针;}
名称
部门
{{auther.name}
{{作者部门}
使用排序(…)调用函数
不排序{…}
@tanmya,你能再解释一下吗!!函数调用是用括号(…)
而不是大括号{…}
进行的,因此它应该是sort('name')
@tanmya,而不是$scope.sort=function(keyname){$scope.sortKey=keyname;$scope.reverse=!$scope.reverse;}我应该使用$scope.sort=function(keyname)($scope.sortKey=keyname;$scope.reverse=!$scope.reverse;)请提示我。我已经做了更改,正在运行。谢谢。