Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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函数的后半部分用于对非数字列进行排序,但未达到我期望的结果,这只是认比较器提供的结果.如何获取第二个子句的认行为?

解决方法

根据 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:如何从自定义排序函数调用默认比较器全部内容,希望文章能够帮你解决Angularjs orderBy:如何从自定义排序函数调用默认比较器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。