Angularjs orderBy:如何从自定义排序函数调用默认比较器
发布时间:2020-12-17 17:01:02 所属栏目:安全 来源:网络整理
导读:我有一个自定义排序函数,用于将字符串转换为网格中一列的数字.其他列由字符串或日期值组成.我有一个自定义排序函数,它检查数字列并执行转换,但对于其他值,我想要默认比较器的行为.我该如何实现这一目标? $scope.sort = function (keyname) { $scope.sortKey
我有一个自定义排序函数,用于将字符串转换为网格中一列的数字.其他列由字符串或日期值组成.我有一个自定义排序函数,它检查数字列并执行转换,但对于其他值,我想要默认比较器的行为.我该如何实现这一目标?
$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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |