LoginSignup
1
3

More than 5 years have passed since last update.

Vue.jsのデータバインディングとレンダリング

Posted at

やりたいこと

  • テーブルの tr を初期表示で5行作成
  • 「追加する」ボタンを押すと1行ずつ tr が複製される
  • tr 中にあるフォームデータは後で送信したいので v-model でデータバインディング

コード

app.vue
//テンプレート
<template>
    <section class="section">
        <table>
            <tr v-for="(item, index) in items">
                <td><input type="text" v-model="items[index].code1"></td>
                <td><input type="text" v-model="items[index].code2"></td>
                <td><input type="text" v-model="items[index].name"></td>
            </tr>
        </table>

        <div class="medium-4">
            <div class="button" @click="add">追加する</div>
        </div>
    </section>
</template>

//スクリプト
<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        },
        data() {
            return {
                items: []
            }
        },
        created() {
            var $defaultNum = 5;
            for(var i = 0; i < $defaultNum; i++){
                this.items.push({
                    code1: '',
                    code2: '',
                    name: ''
                });
            }
        },
        methods: {
            add() {
                this.items.push({
                    code1: '',
                    code2: '',
                    name: ''
                });
                console.log("hoge");
            }
        }
    }
</script>
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3