Vue实现动态添加或者删除对象和对象数组的操作方法

时间:2021-05-26

添加核心代码如下:

this.data.push({ type: [{ value: '选项1', label: 'in' }, { value: '选项3', label: 'out' }], value:[{ value: '选项1', label: 'CSV' }, { value: '选项3', label: 'TSV' }], parameter:'', default:'', description:'', isDelete:false, });

删除核心代码如下:

for (let i = 0;i<this.data.length;i++){ let obj = this.data[i]; if (obj.isDelete){ this.data.splice(i,1); i-- } }

全部代码如下:

<template> <div> ============================32、在Vue项目中实现动态添加或者删除对象和对象数组============================ <div> <el-button type="primary" icon="el-icon-plus" @click="add"></el-button> <el-button type="primary" icon="el-icon-delete" @click="deleteItem"></el-button> <el-row :gutter="50" v-for="(item,index) in data" :key="index"> <!--Type--> <el-col :span="4"> <div class="grid-content bg-purple"> <el-row > <el-col :span="24"> <el-select v-model="value" placeholder="请选择" size="mini"> <el-option v-for="obj in item.type" :key="obj.value" :label="obj.label" :value="obj.value"> </el-option> </el-select> </el-col> </el-row > </div> </el-col> <!--value--> <el-col :span="4"> <div class="grid-content bg-purple-light"> <el-row > <el-col :span="24"> <el-select v-model="value" placeholder="请选择" size="mini"> <el-option v-for="obj in item.value" :key="obj.value" :label="obj.label" :value="obj.value"> </el-option> </el-select> </el-col> </el-row > </div> </el-col> <!--Parameter--> <el-col :span="4"> <div class="grid-content bg-purple"> <el-input size="mini" placeholder="请输入内容" v-model="item.parameter" clearable> </el-input> </div> </el-col> <!--Default--> <el-col :span="4"> <div class="grid-content bg-purple-light"> <el-input size="mini" placeholder="请输入内容" v-model="item.default" clearable> </el-input> </div> </el-col> <!--Description--> <el-col :span="4"> <div class="grid-content bg-purple"> <el-input size="mini" placeholder="请输入内容" v-model="item.description" clearable> </el-input> </div> </el-col> <!--Del--> <el-col :span="4"> <div class="grid-content bg-purple-light " id="checkboxSpacingDiv"> <!-- `checked` 为 true 或 false --> <el-checkbox v-model="item.isDelete" size="medium"></el-checkbox> </div> </el-col> </el-row> </div> </div></template><script> export default { name: "VueArrays_32", data(){ return { data:[ { type: [{ value: '选项1', label: 'in' }, { value: '选项3', label: 'out' }], value:[{ value: '选项1', label: 'CSV' }, { value: '选项3', label: 'TSV' }], parameter:'', default:'', description:'', isDelete:false, }, ], } }, methods:{ add(){ this.data.push({ type: [{ value: '选项1', label: 'in' }, { value: '选项3', label: 'out' }], value:[{ value: '选项1', label: 'CSV' }, { value: '选项3', label: 'TSV' }], parameter:'', default:'', description:'', isDelete:false, }); }, deleteItem(){ for (let i = 0;i<this.data.length;i++){ let obj = this.data[i]; if (obj.isDelete){ this.data.splice(i,1); i-- } } } } }</script><style scoped></style>

效果图如下:

总结

以上所述是小编给大家介绍的Vue实现动态添加或者删除对象和对象数组的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章