Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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

Description

@kumarharsh

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

Activity

  1. self-assigned this
    on Oct 9, 2014
  2. tbosch commented on Oct 9, 2014

    @tbosch
    Contributor

    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/

  3. kumarharsh commented on Oct 10, 2014

    @kumarharsh
    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.net/97h62/46/

  4. tbosch commented on Oct 10, 2014

    @tbosch
    Contributor

    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.

  5. reopened this on Oct 10, 2014
  6. tbosch commented on Oct 10, 2014

    @tbosch
    Contributor

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

  7. added this to the milestone on Oct 10, 2014
  8. removed their assignment
    on Oct 10, 2014
  9. kumarharsh commented on Oct 11, 2014

    @kumarharsh
    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 :(

  10. petebacondarwin commented on Jan 11, 2015

    @petebacondarwin
    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.

  11. Narretz commented on Sep 13, 2015

    @Narretz
    Contributor

    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions