【发布时间】:2020-07-20 23:35:12
【问题描述】:
我需要创建一个动态表单,它以树的方式构建。用户(创建/设计表单的人)可以随时更改表单,因此我的输入也会动态变化。
例子:
root
--groeisnelheid
----niveau
------beginner (input radio)
------recreatief (input radio)
------competitie (input radio)
------tour (input radio)
----input text
----begeleiding
------another input
------and another
--another category
----speed
------input
------input
如您所见,这不是最简单的表单...用户(在本例中为管理员用户)具有编辑或创建新表单的能力。
我可能低估了这份工作,因为我正在尝试创建它的输入端,并且已经在苦苦挣扎。
到目前为止我做了什么:
TreeComponent.vue
<template>
<div class="tree">
<ul class="tree-list">
<tree-node :node="treeData"></tree-node>
</ul>
</div>
</template>
<script>
export default {
props: {
treeData: [Object, Array]
},
data() {
return {
treeValues: []
};
},
methods: {
sendForm: function() {}
}
};
</script>
TreeNodeComponent.vue
<template>
<li v-if="node.children && node.children.length" class="node">
<span class="label">{{ node.name }}</span>
<ul>
<node v-for="child in node.children" :node="child" :key="child.id"></node>
</ul>
</li>
<div v-else class="form-check form-check-inline">
<input
type="radio"
class="form-check-input"
:name="'parent-' + node.parent_id"
:id="node.id"
/>
<label for class="form-check-label">{{ node.name }}</label>
</div>
</template>
<script>
export default {
name: "node",
props: {
node: [Object, Array]
}
};
</script>
这会导致所有输入都按我的意愿显示。但现在真正的问题是;如何在我的根组件(TreeComponent.vue)中获取这些输入的值,以便将其发送到服务器。更改时或用户在表单中继续时。
我习惯于在这方面使用v-model,但我不知道如何在递归组件上使用它,因为文档只涉及设置直接父级的数据。
任何帮助将不胜感激。
【问题讨论】:
标签: javascript html vue.js recursion axios