Skip to content Skip to sidebar Skip to footer

Angular JS Order Select Using Key/value

I just asked about generating a select from key/value map instead of array: AngularJS select box generated from object That is all working fine now: http://jsfiddle.net/UPWKe/1/ &l

Solution 1:

Solution 1: You can use another array to store the order of the fields. For this you would need to use ng-repeat in place of ng-options:

$scope.studentAddressFields = [
    "select",
    "letter",
    "photograph"
]

HTML:

<select ng-model="current.addressCode">
    <option ng-repeat="field in studentAddressFields" 
    value="student.address[field]['code']">
        {{student.address[field]['name']}}
    </option>
</select>

Solution 2: Using a filter:

HTML:

<select ng-model="current.addressCode" ng-options="code as details.name 
for (code, details) in student.address | getOrdered">
</select>

Filter:

myApp.filter('getOrdered', function() {
    return function(input) {
        var ordered = {};
        for (var key in input){            
            ordered[input[key]["code"]] = input[key];
        }           
        return ordered;
    };
});

Post a Comment for "Angular JS Order Select Using Key/value"