简体   繁体   English

AngularJS orderBy函数没有排序

[英]AngularJS orderBy Function With No Sorting

I am attempting to use a custom orderBy function. 我试图使用自定义orderBy函数。 Initially, I want the data to appear in the order it was added to $scope.rows , and only after clicking on a column heading should it order by a specific property. 最初,我希望数据按照添加到$scope.rows的顺序显示,并且只有在单击列标题后才能按特定属性进行排序。 Here's my fiddle: 这是我的小提琴:

http://jsfiddle.net/S8M4c/ http://jsfiddle.net/S8M4c/

Here's my view: 这是我的观点:

<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>

Here's my controller: 这是我的控制器:

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 "";
    }
}

In the case that $scope.orderBy isn't set and I want to return $scope.rows in it's original order, what do I return in $scope.mySort ? 如果$scope.orderBy未设置且我想以原始顺序返回$scope.rows ,那么我在$scope.mySort返回什么? I cannot return row.object.id because the rows are not guaranteed to be added in order of their ID. 我无法return row.object.id因为无法保证按行ID顺序添加行。 Running my code as is on Chrome 32, the first row that appears has an ID of 20, which is the halfway row. 在Chrome 32上按原样运行我的代码,出现的第一行ID为20,即中间行。

I think you have to write your own sortby function. 我认为你必须编写自己的sortby函数。 The original angulars orderBy is a regular filter that returns the sorted array. 原始angulars orderBy是一个返回排序数组的常规过滤器。 Your filter may look something like this: 您的过滤器可能如下所示:

.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;
            });   
        }
    }
})

You can use this filter in this way: 您可以通过以下方式使用此过滤器:

<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>

here is your modified fiddle: http://jsfiddle.net/spRf6/ I have changed the names a little bit so you may see that the sorting works. 这是你修改过的小提琴: http//jsfiddle.net/spRf6/我已经改变了一些名字,所以你可能会看到排序有效。

Create a copy of the objects array and the ordering then becomes trivial: 创建一个对象数组的副本,然后排序变得微不足道:

Controller: 控制器:

$scope.objects = [];

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

View: 视图:

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

No need for the mySort function. 不需要mySort功能。

return $scope.rows.indexOf(row);

( Fiddle. ) 小提琴。

You can also do this with out-of-the-box orderBy by providing a function returning that as the default predicate: 您也可以使用开箱即用的orderBy执行此操作,方法是提供一个函数,将其作为默认谓词返回:

Controller: 控制器:

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

View: 视图:

<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>

Fiddle here. 在这里小提琴 (I've changed the numbers used in the objects so that sort by id, sort by name, and the original sort aren't all the same.) (我已经更改了对象中使用的数字,以便按ID排序,按名称排序,原始排序不完全相同。)

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

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