繁体   English   中英

AngularJS orderBy函数没有排序

[英]AngularJS orderBy Function With No Sorting

我试图使用自定义orderBy函数。 最初,我希望数据按照添加到$scope.rows的顺序显示,并且只有在单击列标题后才能按特定属性进行排序。 这是我的小提琴:

http://jsfiddle.net/S8M4c/

这是我的观点:

<table ng-app ng-controller="ctrl">
    <tr>
        <th><a ng-click="orderBy = 'id'">ID</a></th>
        <th><a ng-click="orderBy = 'name'">Name</a></th>
    </tr>
    <tr ng-repeat="row in rows | orderBy:mySort">
        <td>{{row.object.id}}</td>
        <td>{{row.object.name}}</td>
    </tr>
</table>

这是我的控制器:

function ctrl($scope)
{
    // Initially, we don't sort by anything
    $scope.orderBy = "";
    $scope.rows = [];

    // Add some rows
    for(var i = 10;i < 30;i++)
    {
        $scope.rows.push({settings: {foo: true}, object: {id: i, name: "Name " + i}})   
    };

    $scope.mySort = function(row)
    {
        if($scope.orderBy != "")
        {
            return row.object[$scope.orderBy];
        }

        // What do I return here??
        return "";
    }
}

如果$scope.orderBy未设置且我想以原始顺序返回$scope.rows ,那么我在$scope.mySort返回什么? 我无法return row.object.id因为无法保证按行ID顺序添加行。 在Chrome 32上按原样运行我的代码,出现的第一行ID为20,即中间行。

我认为你必须编写自己的sortby函数。 原始angulars orderBy是一个返回排序数组的常规过滤器。 您的过滤器可能如下所示:

.filter('mySort', function(){
    return function(values, param){
        if(param===''){
            return values;
        }else{
             // very important! create a copy of the array - otherwise 
             // the $wtachCollection function will fire to often!
             var arrayCopy = [];
             for ( var i = 0; i < values.length; i++) { arrayCopy.push(values[i]); }

            return arrayCopy.sort(function(a,b){
                var v1 = a.object[param];
                var v2 = b.object[param];
                // you know best how to sort it!
                if (v1 === v2) return 0;
                return v1 < v2 ? -1 : 1;
            });   
        }
    }
})

您可以通过以下方式使用此过滤器:

<table ng-app="myApp" ng-controller="ctrl">
    <tr>
        <th><a ng-click="orderBy = 'id'">ID</a></th>
        <th><a ng-click="orderBy = 'name'">Name</a></th>
    </tr>
    <tr ng-repeat="row in rows | mySort:orderBy">
        <td>{{row.object.id}}</td>
        <td>{{row.object.name}}</td>
    </tr>
</table>

这是你修改过的小提琴: http//jsfiddle.net/spRf6/我已经改变了一些名字,所以你可能会看到排序有效。

创建一个对象数组的副本,然后排序变得微不足道:

控制器:

$scope.objects = [];

angular.forEach($scope.rows, function(row){
    $scope.objects.push(row.object);
});

视图:

<tr ng-repeat="object in objects | orderBy:orderBy">
    <td>{{object.id}}</td>
    <td>{{object.name}}</td>
</tr>

不需要mySort功能。

return $scope.rows.indexOf(row);

小提琴。

您也可以使用开箱即用的orderBy执行此操作,方法是提供一个函数,将其作为默认谓词返回:

控制器:

$scope.mySort = $scope.unsorted = function(row)
    {
        return $scope.rows.indexOf(row);
    }

视图:

<div ng-app ng-controller="ctrl">
    <table>
        <tr>
            <th><a ng-click="mySort = 'object.id'">ID</a></th>
            <th><a ng-click="mySort = 'object.name'">Name</a></th>
        </tr>
        <tr ng-repeat="row in rows | orderBy:mySort">
            <td>{{row.object.id}}</td>
            <td>{{row.object.name}}</td>
        </tr>
    </table>
    <button ng-click="mySort = unsorted;">Original Sort</button>
</div>

在这里小提琴 (我已经更改了对象中使用的数字,以便按ID排序,按名称排序,原始排序不完全相同。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM