0
<option v-for="{Service, Price} in delivery_array">
    {{Service}} - Rp {{Price}}
</option>

that's my option list from delivery_array

My delivery_array value:

[
    {"Price": "18000", "Service": "REG"},
    {"Price": "30000", "Service": "FAS"}
]

I want to add value attribute in every option element with JSON object like this:

{"Price": "THE_PRICE", "Service": "THE_SERVICE"}

So it looked like this:

<option v-for="{Service, Price} in delivery_array" value='{"service":{{Service}}, "price":{{Price}} }'>
    {{Service}} - Rp {{Price}}
</option>

But I have no idea how to code it like that.

I try it before with v-model attribute like v-model="Service", and I get the error message like this:

: v-model is not supported on this element type. If you are working with `contenteditable`, it's recommended to wrap a library dedicated for that purpose inside a custom component.

If I have a missing information or bad grammar in my question. I will edit it. Sorry for my bad english language.

2 Answers 2

2

The v-model goes on the select tag, since that holds the value for the selected option. You also need a key on the v-for.

I don't know if you can use destructuring in the v-for directly, but this should work:

<select v-model="Service">
    <option v-for="item in delivery_array" :value="item.Service" :key="item.Service">
        {{ item.Service }} - Rp {{ item.Price }}
    </option>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

You could use destructing in v-for
1

The v-model should be in select element and the value in option :

<select v-model="delivery">
<option v-for="{Service, Price} in delivery_array" :value='{"service":Service, "price":Price }'>
    {{Service}} - Rp {{Price}}
</option>

</select>

delivery must be declared as data property, and value bound like :value='...

1 Comment

Sorry, I need help again. The value become Like this: [object Object] How to get the object value from it? I use PHP on my backend.

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.