0

I have map(Bidirectional) of States. Which composed of stateName and stateCode. It is populated by following HTML code but cannot filter by stateCode or stateName in ascending order.

<div class="col-sm-2">
              <select ng-model="location.stateCode"  ng-change="loadDistricts();" ng-options="stateName for (stateCode, stateName) in stateOptions | orderBy:'stateCode'" class="form-control" id="state"></select>
</div>

//Json Object
    stateOptions = {'MH':'Maharashtra','GJ':'Gujarat','MP':'Madhya Pradesh'};
2
  • If the key is stateCode you should have for (stateCode, stateName) instead of for (stateName, stateCode) Commented Oct 10, 2015 at 12:52
  • @Michael - Thanks for comment, i also tried by doing it. In both way it does not populate by ascending order. Commented Oct 10, 2015 at 12:57

3 Answers 3

1

You inverted stateCode and stateName:

ng-options="stateName for (stateCode, stateName) in stateOptions"

Also, as stateOptions is an object, the keys will probably be inserted alphabetically, depending on the js virtual machine. The orderBy filter is here pointless, because (from documentation):

Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers

See fiddle


Edit: If you want to surely control the order, rework your structure as an array:

$scope.stateOptions = [{
    stateCode: 'MH',
    stateName:'Maharashtra'
},{
    stateCode: 'GJ',
    stateName:'Gujarat'
},{
    stateCode: 'MP',
    stateName:'Madhya Pradesh'
}];

ng-options="state.stateName for state in stateOptions | orderBy: 'stateCode'"

See updated fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

Hello @Michael - I edited the question, it does not work in either way.
Hello @Michael- Thank you, but still it is not sorted. What will be option for that, or can i sort it using js and then render it.
You will have to pass an array to ng-options. See edit in answer
Yes ! This will definitely solve the problem. Thanks you so much.
Glad we sorted this out:)
0

Another way

    <select ng-model="location.stateCode" ng-change="loadDistricts();" 

     ng-options="stateName for (stateCode, stateName) in stateOptions | orderBy:'toString()' | filter:stateCode" 

     class="form-control" id="state">

1 Comment

Hello @Aditya- Thank you for posting answer. But there is a problem by using it, it gives error- Filter:notarray.
0

This will be also good option if we do not want to re-structure our object. Fist sort our map object based on keys then pass it to ng-options.

$scope.stateOptions = sortObject($scope.stateOptions);

function sortObject(obj) {
            return Object.keys(obj).sort().reduce(function (result, key) {
            result[key] = obj[key];
            return result;
        }, {});}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.