0

我正在使用 angular ng-flow 组件进行文件上传,除了一些细微差别之外,它的效果很好。我希望能够将我尝试上传的新文件放在列表顶部而不是列表底部,这样用户就可以清楚地看到最新上传的文件。我尝试按命令使用角度顺序,但与将新文件添加到列表相比,除非它是现有列表,否则这似乎不起作用。

基本上,一旦完成,我会为每个文件创建一个新行并打印出文件详细信息:

<tr ng-repeat="file in $flow.files | orderBy:'file.name'" ng-hide="file.isComplete()">

上传前列表如下所示:

老1 老2 老3

然后我添加一个新文件,我看到:

旧 1 旧 2 旧 3 新 1

当我想:

新 1 旧 1 旧 2 旧 3

这可能更多是关于使用 Angular 的 ng-repeat 功能来处理添加到列表中的新项目的问题。

4

2 回答 2

0

您可以使用这样的过滤器来实现以相反的顺序打印arrai

app.filter('reverse', function() {
return function(items) {
return items.slice().reverse();
 };
});

然后可以像这样使用:

  <tr ng-repeat="file in $flow.files | reverse" ng-hide="file.isComplete()">
于 2016-07-05T18:48:55.910 回答
0

要使用orderBy过滤器,您只需让过滤器中的属性,如下所示:

<tr ng-repeat="file in $flow.files | orderBy: 'name'" ng-hide="file.isComplete()">

编辑

由于您想按时间顺序对项目进行排序,因此它应该会给您预期的结果:

<tr ng-repeat="file in files | orderBy: '$index': true">

这是一个片段工作:

var app = angular.module('app', []);

app.controller('mainCtrl', function($scope) {
  $scope.files = [  
     {  
        "id":1,
        "name":"Old1"
     },
     {  
        "id":2,
        "name":"Old2"
     },
     {  
        "id":3,
        "name":"Old3"
     }
  ];

  var increment = 1;
  $scope.push = function (name) {
    $scope.files.push({ "id": increment + 3, "name": name + increment++ })
  }
});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>      
</head>

<body ng-controller="mainCtrl">
<p>Before Filter:
<table>
  <tr ng-repeat="file in files">
    <td ng-bind="file.name"></td>
  </tr>
</table>
<hr />
<p>After Filter:
<table>
  <tr ng-repeat="file in files | orderBy: '$index': true">
    <td ng-bind="file.name"></td>
  </tr>
</table>
<hr>
<button type="button" ng-click="push('New')">Add new file</button>
</body>

</html>

于 2016-07-05T18:56:20.007 回答