0

I have a select drop down with the following data; (This is also the output to the console in Chrome).

{
    8: "Something", 
    9: "Something Again!", 
    10: "And again", 
    11: "And again!", 
    12: "etc...", 
    13: "etc etc...", 
}

The html and angular for the select drop down;

<select name="name" ng-model="choosen"> 
   <option value="">Please Select</select>
   <option ng-repeat="(key, value) in data" value="[[key]]">[[value]]</option>
</select>

The data is got from using the following query within Laravel (4.2) using the lists function;

Model::lists('name','id');

For some reason the data for the drop down gets reordered within my drop down to;

<select name="name" ng-model="choosen"> 
   <option value="">Please Select</select>
   <option value="10">And again</option>
   <option value="11">And again!</option>
   <option value="12">etc...</option>
   <option value="13">etc etc...</option>
   <option value="8">Something</option>
   <option value="9">Something Again!</option>
</select>

How do i make the output from the ng-repeat run through the order of the data in the correct numerical order?

Like so;

<select name="name" ng-model="choosen"> 
   <option value="">Please Select</select>
   <option value="8">Something</option>
   <option value="9">Something Again!</option>
   <option value="10">And again</option>
   <option value="11">And again!</option>
   <option value="12">etc...</option>
   <option value="13">etc etc...</option>
</select>

Plunkr is here

6
  • ->orderBy('id', 'asc') ? ngRepeat doesn't reorder your data so the data you pass to the ngRepeat are in that order (sorry for the wordplay) Commented Oct 17, 2014 at 10:46
  • @originof The order of the data is correct from the controller to the angular, the console output is the order of the data. But when put into the ng-repeat the data is moved around. Commented Oct 17, 2014 at 10:49
  • Are you sure ? Try to write a plunkr Commented Oct 17, 2014 at 10:51
  • @originof Im very much positive. Commented Oct 17, 2014 at 11:03
  • 1
    Probably the workaround is plnkr.co/edit/S8BiSPy9axQobSZVwk3D?p=preview Commented Oct 17, 2014 at 11:22

1 Answer 1

1

Seems that ngRepeat order the data by the $$hash, probably the workaround is to use a function that list the keys and then iterate over them:

 $scope.keys = function(obj){
  return obj? Object.keys(obj) : [];
 }

http://plnkr.co/edit/S8BiSPy9axQobSZVwk3D?p=preview

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

1 Comment

You can also create a filter that do the same job

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.