Angularjs orderBy : how to call default comparator from custom sort function

标签 angularjs sql-order-by comparator

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

$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 函数的后半部分旨在对非数字列进行排序,但没有达到我期望的结果,这只是默认比较器提供的结果。如何获得第二个子句的默认行为?

最佳答案

根据orderBy.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;
}

关于Angularjs orderBy : how to call default comparator from custom sort function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370246/

相关文章:

mysql - 如何获取所选组的最新N条记录

MySQL/MariaDB - 按内部子查询排序

Java 比较器compareToIgnoreCase

java - Android比较特殊字母

javascript - 使用正则表达式检查姓名、10位手机号码和12位号码,使用ng-blur和AngularJS中的匹配功能

javascript - Angular.js linq.js 过滤器无法正常工作的地方

c# - 使用 SUM 和 ORDER BY 的 Linq 查询

java - Collections.sort 是否尊重绑定(bind)输入元素之间的索引? (排序稳定性)

javascript - 我可以在 ncyBreadcrumb 中有一个动态的 parent 吗?

javascript - 为什么 docker-compose 中的 Web 应用程序无法连接到同一组合中的另一个容器?