0

I have added html select options dynamically while clicking on button

<div id="app">
    <div>
        <button class="button btn-primary" @click="addRow">Add row</button>
        <button @click="showValues">
            Show values
        </button>
    </div>
    <div v-for="row in rows" :id=row.id>
        <button-counter></button-counter>
    </div>
</div>
<script>

    Vue.component('button-counter', {
        props: ['value'],
        template: '<select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>'
    })
    var app = new Vue({
        el: "#app",
        data: {
            rows: [],
            values: {},
            count: 0,
            selected: ''
        },
        methods: {
            addRow: function () {
                var txtCount = ++this.count;
                id = 'ddl_' + txtCount;
                this.rows.push({ title: "first", description: "ddl1", id });

            },
            showValues() {
                console.log(this.values)
            }

        }
    });
</script>

I am using component for adding html select dynamically.when i click on Add row button, new drop down will add. My problem is i want to get drop down values on Show values button click.

2 Answers 2

1

For accessing value what you can do is add ref to all components and get the value of the v-model using the $ref but for that, you need to add the v-model to the select component. I have created the plunker with your code and its working fine. For more detail please refer the codepen.

codepen - https://codepen.io/anon/pen/Qoeybv

Sign up to request clarification or add additional context in comments.

Comments

0

I see that you have one "Show values button click", but can exist many select HTML elements. Have to decide which element you want to get. If you want to get all it can be done in this.$children as array of objects. See this [enter link description here][1]. I'm new at vue.js and I do not claim to be a good practice / solution. There is added event on each select (for example) and show last select element in showValues().

[1]: https://codepen.io/iganchev87/pen/xBvOMJ

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.