在Angular中有多个字段

时间:2013-06-11 06:07:36

标签: javascript angularjs sorting angularjs-ng-repeat angularjs-orderby

如何在角度中同时使用多个字段进行排序?按组分组,然后按分组 例如

$scope.divisions = [{'group':1,'sub':1}, {'group':2,'sub':10}, {'group':1,'sub':2},{'group':1,'sub':20},{'group':2,'sub':1},
    {'group':2,'sub':11}];

我想将其显示为

组:子组

1 - 1

1 - 2

1 - 20

2 - 1

2 - 10

2 - 11

<select ng-model="divs" ng-options="(d.group+' - '+d.sub) for d in divisions | orderBy:'group' | orderBy:'sub'" />

7 个答案:

答案 0 :(得分:629)

请看这个:

http://jsfiddle.net/JSWorld/Hp4W7/32/

<div ng-repeat="division in divisions | orderBy:['group','sub']">{{division.group}}-{{division.sub}}</div>

答案 1 :(得分:46)

如果要对控制器内的多个字段进行排序,请使用此

$filter('orderBy')($scope.property_list, ['firstProp', 'secondProp']);

另见https://docs.angularjs.org/api/ng/filter/orderBy

答案 2 :(得分:21)

<select ng-model="divs" ng-options="(d.group+' - '+d.sub) for d in divisions | orderBy:['group','sub']" />

用户数组而不是多个orderBY

答案 3 :(得分:5)

排序可以通过使用&#39; orderBy&#39;来完成。以角度过滤。

两种方式:    1.从视图    2.来自控制器

  1. 从视图
  2. 语法:

    {{array | orderBy : expression : reverse}} 
    

    例如:

     <div ng-repeat="user in users | orderBy : ['name', 'age'] : true">{{user.name}}</div>
    
    1. 来自控制器
    2. 语法:

      $filter.orderBy(array, expression, reverse);
      

      例如:

      $scope.filteredArray = $filter.orderBy($scope.users, ['name', 'age'], true);
      

答案 4 :(得分:0)

我写了这个方便的部分来按对象的多个列/属性进行排序。每次连续列单击时,代码会存储单击的最后一列,并将其添加到不断增加的单击列字符串名称列表中,并将它们放在名为sortArray的数组中。内置的Angular&#34; orderBy&#34; filter只是读取sortArray列表,并按存储在那里的列名的顺序对列进行排序。因此,最后一次单击的列名称将成为主要有序过滤器,前一个名称将单击下一个优先级,等等。相反的顺序会立即影响所有列的顺序,并为完整的数组列表集切换升序/降序:

<script>
    app.controller('myCtrl', function ($scope) {
        $scope.sortArray = ['name'];
        $scope.sortReverse1 = false;
        $scope.searchProperty1 = '';
        $scope.addSort = function (x) {
            if ($scope.sortArray.indexOf(x) === -1) {
                $scope.sortArray.splice(0,0,x);//add to front
            }
            else {
                $scope.sortArray.splice($scope.sortArray.indexOf(x), 1, x);//remove
                $scope.sortArray.splice(0, 0, x);//add to front again
            }
        };
        $scope.sushi = [
        { name: 'Cali Roll', fish: 'Crab', tastiness: 2 },
        { name: 'Philly', fish: 'Tuna', tastiness: 2 },
        { name: 'Tiger', fish: 'Eel', tastiness: 7 },
        { name: 'Rainbow', fish: 'Variety', tastiness: 6 },
        { name: 'Salmon', fish: 'Misc', tastiness: 2 }
        ];
    });
</script>
<table style="border: 2px solid #000;">
<thead>
    <tr>
        <td><a href="#" ng-click="addSort('name');sortReverse1=!sortReverse1">NAME<span ng-show="sortReverse1==false">&#9660;</span><span ng-show="sortReverse1==true">&#9650;</span></a></td>
        <td><a href="#" ng-click="addSort('fish');sortReverse1=!sortReverse1">FISH<span ng-show="sortReverse1==false">&#9660;</span><span ng-show="sortReverse1==true">&#9650;</span></a></td>
        <td><a href="#" ng-click="addSort('tastiness');sortReverse1=!sortReverse1">TASTINESS<span ng-show="sortReverse1==false">&#9660;</span><span ng-show="sortReverse1==true">&#9650;</span></a></td>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="s in sushi | orderBy:sortArray:sortReverse1 | filter:searchProperty1">
        <td>{{ s.name }}</td>
        <td>{{ s.fish }}</td>
        <td>{{ s.tastiness }}</td>
    </tr>
</tbody>
</table>

答案 5 :(得分:0)

创建用于排序的管道。接受字符串和字符串数组,并按多个值排序。适用于Angular(不是AngularJS)。同时支持字符串和数字排序。

@Pipe({name: 'orderBy'})
export class OrderBy implements PipeTransform {
    transform(array: any[], filter: any): any[] {
        if(typeof filter === 'string') {
            return this.sortAray(array, filter)
        } else {
            for (var i = filter.length -1; i >= 0; i--) {
                array = this.sortAray(array, filter[i]);
            }

            return array;
        }
    }

    private sortAray(array, field) {
        return array.sort((a, b) => {
            if(typeof a[field] !== 'string') {
                a[field] !== b[field] ? a[field] < b[field] ? -1 : 1 : 0
            } else {
                a[field].toLowerCase() !== b[field].toLowerCase() ? a[field].toLowerCase() < b[field].toLowerCase() ? -1 : 1 : 0
            }
        });
    }
}

答案 6 :(得分:-7)

确保最终用户的排序不复杂。我一直认为对组和子组进行排序有点复杂。如果它是技术最终用户,那可能没问题。