Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object values instead of simple values #15

Open
TheYves opened this issue Feb 12, 2015 · 9 comments
Open

Object values instead of simple values #15

TheYves opened this issue Feb 12, 2015 · 9 comments

Comments

@TheYves
Copy link

TheYves commented Feb 12, 2015

Is it possible to have objects as values instead simple values?

Here's a plunker: http://plnkr.co/edit/GgfVAn8M4DvAIWzLyI5b?p=preview

@rubenv
Copy link
Owner

rubenv commented Feb 12, 2015

Should work, but an ng-model directly on the scope won't:

  $scope.foo = null;
  $scope.bar = null;

https://egghead.io/lessons/angularjs-the-dot

@TheYves
Copy link
Author

TheYves commented Feb 12, 2015

I'm sorry, I don't understand what you mean. In my plunker you can see that with regular selectboxes I can use objects where with select2 it doesn't work. Whatever I choose from the list, the model is always set to the last entry in the list.

@rubenv
Copy link
Owner

rubenv commented Feb 13, 2015

I'm sorry, I don't understand what you mean.

Check the video I linked. Anyway, that's not an angular-select2 issue, but you will be in trouble if you don't learn it.

As for objects: values in the select2 widget are keyed by their toString value, so if your objects define unique toString values, it should work. If not, I suggest you don't use objects as values (it's ugly anyway), use an index key.

@ramonwenger
Copy link

Hey rubenv

Thank you for that awesome select2-wrapper!

I am running into the same problem as TheYves though.

AngularJS can assign whole objects to a variable in a select (which you say is ugly, can you elaborate on that?), but the directive assigns the object's toString-method as array key, which leads to the last item in the array being selected, no matter which element was clicked on in the dropdown.

Might I suggest a solution where the directive checks wether or not the value is a String or an object, and if it's an object check for an id (or even better, look for the "track by"-attribute of the query string)?

If you are open to the suggestion I can even write the functionality and make a pull request.

Regards

@rubenv
Copy link
Owner

rubenv commented Mar 26, 2015

Might I suggest a solution where the directive checks wether or not the value is a String or an object, and if it's an object check for an id (or even better, look for the "track by"-attribute of the query string)?

Sure, track by makes total sense!

@TheYves
Copy link
Author

TheYves commented Apr 22, 2015

@DonHaron have you already implemented your suggestion? I would be very interested. :)

@ramonwenger
Copy link

@TheYves I started it and made it work, but it was rubbish and not in line with the ngSelect directive, and broke other parts of the select2-directive. So I will have to do a proper rewrite based on ngSelect. I didn't get to it yet though...

@TheYves
Copy link
Author

TheYves commented Apr 22, 2015

Okay, thanks for the reply.

@buremba
Copy link

buremba commented Sep 5, 2016

Any update on this issue?

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

No branches or pull requests

4 participants