Angular JS order select using key/value

前端 未结 1 800
遥遥无期
遥遥无期 2021-01-15 19:27

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.

相关标签:
1条回答
  • 2021-01-15 20:06

    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;
        };
    });
    
    0 讨论(0)
提交回复
热议问题