Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort an angular ng-repeat list based on nested value

How do I sort my ng-repeat list from the nested value of the snippet attribute skärm: My controller looks like this:

'use strict';

/* Controllers */

function PhoneListCtrl($scope){
    $scope.phones = [

        {"name" : "Samsung Galaxy S4",
         "snippet" : {  "Operativsystem" : "Android 4.2.2",
                            "Skärm" : "4,99 tum",
                            "CPU" : "Quad-core 1,6GHz",
                            "Kamera, bak" : "13 MP",
                            "Kamera, fram" : "1,9 MP",
                            "Övrigt" : "Närfältskommunikation (Eng. near field communication, NFC)"
                            },
            "date" : "2012-04-27T00:00:00.000Z"
        },


        {"name" : "iPhone 5",
          "snippet" : { "Operativsystem" : "iOS 7",
                            "Skärm" : "4 tum",
                            "CPU" : "Apple A6 1,3GHz dual core",
                            "Kamera, bak" : "8 MP",
                            "Kamera, fram" : "1,2 MP",
                            "Övrigt" : "-"
                           },
            "date" : "2012-09-21T00:00:00.000Z"
        },

                {"name" : "iPhone 5s",
          "snippet" : { "Operativsystem" : "iOS 7",
                            "Skärm" : "42 tum",
                            "CPU" : "Apple A7 1,3 GHz dual core",
                            "Kamera, bak" : "8 MP",
                            "Kamera, fram" : "1,2 MP",
                            "Övrigt" : "-"
                           },
            "date" : "2013-09-20T00:00:00.000Z"
        }
    ]


};

The alphabetic and date based sortings work but how do i use the nested value of Skärm as the sorting parameter?

        <b>Sort by:  </b> 
        <select ng-model="sortByProp">
            <option value="name">Alfabetiskt</option>
            <option value="date">Nyast</option>
            <option value="snippet.Skärm">Skärmstorlek</option>
        </select>

    </div>
    <div class="span10">
        <h1>The future of mobile devices</h1>

        <ul class="phonesListing">
        <li ng-repeat="phone in phones | filter:query | orderBy:sortByProp">
            <h3>{{phone.name}}</h3>
            <ul>
                <li ng-repeat="(key,value) in phone.snippet">
                    <b>{{key}}:</b>     {{value}}
                </li>
            </ul>
        </li>
        </ul>
        <p>Total number of phones: {{phones.length}}</p>
    </div>
like image 899
David Karlsson Avatar asked Nov 02 '22 14:11

David Karlsson


1 Answers

If you want to sort by Skärm property, you'll have to define sorting expression like this:

 <option value="snippet['Skärm']">Skärmstorlek</option>

Plunker

Looks like angular have some problems with non-ASCII symbols used in expressions.

like image 132
jusio Avatar answered Nov 15 '22 03:11

jusio