AngularJs中列表分隔符的降序

时间:2014-12-14 23:33:04

标签: angularjs angularjs-ng-repeat ionic-framework

我如何以降序的方式订购<div class="item item-divider">,我希望在哪个最近的日期?

// index.html
...
<ion-content>
  <div ng-repeat="(date,list) in items">
            <div class="item item-divider">
                {{date}}
            </div>
            <ul class="list">
                <li class="item item-icon-right" ng-repeat="item in list">
                    <h2>{{item.name}} {{item.surname}}</h2>
                </li>
            </ul>
        </div>
</ion-content>
...

-------------------------------------------------------------------------------------------------

// app.js

angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope) {

   $scope.items = {
     '11/12/2014':[
       {name:'mark',surname:'john',birth:'11/12/2014'}
     ],
     '12/12/2014':[
       {name:'tom',surname:'smith',birth:'12/12/2014'}
       {name:'carl',surname:'northon',birth:'12/12/2014'}
     ]
   }

});

3 个答案:

答案 0 :(得分:0)

您是否可以将数据结构更改为包含两个键'date''people'?然后你可以做到 items | orderBy:'date'

答案 1 :(得分:0)

为什么不在将对象传递给angular之前对其进行排序。

function sortObjectByKeys(object, order) {
    var sortedObject = {}, key, tempKeyArray = [];

    for (key in object) {
        if (object.hasOwnProperty(key)) {
            tempKeyArray.push(key);
        }
    }

    // You can also retrieve all the keys using Object.keys() method and then sort them..

    tempKeyArray.sort(function(a, b){return b - a});

    for (key = 0; key < tempKeyArray.length; key++) {
        sortedObject[tempKeyArray[key]] = object[tempKeyArray[key]];
    }

    return sortedObject;
}

答案 2 :(得分:0)

这应该可以正常工作:

$scope.mysort = function(obj){

    var sortedObject = {}, key, tempKeyArray = [];

    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            tempKeyArray.push(key);
        }
    }
    tempKeyArray.sort(function(a, b){
        var parts = a.split("/");
        a = new Date(parts[2], parts[1] - 1, parts[0])
        parts = b.split("/");
        b = new Date(parts[2], parts[1] - 1, parts[0]);
        return b-a;
    });

    for (key = 0; key < tempKeyArray.length; key++) {
        sortedObject[tempKeyArray[key]] = obj[tempKeyArray[key]];
    }
    return sortedObject;
}