2016-11-01 41 views
3

我有一個自定義的排序函數來將字符串轉換爲網格中一列的數字。其他列由字符串或日期值組成。我有一個自定義的排序函數,它檢查數字列並執行轉換,但對於其他值,我想要默認比較器的行爲。我如何實現這一目標?Angularjs orderBy:如何從自定義排序函數調用默認比較器

$scope.sort = function (keyname) { 
 
    $scope.sortKey = keyname; 
 
    $scope.reverse = !$scope.reverse; 
 
}; 
 

 

 
$scope.sortSubGrid = function (a, b) { 
 
    if ($scope.sortKey === 'caseNumber') { 
 
    return = parseInt(b.value) -parseInt(a.value); 
 
    } 
 
    else { 
 
    return = a.value > b.value; 
 
    } 
 
};
<tr dir-paginate="agreement in agreements|orderBy:sortKey:reverse:sortSubGrid" ...>

的sortSubGrid功能下半年意在非數字列進行排序,但沒有取得成果我希望,這僅僅是默認的比較器提供的結果。如何獲得第二個子句的默認行爲?

回答

0

根據ordeBy.js中的代碼,您可以使用默認或自定義,但不像您所描述的那樣,這將是一個不錯的功能。

// Define the `compare()` function. Use a default comparator if none is specified. 
var compare = isFunction(compareFn) ? compareFn : defaultCompare; 

因此,您可以隨時獲取原始defaultCompare代碼並對其進行修改。

// source orderBy.js 
function defaultCompare(v1, v2) { 
    var result = 0; 
    var type1 = v1.type; 
    var type2 = v2.type; 

    if (type1 === type2) { 
    var value1 = v1.value; 
    var value2 = v2.value; 

    if (type1 === 'string') { 
     // Compare strings case-insensitively 
     value1 = value1.toLowerCase(); 
     value2 = value2.toLowerCase(); 
    } else if (type1 === 'object') { 
     // For basic objects, use the position of the object 
     // in the collection instead of the value 
     if (isObject(value1)) value1 = v1.index; 
     if (isObject(value2)) value2 = v2.index; 
    } 

    if (value1 !== value2) { 
     result = value1 < value2 ? -1 : 1; 
    } 
    } else { 
    result = type1 < type2 ? -1 : 1; 
    } 

    return result; 
} 
相關問題