Javascript AngularJS文件大小自定义排序过滤器
我有一个列,标题是文件大小,数据是字符串格式的,就像“1.2MB,2.4KB,241字节…” 我使用orderBy对其应用了角度排序,但它不能正常工作。 它只是在上帝知道的逻辑上改变价值观 我想到的一个解决方案是将字符串转换成一些整数/浮点数,然后根据这些整数/浮点数对列进行排序 你知道我怎样才能做到这一点吗?比如,我如何将这些文件大小转换成一些整数并对其进行排序?我将在函数中编写什么代码Javascript AngularJS文件大小自定义排序过滤器,javascript,jquery,angularjs,frontend,Javascript,Jquery,Angularjs,Frontend,我有一个列,标题是文件大小,数据是字符串格式的,就像“1.2MB,2.4KB,241字节…” 我使用orderBy对其应用了角度排序,但它不能正常工作。 它只是在上帝知道的逻辑上改变价值观 我想到的一个解决方案是将字符串转换成一些整数/浮点数,然后根据这些整数/浮点数对列进行排序 你知道我怎样才能做到这一点吗?比如,我如何将这些文件大小转换成一些整数并对其进行排序?我将在函数中编写什么代码 我相信我们必须对字符串进行Mb、Kb或字节等检查。如果行中有十进制数据,可以使用简单的orderBy 我为
我相信我们必须对字符串进行Mb、Kb或字节等检查。如果行中有十进制数据,可以使用简单的orderBy 我为你们树立了榜样
var controllers = {};
var app = angular.module('UM',[]);
//routing
app.config(function($routeProvider){
$routeProvider
.when('/',{
template: '<a href="#" class="btn">home</a><input ng-model="sorter" /><table class="table table-striped"> <thead> <th > <a href="" ng-click="sortList(\'id\')">ID</a> </th> <th> <a href="" ng-click="sortList(\'firstname\')">First Name</a> </th> <th> <a href="" ng-click="sortList(\'lastname\')">Surname</a> </th> <th> <a href="" ng-click="sortList(\'age\')">Age</a> </th> <th> <a href="" ng-click="sortList(\'cars\')">cars</a> </th> </thead> <tbody> <tr ng-repeat="person in people | orderBy:sorter "> <td>{{person.id | number}}</td> <td>{{person.firstname}} </td> <td>{{person.lastname}} </td> <td>{{person.age | number}}</td> <td>{{person.cars}} </td> </tr> </tbody></table>',
controller: 'listCtrl'
}).when('/test',{
template: '<h6>{{people}}</h6>',
controller: 'listCtrl'
});
});
//user data
app.service('People', function() {
var People = {};
People.details = [{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"33.1","cars":"Yaris"},
{"firstname":"Jason","lastname":"Diry","age":"32.1","id":"5"},
{"id":"6","firstname":"Bilson","lastname":"Berby","age":"33.3","cars":"Tipo"}]
return People;
});
//list ctrl
controllers.listCtrl = function ($scope,People) {
$scope.people = People.details;
$scope.sortList = function(sortname) {
$scope.sorter = sortname;
}
}
//controllers
app.controller(controllers);
var控制器={};
var app=angular.module('UM',[]);
//路由
app.config(函数($routeProvider){
$routeProvider
。当(“/”{
模板:{{person.id | number}{{{person.firstname}{{person.lastname}{{person.age | number}{{{person.cars}},
控制器:“listCtrl”
})。当(“/test”{
模板:“{{people}}”,
控制器:“listCtrl”
});
});
//用户数据
应用程序服务('人',功能(){
var-People={};
People.details=[{“id”:“42”,“firstname”:“Sarah”,“lastname”:“Dilby”,“age”:“33.1”,“cars”:“Yaris”},
{“firstname”:“Jason”,“lastname”:“Diry”,“age”:“32.1”,“id”:“5”},
{“id”:“6”,“firstname”:“Bilson”,“lastname”:“Berby”,“age”:“33.3”,“cars”:“Tipo”}]
还人,;
});
//列表控制
controllers.listCtrl=函数($scope,People){
$scope.people=people.details;
$scope.sortList=函数(sortname){
$scope.sorter=sortname;
}
}
//控制器
应用控制器(控制器);
检查如果行中有十进制数据,则可以使用simple orderBy 我为你们树立了榜样
var controllers = {};
var app = angular.module('UM',[]);
//routing
app.config(function($routeProvider){
$routeProvider
.when('/',{
template: '<a href="#" class="btn">home</a><input ng-model="sorter" /><table class="table table-striped"> <thead> <th > <a href="" ng-click="sortList(\'id\')">ID</a> </th> <th> <a href="" ng-click="sortList(\'firstname\')">First Name</a> </th> <th> <a href="" ng-click="sortList(\'lastname\')">Surname</a> </th> <th> <a href="" ng-click="sortList(\'age\')">Age</a> </th> <th> <a href="" ng-click="sortList(\'cars\')">cars</a> </th> </thead> <tbody> <tr ng-repeat="person in people | orderBy:sorter "> <td>{{person.id | number}}</td> <td>{{person.firstname}} </td> <td>{{person.lastname}} </td> <td>{{person.age | number}}</td> <td>{{person.cars}} </td> </tr> </tbody></table>',
controller: 'listCtrl'
}).when('/test',{
template: '<h6>{{people}}</h6>',
controller: 'listCtrl'
});
});
//user data
app.service('People', function() {
var People = {};
People.details = [{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"33.1","cars":"Yaris"},
{"firstname":"Jason","lastname":"Diry","age":"32.1","id":"5"},
{"id":"6","firstname":"Bilson","lastname":"Berby","age":"33.3","cars":"Tipo"}]
return People;
});
//list ctrl
controllers.listCtrl = function ($scope,People) {
$scope.people = People.details;
$scope.sortList = function(sortname) {
$scope.sorter = sortname;
}
}
//controllers
app.controller(controllers);
var控制器={};
var app=angular.module('UM',[]);
//路由
app.config(函数($routeProvider){
$routeProvider
。当(“/”{
模板:{{person.id | number}{{{person.firstname}{{person.lastname}{{person.age | number}{{{person.cars}},
控制器:“listCtrl”
})。当(“/test”{
模板:“{{people}}”,
控制器:“listCtrl”
});
});
//用户数据
应用程序服务('人',功能(){
var-People={};
People.details=[{“id”:“42”,“firstname”:“Sarah”,“lastname”:“Dilby”,“age”:“33.1”,“cars”:“Yaris”},
{“firstname”:“Jason”,“lastname”:“Diry”,“age”:“32.1”,“id”:“5”},
{“id”:“6”,“firstname”:“Bilson”,“lastname”:“Berby”,“age”:“33.3”,“cars”:“Tipo”}]
还人,;
});
//列表控制
controllers.listCtrl=函数($scope,People){
$scope.people=people.details;
$scope.sortList=函数(sortname){
$scope.sorter=sortname;
}
}
//控制器
应用控制器(控制器);
检查我将在orderBy指令中使用自定义比较器。见文件
angular.module('orderByExample',[])
.controller('ExampleController',['$scope',function$scope){
$scope.files=[
{name:'File1',size:'1.2MB'},
{name:'File2',size:'2.4KB'},
{name:'File3',size:'241字节'},
{name:'File4',size:'2.0MB'},
{name:'File5',size:'16.1KB'}
];
$scope.fileSizeComparator=函数(s1、s2){
//将大小字符串拆分为数字和字母部分
变量s1Splitted=s1.value.split(“”);
变量s2Splitted=s2.value.split(“”);
如果(s1已拆分[1]==s2已拆分[1]){
//如果大小类型相同,请比较数字
返回parseFloat(s1Splitted[0])>parseFloat(s2Splitted[0])?-1:1;
}
//默认值:比较大小类型Mb>Kb>Bytes
返回s1Splitted[1]>s2 splitted[1]?-1:1;
};
}]);代码>
名称
大小
{{file.name}
{{file.size}
我会在orderBy指令中使用自定义比较器。见文件
angular.module('orderByExample',[])
.controller('ExampleController',['$scope',function$scope){
$scope.files=[
{name:'File1',size:'1.2MB'},
{name:'File2',size:'2.4KB'},
{name:'File3',size:'241字节'},
{name:'File4',size:'2.0MB'},
{name:'File5',size:'16.1KB'}
];
$scope.fileSizeComparator=函数(s1、s2){
//将大小字符串拆分为数字和字母部分
变量s1Splitted=s1.value.split(“”);
变量s2Splitted=s2.value.split(“”);
如果(s1已拆分[1]==s2已拆分[1]){
//如果大小类型相同,请比较数字
返回parseFloat(s1Splitted[0])>parseFloat(s2Splitted[0])?-1:1;
}
//默认值:比较大小类型Mb>Kb>Bytes
返回s1Splitted[1]>s2 splitted[1]?-1:1;
};
}]);代码>
名称
大小
{{file.name}
{{file.size}
请包括一些相关信息