【发布时间】:2020-05-06 06:57:14
【问题描述】:
我的要求是在我的表单中制作嵌套数据,并且每个数据都能够添加更多行或删除它们。我以前用 JavaScript 做过这样的事情,但是用 VueJs 这是我的第一次尝试,所以我有点需要推动 :)
逻辑
- 父数据有选择选项,让用户选择什么 他们需要的输入类型,并根据该选择附加 输入。 (红色)
- 可以添加或删除我的父数据,这样我就可以拥有多个父数据 并且他们每个人同时有几个孩子(蓝色)
- 可以添加或删除我的孩子数据(绿色)
代码
这是我的第一个静态 html(还没有为它编写脚本)
Ps:所有部分都有注释。
<el-form-item label="Variation Name">
<el-col :span="12">
// parent
<el-input placeholder="Please input your variation name" v-model="form.variationParent" class="input-with-select">
// select type of child's input
<el-select v-model="form.variationParent" slot="prepend" placeholder="Select">
<el-option label="Text" value="1"></el-option>
<el-option label="Text Area" value="2"></el-option>
<el-option label="Boolean" value="3"></el-option>
</el-select>
<el-button slot="append" type="success" icon="el-icon-plus"></el-button> //add parent
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button> // delete parent
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9">
// child's
<el-input v-model="form.variationChilds" placeholder="Please input your variation value" class="input-with-select">
<el-button slot="append" type="success" icon="el-icon-plus"></el-button> //add child
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button> // delete child
</el-input>
</el-col>
</el-form-item>
感谢任何形式的idea 和sample codes。
更新
我已经设法使用此代码添加和删除父部分:
<el-form-item v-for="(index, k) in variationParents" :key="k" label="Variation Name">
<el-col :span="12">
<!-- parent -->
<el-input placeholder="Please input your variation name" v-model="form.variationParent" class="input-with-select">
<el-select v-model="form.variationParent" slot="prepend" placeholder="Select">
<el-option label="Text" value="input"></el-option>
<el-option label="Text Area" value="textarea"></el-option>
<el-option label="Boolean" value="boolean"></el-option>
</el-select>
<el-button slot="append" @click="add(k)" type="success" icon="el-icon-plus"></el-button>
<el-button slot="append" @click="remove(k)" v-show="k || ( !k == variationParents.lenghth > 1)" type="danger" icon="el-icon-delete"></el-button>
</el-input>
</el-col>
<el-col class="line text-center" :span="3">Variation Value(s)</el-col>
<el-col :span="9">
<!-- child's -->
<el-input v-model="form.variationChilds" placeholder="Please input your variation value" class="input-with-select">
<el-button slot="append" type="success" icon="el-icon-plus"></el-button>
<el-button slot="append" type="danger" icon="el-icon-delete"></el-button>
</el-input>
</el-col>
</el-form-item>
data() {
return {
variationParents: [
{
name: '',
type: ''
}
],
}
},
methods: {
add(index){
this.variationParents.push({name: '', type: ''});
},
remove(index){
this.variationParents.splice(index, 1);
},
}
这是结果+问题
如您所见,我有新的父母行和add / remove 按钮在工作,但我的输入都得到相同的值,这意味着如果我将第一个输入设置为所有的东西都会得到相同的值。
它们需要每个具有不同的值并作为数组发送到后端。
【问题讨论】:
-
@YomS。关于这个问题,我所拥有的只是我包含在问题中的 html 部分,但如果你需要我可以制作 jsfiddle,但我拥有的所有代码都是这样的 :)
-
嗯,这个小提琴看起来完全不同,与您的模板无关。请发布一些您迄今为止尝试过的数据或脚本。
-
您获得共享值的原因是因为您在不同的对象 (
form) 上执行v-model,而不是在v-for中循环的内容。
标签: javascript vue.js element-ui