AngularJS OrderBy:如何从自定义排序函数调用默认比较器

lyn*_*ker 5 sql-order-by comparator angularjs

我有一个自定义排序功能,可将字符串转换为网格中一列的数字。其他列由字符串或日期值组成。我有一个自定义的排序功能,该功能检查数字列并执行转换,但是对于其他值,我需要默认比较器的行为。我该如何实现?

$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;
  }
};
Run Code Online (Sandbox Code Playgroud)
<tr dir-paginate="agreement in agreements|orderBy:sortKey:reverse:sortSubGrid" ...>
Run Code Online (Sandbox Code Playgroud)

sortSubGrid函数的后半部分旨在对非数字列进行排序,但未达到我期望的结果,这只是默认比较器提供的结果。如何获得第二个子句的默认行为?

JDL*_*JDL 0

根据orderBy.js中的代码,您可以使用默认值或自定义值,但不能同时使用您所描述的两者,但这将是一个很好的功能。

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

所以你总是可以得到原来的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;
}
Run Code Online (Sandbox Code Playgroud)