您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
添加核心代码如下:
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实现动态添加或者删除对象和对象数组的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。