码迷,mamicode.com
首页 > 编程语言 > 详细

ionic项目之排序问题

时间:2015-03-09 19:11:47      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

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">










ionic项目之排序问题

标签:

原文地址:http://blog.csdn.net/superjunjin/article/details/44157421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!