简体   繁体   English

AngularJS - 按键、值对重复 ng 时的 orderBy 值

[英]AngularJS - orderBy value when ng-repeat by key, value pair

I have counted the ocurrences of each item in a set of data with countBy function in lodash, and the result is this:我已经用 lodash 中的 countBy 函数计算了一组数据中每个项目的出现次数,结果是这样的:

$scope.colors= {
"Orange": 3,
"Blue": 2,
"Pink": 1,
"Red": 1,
"Black": 2,
};

Now I would like to display the data and order it by its value.现在我想显示数据并按其值排序。 I've tried with我试过

<div ng-controller="myCtrl" ng-app="myApp">
    <ul>
      <li ng-repeat="(key,value) in colors | orderBy:value">
        {{key}} ({{value}})
      </li>
    </ul>
</div>

But the orderBy filter doesn't seem to do the trick (see the plunkr here )但是 orderBy 过滤器似乎并没有起作用(请参阅此处的 plunkr )

Is there any way to do it with the current data form, or is it a better way to structure the data in order to achieve the desired result?有什么办法可以用当前的数据形式来做,还是有更好的方法来构造数据以达到预期的结果? Thanks in advance!提前致谢!

As the documentation says, orderBy expects an array as input, not an object.正如文档所说, orderBy 需要一个数组作为输入,而不是一个对象。 In general, although it's supported, I've never met a use-case where using an object with ng-repeat was a good idea.一般来说,虽然它受支持,但我从未遇到过使用带有 ng-repeat 的对象是个好主意的用例。

Just transform your object into an array:只需将您的对象转换为数组:

$scope.colors = [];
angular.forEach(occurrences, function(value, key) {
    $scope.colors.push({
        color: key,
        count: value
    });
});

and then use然后使用

<li ng-repeat="element in colors | orderBy:'count'">
    {{ element.color }} ({{ element.count }})
</li>

See your updated plunkr .查看您更新的 plunkr

经过多次尝试,我找到了一种迭代对象并按键排序的方法。

<div ng-repeat="key1 in keys(obj1) | orderBy: key1">

I would instead use an array like so:我会改为使用这样的数组:

colors = [{color: 'red', value: 1}, {color: 'blue', value: 2}]

Then you can use然后你可以使用

ng-repeat="color in colors | orderBy:'value'"

Plunkr普朗克

different approach - for不同的方法 - 对于

string from JsonConvert.SerializeObject(DataTable) , so basically List<Dictionary<string, string>>来自JsonConvert.SerializeObject(DataTable) 的字符串,所以基本上List<Dictionary<string, string>>

Example is combination of few solutions (sorry for no references)示例是几个解决方案的组合(抱歉没有参考)
In AngularJs controller:在 AngularJs 控制器中:

vm.propertyName = '\u0022Spaced Column Name\u0022';
vm.reverse = true;

vm.sortBy = function (propertyName) {
                   vm.reverse = (vm.propertyName === '\u0022' + propertyName +'\u0022') ? !vm.reverse : false;
                   vm.propertyName = '\u0022' + propertyName + '\u0022';
    };


vm.Data = JSON.parse(retValue.Data);
                    for (var i = 0; i < vm.Data.length; i++) {
                        var obj = {};
                        angular.forEach(vm.Data[i], function (value, key) {
                            obj[key] = value;

                        });
                        vm.DataArr.push(obj);
                    }

By ng-if="key != 'Id'" I'm hiding Id column with Guid通过ng-if="key != 'Id'"我用 Guid 隐藏了 Id 列
And then in html:然后在 html 中:

<table border="1">
            <tr>
                <td ng-repeat="(key, value) in vm.Data[0]" ng-if="key != 'Id'">
                    <button ng-click="vm.sortBy(key)">{{key}}</button>
                    <span ng-show="vm.propertyName == '\u0022'+key+'\u0022' && !vm.reverse" class="fa fa-arrow-up"></span>
                    <span ng-show="vm.propertyName == '\u0022'+key+'\u0022'  && vm.reverse" class="fa fa-arrow-down"></span>
                </td>
            </tr>
            <tr ng-repeat="row in vm.DataArr |orderBy:vm.propertyName:vm.reverse track by $index" ng-init="rowInd0 = $index">
                <td ng-repeat="(key, value) in vm.Data[0]" ng-if="key != 'Id'">
                     <span>{{row[key]}}</span>
                </td>
            </tr>
        </table>

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

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