简体   繁体   English

AngularJS OrderBy具有自定义功能

[英]AngularJS OrderBy with custom function

I'm new to AngularJS, but so far I've been able to wrap my head around everything. 我是AngularJS的新手,但到目前为止,我已经能够绕过一切。 But the OrderBy is causing me issues. 但OrderBy给我带来了问题。 And I haven't found any issues like mine yet. 我还没有找到像我这样的问题。 I have a feeling it's because I'm missing something about the $scope and how orderBy actually works. 我有一种感觉,因为我错过了关于$ scope以及orderBy实际工作方式的一些信息。

I'm creating a list that will display writers in my region for this years NaNoWriMo. 我正在创建一个列表,在今年NaNoWriMo中显示我所在地区的作家。 I've separated the users as Factories, and have gotten them displayed. 我已将用户分隔为工厂,并将其显示出来。 But, I'm having issues getting them sorted. 但是,我遇到了让它们排序的问题。 The Name and Wordcount sort without a problem. 名称和Wordcount排序没有问题。 But the Calculated Average Wordcount is not sorting at all. 但计算平均字数完全没有排序。 It won't call the function I've made for it, even. 它甚至不会调用我为它制作的功能。

Here's my simplified layout, and the JSFiddle setup (updated) . 这是我的简化布局和JSFiddle设置(更新)

JS: JS:

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

app.controller('ListController', ['$scope',

function ($scope) {
    $scope.users = userData;
    $scope.day = 30;

    $scope.avgWords = function (user) {
        return Math.floor(user.wordcount / $scope.day);
    };

    $scope.sort = "name";
    $scope.sortRev = false;

    $scope.sortChange = function (field) {
        if ($scope.sort === field) {
            $scope.sortRev = !$scope.sortRev;
            return;
        }

        $scope.sort = field;
        $scope.sortRev = false;
    };

    $scope.sortAvg = function (user) {
        alert("sorted!");
        return Math.floor(user.wordcount / $scope.day);
    };
}]);

var userData = [{
    "name": "Kris",
        "wordcount": 42382
}, {
    "name": "Tim",
        "wordcount": 60120
}, {
    "name": "Elsa",
        "wordcount": 150675
}];
})();

HTML: HTML:

<div ng-controller="ListController">
<table>
    <thead>
        <tr>
            <th ng-click="sortChange('name');">Username</th>
            <th ng-click="sortChange('wordcount');">Total Words</th>
            <th ng-click="sortChange('sortAvg');">Average WPD</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="user in users | orderBy:sort:sortRev">
            <td>{{user.name}}</td>
            <td>{{user.wordcount}}</td>
            <td>{{avgWords(user)}}</td>
        </tr>
    </tbody>
</table>

You can always just add the average word property to the objects in the user array on the scope. 您始终只需将平均单词属性添加到作用域上用户数组中的对象即可。 That way each column can be filtered by the appropriate property...so something like 这样每个列都可以通过适当的属性进行过滤......所以就像这样

http://jsfiddle.net/poppypoop/swer05sx/ http://jsfiddle.net/poppypoop/swer05sx/

    $scope.users = userData;
    $scope.day = 30;

      for(var i=0; i < $scope.users.length; i++){
        $scope.users[i].avgWords = Math.floor($scope.users[i].wordcount / $scope.day);
    }

When you click on the Average WPD heading, you set $scope.sort to the string "avgWords". 单击Average WPD标题时,将$ scope.sort设置为字符串 “avgWords”。 So, orderBy uses this string to sort the users, and thus orders the users by the value of their avgWords field, which is always undefined . 因此,orderBy使用此字符串对用户进行排序,从而通过其avgWords字段的值对用户进行排序,该字段始终undefined

If you want to sort using a custom function rather than a field, you must set $scope.sort to the function you want to sort by: 如果要使用自定义函数而不是字段进行排序,则必须将$scope.sort设置$scope.sort要排序的函数:

$scope.sort = $scope.avgWords;

To do that, the ng-click on the heading should be 要做到这一点,标题上的ng-click应该是

sortChange(avgWords)

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

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