1

trying to create a dependency between two form select options where in the second select tag i would like to show data based on the first option select. I know i can use v-if but it's not really giving me a solution that feels like best practice. my relevant part of the form looks like this:

<el-form-item label="Provider">
    <el-select v-model="form.provider" placeholder="select provider">
        <el-option v-for="provider in form.providers"
                   :label="provider"
                   :value="provider">{{provider}}
        </el-option>
    </el-select>
</el-form-item>
<el-form-item label="Accounts">
    <el-select v-model="form.account" placeholder="Select account">
        <!--****here it is****-->
        <el-option v-for="account in form.accounts.revcontent"
                   label="account"
                   value="account">{{account}}</el-option>
    </el-select>
</el-form-item>

now here in my code im using provider name hardcoded -> revcontent but i would like this value to be the v-model="form.provider" value. so incase user chose revcontent as provider what actually happenning is what show sin this piece of code but if he chose adsense for example the logic sould change to this:

  <el-option v-for="account in form.accounts.adsense"...>

I tried solutions like:

v-for="account in form.accounts.form.provider" 

and

v-for="account in form.accounts.{{form.proivder}}"

but they obviously didn't work any idea how can i achive this in a way that will consider as best practice ?? thx

1 Answer 1

1

You can use a computed property to return the options for the 2nd select based on the first select's v-modellike this:

computed: {
    secondSelect(){
        return this.form.accounts[this.form.provider];
    }
} 

Then you can loop through this computed [property like this :

<el-form-item label="Provider">
    <el-select v-model="form.provider" placeholder="select provider">
        <el-option v-for="provider in form.providers"
                   :label="provider"
                   :value="provider">{{provider}}
        </el-option>
    </el-select>
</el-form-item>
<el-form-item label="Accounts">
    <el-select v-model="form.account" placeholder="Select account">
        <!--****here it is****-->
        <el-option v-for="account in secondSelect"
                   label="account"
                   value="account">{{account}}</el-option>
    </el-select>
</el-form-item> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.