angular-select2 icon indicating copy to clipboard operation
angular-select2 copied to clipboard

Object values instead of simple values

Open TheYves opened this issue 10 years ago • 9 comments

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

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

TheYves avatar Feb 12 '15 14:02 TheYves

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

rubenv avatar Feb 12 '15 16:02 rubenv

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.

TheYves avatar Feb 12 '15 19:02 TheYves

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.

rubenv avatar Feb 13 '15 09:02 rubenv

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

ramonwenger avatar Mar 26 '15 12:03 ramonwenger

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!

rubenv avatar Mar 26 '15 12:03 rubenv

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

TheYves avatar Apr 22 '15 15:04 TheYves

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

ramonwenger avatar Apr 22 '15 15:04 ramonwenger

Okay, thanks for the reply.

TheYves avatar Apr 22 '15 15:04 TheYves

Any update on this issue?

buremba avatar Sep 05 '16 11:09 buremba