为什么角顺序自定义比较器不能工作
Why is angular orderBy custom comparator not working?
我正在尝试为角orderBy指令实现自定义比较器。正如您在代码片段中看到的那样,尽管定制比较器应该根据orderBy
的angular文档工作,但它被忽略了(console.log中没有记录任何内容)。angular.module('orderByExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.files = [
{name: 'File1', size: '1.2 Mb'},
{name: 'File2', size: '2.4 Kb'},
{name: 'File3', size: '241 Bytes'},
{name: 'File4', size: '2.0 Mb'},
{name: 'File5', size: '16.1 Kb'}
];
$scope.fileSizeComparator = function(s1, s2) {
// split the size string in nummeric and alphabetic parts
console.log(s1);
console.log(s2);
var s1Splitted = s1.size.split(" ");
var s2Splitted = s2.size.split(" ");
if (s1Splitted[1] === s2Splitted[1]) {
// if size type is the same, compare the number
return s1Splitted[0] > s2Splitted[0];
}
// default : compare on size type Mb > Kb > Bytes
return s1Splitted[1] > s2Splitted[1];
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="orderByExample">
<div ng-controller="ExampleController">
<table>
<tr>
<th>Name</th>
<th>Size</th>
</tr>
<tr ng-repeat="file in files | orderBy:'size':false:fileSizeComparator">
<td>{{file.name}}</td>
<td>{{file.size}}</td>
</tr>
</table>
</div>
</div>
我在JsFiddle上测试了angular文档中的示例代码,它也不起作用。什么好主意吗?
我在@morels的帮助下找到了解决方案:是的,我确实应该返回1和-1。但主要问题是比较国被忽略了。显然,这是因为这个特性只对angular 1.5.7及更高版本可用。我还需要对传递给s1和s2的参数的.value
使用.split
。
angular.module('orderByExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.files = [
{name: 'File1', size: '1.2 Mb'},
{name: 'File2', size: '2.4 Kb'},
{name: 'File3', size: '241 Bytes'},
{name: 'File4', size: '2.0 Mb'},
{name: 'File5', size: '16.1 Kb'}
];
$scope.fileSizeComparator = function(s1, s2) {
// split the size string in nummeric and alphabetic parts
var s1Splitted = s1.value.split(" ");
var s2Splitted = s2.value.split(" ");
if (s1Splitted[1] === s2Splitted[1]) {
// if size type is the same, compare the number
return parseFloat(s1Splitted[0]) > parseFloat(s2Splitted[0]) ? -1 : 1;
}
// default : compare on size type Mb > Kb > Bytes
return s1Splitted[1] > s2Splitted[1] ? -1 : 1;
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<div ng-app="orderByExample">
<div ng-controller="ExampleController">
<table>
<tr>
<th>Name</th>
<th>Size</th>
</tr>
<tr ng-repeat="file in files | orderBy:'size':false:fileSizeComparator">
<td>{{file.name}}</td>
<td>{{file.size}}</td>
</tr>
</table>
</div>
</div>
因为您应该返回-1,0,1
而不是true,false
。
在官方文档后面你可以看到一个典型的比较函数的格式:
$scope.localeSensitiveComparator = function(v1, v2) { // If we don't get strings, just compare by index if (v1.type !== 'string' || v2.type !== 'string') { return (v1.index < v2.index) ? -1 : 1; } // Compare strings alphabetically, taking locale into account return v1.value.localeCompare(v2.value); };
请重写为:
$scope.fileSizeComparator = function(s1, s2) {
// split the size string in nummeric and alphabetic parts
console.log(s1);
console.log(s2);
var s1Splitted = s1.size.split(" ");
var s2Splitted = s2.size.split(" ");
if (s1Splitted[1] === s2Splitted[1]) {
// if size type is the same, compare the number
if ( s1Splitted[0] > s2Splitted[0])
return 1;
else
return -1;
}
// default : compare on size type Mb > Kb > Bytes
return s1Splitted[1] > s2Splitted[1] ? -1 : 1;
};
请注意,您永远不会错过相等,即0
。
这样做
ng-repeat="file in files | orderBy: sizeFilter: true"
$scope.sizeFilter=function(file){
$scope.size = file.size;
return $scope.size;
};
相关文章:
- 用javascript将script元素附加到头部;铬不能工作
- 为什么jQuery代码段在没有IFrame的情况下可以工作,而在有IFrame时却不能工作
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 引导程序's的javascript在本地工作,但在部署到服务器时不能工作
- JQuery脚本没有'Don’我不能工作两次
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- ng样式在与的预标记中!重要的是不能工作
- angularjs 控制器在指令中使用控制器作为不能工作
- jQuery LightSlider不能工作,但在JSFiddle中工作得很好
- Javascript - 我如何让一个语句工作一次,如果它已经工作了 1 次,则不能工作,然后再次工作
- 我的查询获胜'因为一个剧本,它不能工作(它以错误的方式工作)!?但为什么呢?SESSIONS和PHP
- 我的Javascript代码没有'铬不能工作
- 使用JQuery调整图像大小和重新排列图像;第一次装载时不能工作
- 为什么不是'这个Javascript计算器不能工作
- Javascript在窗口中返回.onbeforeunload不会'铬不能工作
- Javascript代码包含在html中时可以工作,但单独加载/注入时则不能工作
- 简单的jquery选择器不会;铬不能工作
- 为什么不'这个Javascript函数不能工作
- 为什么不'这个js数组函数不能工作
- 视口比例;方向改变后不能工作