Framework used: Vue and Element UI
Mission: To display the first matched value as default.
Problem: If Texas is selected from the common states, it shows Texas from the all states section when opening the dropdown(both should be selected as both have same values which is correct, the working is totally correct).
Approach: I have tried to hide the all states list when the value is present in the common states but it is not the desired outcome.
Desired Outcome: If Texas is selected and is in the common states, I would like to show that as default while opening the dropdown(in the common states section instead of the all states section). Is it possible to do so?
Link to Codepen: https://codepen.io/limbe_me/pen/BaMwRNz
Boilerplate:
<template>
<div>
<el-select v-model="selectedState" placeholder="Select a state">
<el-option-group label="common states">
<el-option
v-for="item in commonStates"
:label="item"
:key="item + '_common'"
:value="item"
></el-option>
</el-option-group>
<el-option-group label="all states">
<el-option
v-for="item in allStates"
:label="item"
:key="item + '_all'"
:value="item"
></el-option>
</el-option-group>
</el-select>
</div>
</template>
<script>
export default {
data() {
return {
selectedState: "",
commonStates: ["California", "New York", "Florida", "Texas", "Hawaii"],
allStates: [
"Alabama",
"Alaska",
"Arizona",
"Arkansas",
"California",
".....",
"Washington",
"West Virginia",
"Wisconsin",
"Wyoming"
]
};
},
methods: {
// Your methods go here
}
};
</script>
You can do it like this
Add
v-if="enabled"to yourallStatesoption group. Then set ittruewhen dropdown is visibleIn
ElementUIdocument, I saw they havevisible-change. So you can hook that event up and setenabledto$eventHere the Codepen: https://codepen.io/duckstery/pen/QWYqOQo
For your extend request, I've adjusted mine code
Set default
enabledtotrueYou'll have to check if the
selectedStateis included incommonStates. Iftrue, setenabledtofalseand set it totruein thenextTickHere the Codepen: https://codepen.io/duckstery/pen/wvNrmMm