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

feat(select,ng-options): New expression (identify by) in ng-options #2040

Closed
wants to merge 2 commits into from
Closed

feat(select,ng-options): New expression (identify by) in ng-options #2040

wants to merge 2 commits into from

Conversation

quazzie
Copy link
Contributor

@quazzie quazzie commented Feb 20, 2013

Add a new expression (identify by) in ng-options.
This can be used when working with objects. Instead of objects comparing by reference
it will compare by ex. a property.

So you can write:

ng-options="obj identify by obj.id as obj.name for obj in objects"

The identify part will be used when checking for equality of objects.

Example:
If you get an json object from server with nested objects and a collection of those nested
objects you can use this to select correct object.

<select
    ng-model="user.building"
    ng-options="building identify by building.id as building.name 
                      for building in buildings"
    >
</select>

//scope.user comes as json from server
//scope.buildings comes as jsonarray from server
scope: {
  user: { 
      name: 'Test user', 
      building: { id: 1, name: 'Office 1', address: 'Bla bla 12', rooms: 4 } 
  }
  buildings: [
    { id: 1, name: 'Office 1', address: 'Bla bla 12', rooms: 4 }, 
    { id: 2, name: 'Office 2', address: 'Bla bla 13', rooms: 5 },
    ... 
  ]
}

This will correctly select building with id 1 in the select and when select is changed
it will populate the user with the building object selected.

@icenine457
Copy link

This would be a godsend for my application, as I'm using Mongo and having to do an additional look-up based on an identifier seems sloppy.

FYI: Knockout has similar capabilities by setting optionText and optionValue bindings. Works like a charm.

PS: I was impatient, and so I cloned your branch and am using this now (not in a production environment, but I'm really hopeful this makes it in!)

Adds a new expression (identify by) in ng-options.
This can be used when working with objects.

You can now write:
ng-options="obj identify by obj.id as obj.name for obj in objects"

The identify part will be used when checking for equality of objects.

Examples:
<select
    ng-model="user.favMovieStub"
    ng-options="movie identify by movie.id as movie.name for movie in movies">
</select>

scope: {
  user: { name: 'Test user', favMovieStub: { id: 1, name: 'Starwars' } }
  movies: [{ id: 1, name: 'Starwars', rating: 5, ... }, { id: 13, ... }]
}

Will match user favMovieStub to the first movie in the movies array.
@pavelarnost
Copy link

This patch (or some alternative of it) should definitely be accepted. Without it, a lot of unnecessary boilerplate code is needed.

@nils-wisiol
Copy link

Is this patch likely to be merged? It would be a great help for me, too.

@g00fy-
Copy link

g00fy- commented Apr 20, 2013

+1, why this patch isn't accepted or closed yet?

@petebacondarwin
Copy link
Contributor

It ought to be using a syntax similar, or compatible to, the new ng-repeat syntax: Master Repeat Docs

@g00fy-
Copy link

g00fy- commented Apr 20, 2013

@petebacondarwin do you mean variable in expression track by tracking_expression ?

@quazzie
Copy link
Contributor Author

quazzie commented Apr 22, 2013

Sure, i'm not picky on syntax. I'll take a look at changing it tomorrow.

@quazzie
Copy link
Contributor Author

quazzie commented Apr 23, 2013

Uhmm should it be
"select item as item.text track by item.id for item in items" or
"select item track by item.id as item.text for item in items" ?

@petebacondarwin
Copy link
Contributor

Whatever it is we should try to maintain some level of consistency across the various comprehension syntaxes. This track keyword seems unnecessary to me, but there you go. My take on the ng-options syntax would be:

ng-options="item as item.text for item in items track by item.id"

But again we need someone in the core team to chime in here.

@quazzie
Copy link
Contributor Author

quazzie commented Apr 25, 2013

I agree that we need some input from core team.

@IgorMinar
Copy link
Contributor

+1 for @petebacondarwin's syntax. let's go forward with that.

@quazzie
Copy link
Contributor Author

quazzie commented May 13, 2013

Was it this correct to add a commit ?
Sorry, i'm still learning git.

@quazzie
Copy link
Contributor Author

quazzie commented May 13, 2013

Uhmm it looks like it got merged with my other patch (backbutton-locationchangestart)...
And i have no idea why...

update test for new expression
@quazzie
Copy link
Contributor Author

quazzie commented May 13, 2013

Think i fixed it... ? :)

@petebacondarwin
Copy link
Contributor

PR Checklist (Minor Feature)

  • Contributor signed CLA now or in the past (if you just signed, leave a comment here with your real name for cross reference)
  • Feature improves existing core functionality
  • API is compatible with existing Angular apis and relevant standards (if applicable)
  • PR doesn't contain a breaking change
  • PR contains unit tests
  • PR contains e2e tests (if suitable)
  • PR contains documentation update
  • PR passes all tests on Travis (sanity)
  • PR passes all tests on ci.angularjs.org (cross-browser compatibility)
  • PR is rebased against recent master
  • PR is squashed into one commit per logical change
  • PR's commit messages are descriptive and allows us to autogenerate release notes (required commit message format)
  • All changes requested in review have been implemented

@petebacondarwin
Copy link
Contributor

Landed at: c32a859. Thanks!

@quazzie quazzie deleted the select-options-identifiedby branch May 15, 2013 06:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants