2016-09-19 6 views
1

변수 부터 ngRepeat로 만든 테이블이 있습니다.입니다. 필터를 사용하면 대신 first.id_second 번으로 second.name을 기록하며 훌륭하게 작동합니다. 이제 열 first.id_second을 정렬하려고하고 있습니다. 그리고 그것을 first.id_second으로 정렬하지 않고 second.name별로 정렬하려고합니다. I는 속성을 기준으로 필터링하는 방법을 모른다AngularJS 필터 orderBy, 하나씩 다른 변수로 정렬

var first = [{ 
    "id": 0, 
    "id_second": "111" 
},{ 
    "id": 1, 
    "id_second": "222" 
}] 

var second = [{ 
    "id_second": "111", 
    "name": "name1" 
},{ 
    "id_second": "222", 
    "name": "name2" 
}] 

일반적으로, 내 HTML에서 내가

ng-click="order('col_name')" 

그리고 컨트롤러

$scope.order = function(predicate) { 
    $scope.predicate = predicate; 
    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
    $scope.first = orderBy($scope.first, predicate, $scope.reverse); 
}; 

var orderBy = $filter('orderBy'); 

에있을 것입니다 : 이것은 내 구조 다른 변수에서. 나는 사용자 지정 필터를 작성해야한다고 생각하지만 지금까지는 성공적이지 못했습니다.

아이디어가 있으십니까? 고맙습니다.

+0

그냥 제안 : 환영 할 것 각진'$ filter' 서비스 대신 [_.sortBy] (https://lodash.com/docs/3.10.1#sortBy)를 사용하여 lo-dash를 사용하지 마십시오. – MiTa

+0

필자는 필자가 필요로하는 "작은"것에 대해 추가 라이브러리를 사용하는 것을 좋아하지 않는다. 특히이 프로젝트가 이미 가지고있는 많은 코드를 다시 작성한다는 것을 의미한다면. – ljerka

답변

0

이 간단한 작업을 수행 할 방법이없는 것 같습니다. 그래서 결국에는 quicksort를 구현 한 custom orderBy 함수를 작성했습니다.

// arranging data from second to get to the data easier 
var secondKeyVal = {}; 
for (var i = 0; i < $scope.second.length; i++) { 
    secondKeyVal[$scope.second[i].id_second] = $scope.second[i].naziv; 
} 

// function that is called from html 
$scope.orderByCustom = function(predicate) { 
    $scope.predicate = predicate; 
    $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
    $scope.orderByCustomSort(); 
}; 

$scope.orderByCustomSort= function() { 
    var sorted = quickSort($scope.first); 
    $scope.first = sorted; 
}; 

function quickSort(a) { 
    if (a.length <= 1) return a; 
    var left=[], right=[], pivot=secondKeyVal[a[0].id_second]; 
    for (var i = 1; i < a.length; i++) { 
     secondKeyVal[a[i].id_second] <= pivot ? left.push(a[i]) : right.push(a[i]); 
    } 

    return quickSort(left).concat(a[0], quickSort(right)); 
} 
사람이 추가 라이브러리를 사용하지 않고이 밖으로 당겨 관리하는 경우 당연히이, 제가 이미 사용중인 HTML 필터와 해 orderBy을 변경해야 할 것

, 대부분, 왜

관련 문제