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

VaSelect using objects as options: get get full object on select event #4433

Open
beard7 opened this issue Nov 20, 2024 · 3 comments
Open

VaSelect using objects as options: get get full object on select event #4433

beard7 opened this issue Nov 20, 2024 · 3 comments

Comments

@beard7
Copy link

beard7 commented Nov 20, 2024

When using objects as options for a VaSelect, I often need to update two (or more) model properties with properties from the selected option.

This is not easy with VaSelect as the selected value is the option property specified by valueBy: there's no event that provides the full object, unless you don't use valueBy which introduces other issues.

A @select event that provides the selected object would be really useful.

@m0ksem
Copy link
Collaborator

m0ksem commented Nov 21, 2024

What “other” issues do you have if valueBy is not used?

unless you don't use valueBy which introduces other issues.

@beard7
Copy link
Author

beard7 commented Nov 21, 2024

Well, if valueBy is not used, v-model will be bound to the whole object, rather than, say, the object id. So, let's say I have a VaSelect that I want to use to set both 'user_id' and 'user_name' in the model... this will provide the full option in the @update event, but it will also set model.uer_id to the whole object:

<VaSelect
v-model="model.user_id"
:options="users"
text-by="name"
track-by="id"
@update:modelValue="selectUser" />

I've resorted to using a computed property with getter and setter, which work well, but I'm not sure if it's the easiest solution:

...
<VaSelect
  v-model="user"
  label="User"
  :options="users"
  text-by="name"
  :value-by="(option) => { 
    return { id: option.id, name: option.name };
  }"
/>
...

...
const user = computed({
    get() {
        return { id: model.value.user_id, name: model.value.user_name};
    },
    set(value) {
        model.value.user_id = value.id;
        model.value.user_name = value.name;
    },
});
...

@m0ksem
Copy link
Collaborator

m0ksem commented Nov 26, 2024

You can use trackBy set to user_id instead of id and changing shape of your option.

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

2 participants