欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-09-08 11:19:24
添加核心代码如下: this.data.push({ type: [{ value: '选项1', label: '...

添加核心代码如下:

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实现动态添加或者删除对象和对象数组的操作方法

总结

以上所述是小编给大家介绍的vue实现动态添加或者删除对象和对象数组的操作方法,希望对大家有所帮助