Angular ng repeat order将多个字段作为一个字段



我的问题与Angular中的orderBy多个字段不同。

我的数组中有一些对象具有

{
  estimatedStartDate : "..."
}

有些有:

{
  actualStartDate : "..."
}

那么,有没有一种方法可以通过考虑这两个字段来排序呢。

orderBy:['estimatedStartDate','actualStartDate'] 

^这行不通。

这是一把小提琴:http://jsfiddle.net/g201tudg/1/

所以假设这两个字段相同,然后排序

在控制器中添加这样的函数:

$scope.sortByEitherDate = function(row) {
    if (row.actualStartDate) {
        return row.actualStartDate;
    }
    return row.estimatedStartDate;
}

然后用orderBy按它排序:sortByEitherDate

工作小提琴:http://jsfiddle.net/g201tudg/4/

您可以编写自己的自定义订单函数。它需要一个参数——你的卡片——然后返回这两个字段中的任何一个。

JS

    $scope.sort = function(card) {
        return card.actualStartDate !== undefined ? card.actualStartDate : card.estimatedStartDate;
    }

HTML

<div ng-repeat="card in myCards | orderBy:sort">...</div>

https://stackoverflow.com/a/25116296/2474688
试试这个,看看它是否有效。只需通过这个线程,它们就可以通过自定义过滤器根据索引进行排序。

您可以使用自定义OrderBy排序:

<tr ng-repeat="contact in contacts | orderBy:randomSort"> 
$scope.randomSort = function(contact) {
  // your condition
};

根据我的经验,Angular ngRepeat order不喜欢未定义的值,因此将缺少的成员添加为null可以解决您的问题。

顺便说一句,你不需要提到"医生"。只写成员就足够了。

此外,编写自定义排序方法会消耗大量客户端资源。

这是经过编辑的小提琴,它可以正常工作:http://jsfiddle.net/g201tudg/2/

最新更新