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

UiSelect - Select option by value #285

Closed
giovannefeitosa opened this issue Mar 23, 2017 · 2 comments
Closed

UiSelect - Select option by value #285

giovannefeitosa opened this issue Mar 23, 2017 · 2 comments

Comments

@giovannefeitosa
Copy link

Would be nice to select an option by it's string value instead of an object.

<template>
    <ui-select
        ref="selectUser"
        label="Select an user"
        v-model="selectedUser"
        :options="users">
    </ui-select>
</template>

<script>
export default {
    data() {
        return {
            users: [{label: "First User", value: 1}, {label: "Second User", value: 2}],
            selectedUser: '',
        }
    },
    created() {
        // simulate ajax call
        setTimeout(() => {
            this.$refs['selectUser'].selectOptionByValue(2);
        }, 5000);
    }
}
</script>

I wish to contribute with this. Can I?

@JosephusPaye
Copy link
Owner

HI @shakegioh,

I'm working on some updates to UiSelect for the next release, one of which will be what you are suggesting, and also #270.

Currently you can select an option (using the object) by simply assigning the option to the prop you pass to v-model.

@JosephusPaye
Copy link
Owner

Even though this would be convenient, for consistency, I think it's best to leave the current behavior as-is.

If not, the value (v-model) will be a string on initial render, and when changed, will go back to being an object.

You select the option by value using this:

data() {
    const users = [{label: "First User", value: 1}, {label: "Second User", value: 2}];

    return {
        users: users,
        selectedUser: users.filter(user => user.value == 2)[0],
    };
},

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

No branches or pull requests

2 participants