amitava82 / angular-multiselect

[NOT MAINTAINED]Native AngularJS multiselect directive
http://amitava82.github.io/angular-multiselect
MIT License
140 stars 124 forks source link

Can I use different option properties as names and values? #26

Closed airandfingers closed 8 years ago

airandfingers commented 10 years ago

Per the example at http://plnkr.co/edit/LPGYIf?p=preview, I'm using one property of my option objects as the name for the option, with options="t.name for t in tags".

Rather than use the entire option object as a value, I want to only use the _id property, so that the model I bind to contains a list of _id Strings, rather than a list of Objects. This matches my backend, where my MongoDB documents have _id references to other documents.

Is it possible to use a single property as the value of an option, e.g. values="t._id for t in tags"?

My code is live at http://trollsgoals.ayoshitake.com/

airandfingers commented 10 years ago

Apparently, the "optionParser" factory already supports a modelMapper option; in https://github.com/airandfingers/angular-multiselect/commit/9e3ee1641aa31377443b01b04b8edf0640765691 I enabled the use of modelMapper, so that the given expression is used instead of the entire model object.

To use this option, I followed the regular expression in optionParser, and changed options from "t.name for t in tags" to "t._id as t.name for t in tags"

I've tested this briefly without the "t._id as" part included, and it seems to work as before. I don't know whether my change will work in all cases, so for now I'm not going to submit a Pull Request with my change.

RobbertWolfs commented 8 years ago

@airandfingers Is your pull request accepted? Because I think we have the same problem, we also want the model we bind to contains a list of id strings.. We are also trying to use "t._id as t.name for t in tags" but it always binds our full model to the list.

amitava82 commented 8 years ago

I'm going to look into this tomorrow and make necessary changes 👍

RobbertWolfs commented 8 years ago

That would be awesome 👍

amitava82 commented 8 years ago

@RobbertWolfs So, I just tried c.id as c.name for c in fruits and it binds id to the selected. Can you show me an example in plunk?

Please make sure you have latest code. Above plunker is using old codebase.

http://plnkr.co/edit/NKas7sSP5lGJz3OTv0YU?p=preview

RobbertWolfs commented 8 years ago

@amitava82 Let me try to implement it as your example. Otherwise I will try to create a plunker.

RobbertWolfs commented 8 years ago

Could it be it gives problems with angular 1.5.5? Because we are using the angular components from angular 1.5.x...

I tried to create a gist of all the necessary code https://gist.github.com/RobbertWolfs/4c03ff1a27b042d3fb1d97c1b86445d7

And this is the result we get...

screen shot 2016-06-07 at 10 54 53
amitava82 commented 8 years ago

Are you sure that you are using latest version of multiselect lib?

RobbertWolfs commented 8 years ago

I just did npm install angular-multiselect

amitava82 commented 8 years ago

ah.. It's old.. Hold on, I'll push latest.

RobbertWolfs commented 8 years ago

ok thanks

amitava82 commented 8 years ago

Alright published angular-multiselect@1.2.0

RobbertWolfs commented 8 years ago

Let me check if I get it to work with the new version.

RobbertWolfs commented 8 years ago

@amitava82 Thank you it works like a charm 👍 💯