【问题标题】:VueJS adding form components dynamically in a nested v-for loop?VueJS 在嵌套的 v-for 循环中动态添加表单组件?
【发布时间】:2019-03-06 01:11:29
【问题描述】:

我正在尝试实现以下目标,但遇到了障碍。

我有以下表格:

当我点击“新交易版块”按钮时,我会创建一个新版块,如下所示:

然而,当按下“新项目”按钮时,我想要做的是能够在每个部分中添加多个文本框。我尝试在按下“新政按钮”时创建的容器中嵌套第二个 v-for 循环,但未能使其正常工作。

我对使用任何类型的 JS 都非常陌生,更不用说 VueJS 框架了,因此我们将不胜感激。到目前为止,这是我的代码:

<!--Start of content-->
        <div class="container">
            <button class="btn btn-success mt-5 mb-5" @click="addNewSection">
                New Deal Section
            </button>

            <div class="card mb-3" v-for="(section, index) in sections">

                <div class="card-body">
                    <button class="btn btn-success mt-5 mb-5" @click="addNewItem">
                        New Item
                    </button>

                    <span class="float-right" style="cursor:pointer">
                        X
                    </span>

                    <h4 class="card-title">Deal section {{ index + 1}}</h4>

                    <div class="employee-form" v-for="(addition, index) in additionals">
                        <input type="text" class="form-control mb-2" placeholder="Item" v-model="addition.item">
                    </div>

                    <div class="employee-form">
                        <input type="text" class="form-control mb-2" placeholder="Item" v-model="section.item">
                    </div>
                </div>
            </div>
        </div>

        <script>
            var app = new Vue({
                el: '.container',
                data: {
                    sections: [
                        {
                            item: '',
                        }
                    ]
                },
                methods: {
                    addNewSection () {
                        this.sections.push({
                            item: ''
                        })
                    },
                    addNewItem () {
                        this.additionals.push({
                            item: ''
                        })
                    }
                }
            })
        </script>

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    您应该在sections 数组中添加additionals 数组,如下所示:

    <div id="app">
        <div class="container">
            <button class="btn btn-success mt-5 mb-5" @click="addNewSection">
                New Deal Section
            </button>
    
            <div class="card mb-3" v-for="(section, index) in sections">
                <hr>
                <div class="card-body">
                    <button class="btn btn-success mt-5 mb-5" @click="addNewItem(index)"> <!-- passing the index -->
                        New Item
                    </button>
    
                    <span class="float-right" style="cursor:pointer">
                        X
                    </span>
    
                    <h4 class="card-title">Deal section {{ index + 1}}</h4>
    
                    <div class="employee-form" v-for="(addition, index) in section.additionals"> <!-- additionals of the section -->
                        <input type="text" class="form-control mb-2" placeholder="Item" v-model="addition.item">
                    </div>
    
                    <div class="employee-form">
                        <input type="text" class="form-control mb-2" placeholder="Item" v-model="section.item">
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        var app = new Vue({
            el: '.container',
            data: {
                sections: [
                    {
                        item: '',
                        additionals: [] // <-
                    }
                ]
    
            },
            methods: {
                addNewSection() {
                    this.sections.push({
                        item: '',
                        additionals: [] // <-
                    })
                },
                addNewItem(id) {
                    // passing the id of the section
                    this.sections[id].additionals.push({
                        item: ''
                    })
                }
            }
        })
    </script>
    

    JSFiddle:https://jsfiddle.net/Wuzix/qs6t9L7x/

    【讨论】:

    • 感谢您的回答。拯救了我的一天!谢谢,伙计,但是,不需要在 data function of the Vue instance 内的 sections 数组内添加对象,因为当您推入数组时,数据已经被按需定义,除非您的部分是强制性的,那么就可以了跨度>
    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多