【问题标题】:AngularJS custom sort filter for file sizeAngularJS自定义文件大小排序过滤器
【发布时间】:2016-11-03 12:57:12
【问题描述】:

我有一列,其中标题是文件大小,数据是字符串格式,例如“1.2 Mb, 2.4 Kb, 241 Bytes...”

我已经使用 orderBy 对其进行了角度排序,但它无法正常工作。 它只是改变了上帝知道什么逻辑的价值观。

我想到的一个解决方案是将字符串转换为一些整数/浮点数,然后根据这些整数/浮点数对列进行排序。

知道如何实现这一目标吗?就像我如何将这些文件大小转换为一些整数并对其应用排序?我将在函数中编写什么代码?

我相信我们必须对字符串进行 Mb、Kb 或字节等检查。

【问题讨论】:

  • 请在您的问题中包含一些相关代码(视图+控制器)
  • 字符串上的 orderBy 函数不会为您提供所需的数字和字母组合排序。您需要为此制作一个自定义比较器。首先比较 Mb、Kb、...,然后比较数字部分。
  • 如果可以的话,我建议以字节为单位。然后在您的 UI 上,您可以使用过滤器来显示它 - 例如将“1024”显示为“1 MB”。 p.s. MB = 兆字节,Mb = 兆位

标签: javascript jquery angularjs frontend


【解决方案1】:

我会在 orderBy 指令中使用自定义比较器。见文档angular orderBy

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>

至少是这个想法,但该指令似乎没有使用比较器。将代码从 Angular 文档复制到 JsFiddle 似乎也不起作用。有人知道这个功能是否被贬低了吗?

更新

修复代码,找出问题所在:需要 angular 1.5.7 并在比较器中返回 1 或 -1 而不是 true/false

【讨论】:

  • 我的代码也有点不同: {{files.name}} {{files.size |文件大小}}
  • 我已经修复了我的代码,现在它应该可以工作了。如果您希望我帮助您提供特定代码,您需要发布它。
【解决方案2】:

如果你的行中有十进制数据,你可以使用简单的 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);

查看Fiddle

【讨论】:

  • 好吧,这到底是如何工作的?...如何区分“33.1”和“32.1”....因为两者都是字符串...?
  • 我看到你做了类似的事情:{{person.age | number}}...这是转换成数字吗?
  • 但我的代码有点不同: {{files.name}} {{files.size |文件大小}}
猜你喜欢
  • 2017-10-06
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多