如何根据angularjs中的上一列对列进行排序

时间:2015-03-16 16:53:09

标签: angularjs filter sql-order-by

我想对两列进行排序,这些列是链接的示例:我对名称进行排序,然后我想按日期对名称列表进行排序,但是,当我这样做时,它会覆盖我的排序并按日期对其进行排序忽略按名称排序,

是否可以按名称排序日期?单击标题时? 如果有人可以给我一个提示吗?

$scope.filteredList =null;
$scope.orderByName = function(predicate,reverse){
    if($scope.filteredList == null) {
        $scope.dataList = $filter("orderBy")($scope.dataList, predicate, reverse);
        $scope.filteredList =  $scope.dataList;
    }
    else{
    $scope.dataList = $filter("orderBy")($scope.filteredList,predicate,reverse);
    $scope.filteredList =  $scope.dataList;
    }

};
$scope.orderByDate = function(predicate,reverse){
    if($scope.filteredList == null){
        $scope.dataList = $filter("orderBy")($scope.dataList,predicate,reverse);
        $scope.filteredList =  $scope.companyInfo;
    }
    else{
        $scope.companyInfo = $filter("orderBy")($scope.filteredList,predicate,reverse);
        $scope.filteredList =  $scope.dataList;
    }
};

HTML:

<a ng-click="reverse=!reverse;orderByName('date',reverse)">
<a ng-click="reverse=!reverse;orderByDate('name',reverse)">

以下是我正在使用的代码,但每次点击任何列时,它都会重新对数据进行排序..

1 个答案:

答案 0 :(得分:0)

您可能希望按功能编写自己的订单。你的HTML可能看起来像:

 ng-repeat="employee in employees | orderBy:mySortFunction"

在你的功能中,你可以写一些类似的东西:

 $scope.mySortFunction = function(employee) {
    return employee.name + '~' + employee.date;
 };

在您的情况下,以下代码应该有效:

$scope.filteredList =null;
$scope.orderByName = function(predicate,reverse){
    $scope.sortedOnName = true;
    if($scope.filteredList == null) {
        $scope.dataList = $filter("orderBy")($scope.dataList, predicate, reverse);
        $scope.filteredList =  $scope.dataList;
    }
    else{
    $scope.dataList = $filter("orderBy")($scope.filteredList,predicate,reverse);
    $scope.filteredList =  $scope.dataList;
    }

};

 $scope.mySortFunction = function(dataListElement) {
    return dataListElement.name + '~' + dataListElement.date;
 };

$scope.orderByDate = function(predicate,reverse){
    if ($scope.sortedOnName){
      if($scope.filteredList == null){
          $scope.dataList = $filter("orderBy")($scope.dataList,$scope.mySortFunction,reverse);
          $scope.filteredList =  $scope.companyInfo;
      }
      else{
          $scope.companyInfo = $filter("orderBy")($scope.filteredList,$scope.mySortFunction,reverse);
          $scope.filteredList =  $scope.dataList;
      }
    }
    else {
      if($scope.filteredList == null){
          $scope.dataList = $filter("orderBy")($scope.dataList,predicate,reverse);
          $scope.filteredList =  $scope.companyInfo;
      }
      else{
          $scope.companyInfo = $filter("orderBy")($scope.filteredList,predicate,reverse);
          $scope.filteredList =  $scope.dataList;
      }
    }
};

理论上它应该,但是如果你做了一些改正以使它工作,请告诉我,以便我可以更新答案。