vue 通过下拉框选中的数据 去创建输入框
程序员文章站
2024-01-02 22:53:52
...
我用的是ui框架是Ant Design Vue
<a-row>
<a-row class="body">
<!-- 循环创建 -->
<a-col :span="24" v-show="peopleItemArr">
<a-form-model-item
:label="item.name"
v-for="item in peopleItemArrVUEX"
:key="item.id"
>
<a-input placeholder v-model="item.value" />
</a-form-model-item>
</a-col>
<a-col :span="24">
<a-form-model-item
label="其他指标项"
:label-col="{ span: 6 }"
:wrapper-col="{ span: 16 }"
>
<a-select placeholder @change="handlePeoItemChange($event)" allowClear>
<a-select-option
v-for="item in peopleItemArr"
:key="item.id"
:value="item.name"
>{{item.name}}</a-select-option>
</a-select>
<a-button type="primary" icon="plus" @click="setPeoItemAdd">创建</a-button>
</a-form-model-item>
</a-col>
</a-row>
</a-row>
data() {
peopleItemArrVUEX: [], // 循环选择的所有项
peoItemStrVUEX: {}, // 接收选中值
peopleItemArr:[ // 这里是后台数据
{ id: 10, pid: 5, name: "生日",childList: [] },
{ id: 9, pid: 3, name: "年龄",childList: [] },
{ id: 8, pid: 6, name: "学校",childList: [] },
]
},
methods: {
// 下拉框选中值
handlePeoItemChange(val) {
let obj = {};
obj = this.peopleItemArr.find((item) => {
return item.name === val;
});
this.peoItemStrVUEX = obj;
},
// 判断下拉框是否选中了值 并进行去重处理
setPeoItemAdd() {
if (Object.keys(this.peoItemStrVUEX).length === 0) {
return false;
}
this.peopleItemArrVUEX.push(this.peoItemStrVUEX);
this.peopleItemArrVUEX = this.unique(this.peopleItemArrVUEX);
},
// 数组对象去重
unique(arr) {
const res = new Map();
return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
},
}
// 与后台交互 直接拿peopleItemArrVUEX里面的值就好