简体   繁体   中英

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:

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

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. In general, although it's supported, I've never met a use-case where using an object with ng-repeat was a good idea.

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 .

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

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

Example is combination of few solutions (sorry for no references)
In AngularJs controller:

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
And then in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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