8000 ngOptions mutilating ordering of values in array · Issue #9465 · angular/angular.js · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngOptions mutilating ordering of values in array #9465

Closed
kumarharsh opened this issue Oct 7, 2014 · 7 comments
Closed

ngOptions mutilating ordering of values in array #9465

kumarharsh opened this issue Oct 7, 2014 · 7 comments

Comments

@kumarharsh
Copy link

If I have an array with 11 or more items, and I write this piece of code:

var years = [
"001",
"002",
...
"011"
];
  <select ng-model="model" ng-options="key as year for (key, year) in years"></select>

Then the order in the options is this:

[
001,
002,
011,
003
...
]

relevant jsfiddle: http://jsfiddle.net/97h62/44/

screen shot 2014-10-07 at 21 03 37

@tbosch tbosch self-assigned this Oct 9, 2014
@tbosch
Copy link
Contributor
tbosch commented Oct 9, 2014

Hi,
this is fixed in the latest 1.2.x (1.2.26) and 1.3.x (1.3.0-rc.5) release, see http://jsfiddle.net/97h62/45/

@tbosch tbosch closed this as completed Oct 9, 2014
@kumarharsh
Copy link
Author

@tbosch It's not fixed.

Do refer to this updated fiddle, and note the position of the string "011" in the select dropdown. Shouldn't it be at the bottom rather than the third position?

http://jsfiddle 8000 .net/97h62/46/

@tbosch
Copy link
Contributor
tbosch commented Oct 10, 2014

Ok, now I know the problem:
If you do (key, value) in years we assume that years is an object. So we get all the entries from it and sort the keys. In your case years is an array, i.e. the keys are '0','1','2','3','4','5',... (as strings, as properties of objects are always strings). As iteration over properties in objects do not have a defined order, angular sorts those keys, and as your example has 11 entries that last entry has the property 10 and is therefore put behind the property 1, which is the second entry.

To fix your problem, don't use (key, value) in years but rather year for year in years, see http://jsfiddle.net/97h62/47/.

So but yes, it is weird that using (key, value) automatically assumes an object datasource. We should actually check the type of the datasource instead of relying on the syntax that has been used.

@tbosch tbosch reopened this Oct 10, 2014
@tbosch
Copy link
Contributor
tbosch commented Oct 10, 2014

Note that this has been there since the beginning of ngOptions, so it is no regression...

@kumarharsh
Copy link
Author

Thanks tbosch. I did get the logic behind the sorting, and it is rather inevitable if angular is naïve enough to treat the array indices as strings. But, in the absence of properly specifying a track by expression along with select as, I feel that there is no way to go other than applying transformers before and after the selection to pass my data.

And no, the year for year in years would not work in my case, as it's actually the index my app really needs :(

@petebacondarwin
Copy link
Contributor

This is fixed by #10639 - see http://jsfiddle.net/8gLagt22/ - ngOptions now doesn't attempt to sort the keys of an object at all.

@Narretz
Copy link
Contributor
Narretz commented Sep 13, 2015

This is fixed in 1.4. If you are still on 1.3.x, you are encouraged to upgrade. Compared to 1.2 -> 1.3 the upgrade is quite painless.:)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants
0