1,一开始用js原生的排序方法,有个错误(超过十条数据后就会排序混乱,可能是在angularjs中使用js原生方法的bug)
var results = results.sort(function(a, b){
switch ( sortKey ) {
case "date":
return a.id < b.id;
case "donedate":
return a.donedate > b.donedate;
case "title":
return a.title > b.title;
case "importance":
return parseInt(b.importance) - parseInt(a.importance);
default:
return a.id < b.id;
}
});
2,其实angularjs有自己的排序方法(orderBy)
http://docs.angularjs.cn/api/ng/filter/orderBy
Usage
In HTML Template Binding
{{ orderBy_expression | orderBy : expression : reverse}}
In JavaScript
$filter('orderBy')(array, expression, reverse)
orderBy_expression:要排序的数据
expression:排序类型
reverse:升序或降序(true或false)
js代码
$scope.sort = function() {
nonePopover();
$ionicActionSheet.show({
buttons: [{
text: '按<b>发布日期</b>排序'
}, {
text: '按<b>完成日期</b>排序'
}, {
text: '按<b>标题</b>排序'
},{
text: '按<b>重要度</b>排序'
}],
titleText: '选择排序方法',
cancelText: '关闭',
cancel: function() {
return true;
},
buttonClicked: function(index) {
var sortKey = "";
switch (index) {
case 0:
$scope.expression = "id";
$scope.reverse = true;
break;
case 1:
$scope.expression = "donedate";
$scope.reverse = false;
break;
case 2:
$scope.expression = "title";
$scope.reverse = true;
break;
case 3:
$scope.expression = "importance";
$scope.reverse = true;
break;
default:
$scope.expression = "id";
$scope.reverse = true;
}
TodoListService.findByGroupId($stateParams.groupId, sortKey).then(function(todolists) {
$scope.todolists = todolists; // 未完成
});
return true;
}
});
}
html代码
<div class="item item-checkbox"
ng-repeat="todo in todolists | orderBy : expression : reverse "
ng-click="show({{todo.id}})"
ng-show="todolists.length">