vue动态添加行/删除行

页面效果

在这里插入图片描述

点击相应的添加后,每个 el-table 增加一行
在这里插入图片描述

代码:

<el-card>
                <div class="titleitem">
                  <span >工作/学习经历</span>
                </div>
                <el-table
                  :data="experienceData"
                  stripe
                  style="width: 100%">
                  <el-table-column
                    prop="starttime"
                    label="开始时间"
                    width="260">
                    <template slot-scope="scope">
                      <el-date-picker
                        type="month"
                        size="small"
                        placeholder="选择年月"
                        v-model="scope.row.starttime">
                      </el-date-picker>
                      <!-- <el-input></el-input> -->
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="endtime"
                    label="结束时间"
                    width="260">
                    <template slot-scope="scope">
                      <el-date-picker
                        type="month"
                        size="small"
                        placeholder="选择年月"
                        v-model="scope.row.endtime">
                      </el-date-picker>
                      <!-- <el-input v-model="scope.row.endtime"></el-input> -->
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="resume"
                    label="简历"
                    width="380">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.resume"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="operate"
                    label="操作">   
                    <template slot-scope="scope">
                      <el-button 
                        size="mini" 
                        type="success" 
                        icon="el-icon-save" 
                        @click="handlesaveExperience(scope.$index, scope.row)">保存
                      </el-button>
                      <el-button 
                        size="mini" 
                        type="danger" 
                        icon="el-icon-delete" 
                        @click="handleDeleteExperience(scope.$index, scope.row)">删除
                      </el-button>
                      
                    </template>                
                  </el-table-column>            
                </el-table>
                <div>
                  <el-button 
                    type="primary" 
                    icon="el-icon-plus" 
                    size="mini" 
                    @click="handleAddExperienceline">新增学习/工作简历
                  </el-button>
                </div>
              </el-card>
              <el-card>
                <div class="titleitem">
                  <span >奖惩情况</span>
                </div>
                <el-table
                  :data="bonuspenaltyData"
                  stripe
                  style="width: 100%">
                  <el-table-column
                    prop="status"
                    label="奖励/惩罚"
                    width="100">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.status"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="date"
                    label="奖惩年月"
                    width="260">
                    <template slot-scope="scope">
                      <el-date-picker
                        type="month"
                        size="small"
                        placeholder="选择年月"
                        v-model="scope.row.date">
                      </el-date-picker>
                      <!-- <el-input></el-input> -->
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="num"
                    label="奖惩文号"
                    width="180">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.num"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="remarks"
                    label="备注"
                    width="360">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.remarks"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="operate"
                    label="操作">   
                    <template slot-scope="scope">
                      <el-button 
                        size="mini" 
                        type="success" 
                        icon="el-icon-save" 
                        @click="handlesaveBonuspenalt(scope.$index, scope.row)">保存
                      </el-button>
                      <el-button 
                        size="mini" 
                        type="danger" 
                        icon="el-icon-delete" 
                        @click="handleDeleteBonuspenalt(scope.$index, scope.row)">删除
                      </el-button>                      
                    </template>                
                  </el-table-column>            
                </el-table>
                <div>
                  <el-button 
                    type="primary" 
                    icon="el-icon-plus" 
                    size="mini" 
                    @click="handleAddBonuspenaltLine">新增奖惩情况
                  </el-button>
                </div>
              </el-card>

              <el-card>
                <div class="titleitem">
                  <span >年度考核</span>
                </div>
                <el-table
                  :data="AnnualAssessmentData"
                  stripe
                  style="width: 100%">
                  <el-table-column
                    prop="year"
                    label="年度"
                    width="260">
                    <template slot-scope="scope">
                      <el-date-picker
                        type="year"
                        size="small"
                        placeholder="选择年"
                        v-model="scope.row.year">
                      </el-date-picker>
                      <!-- <el-input v-model="scope.row.year"></el-input> -->
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="result"
                    label="结果"
                    width="260">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.result"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="remarks"
                    label="备注"
                    width="380">
                    <template slot-scope="scope">
                      <el-input v-model="scope.row.remarks"></el-input>
                    </template>
                  </el-table-column>            
                  <el-table-column
                    prop="operate"
                    label="操作">   
                    <template slot-scope="scope">
                      <el-button 
                        size="mini" 
                        type="success" 
                        icon="el-icon-save" 
                        @click="handlesaveAnnualAssessment(scope.$index, scope.row)">保存
                      </el-button>
                      <el-button 
                        size="mini" 
                        type="danger" 
                        icon="el-icon-delete" 
                        @click="handleDeleteAnnualAssessment(scope.$index, scope.row)">删除
                      </el-button>                      
                    </template>                
                  </el-table-column>            
                </el-table>
                <div>
                  <el-button 
                    type="primary" 
                    icon="el-icon-plus" 
                    size="mini" 
                    @click="handleAddAnnualAssessmentLine">新增年度考核
                  </el-button>
                </div>
              </el-card>

data部分

  data() {
    return {
      options:[],
      value:'',
      value1:'',
      month1:'',
      month2:'',
      experienceData: [{
          starttime: '1996-05',
          endtime: '1999-06',
          resume: '就读于xxxx学习xxxx专业',
        }, ],
      bonuspenaltyData:[{
        status:'奖励',
        date:'2022-05',
        num:'123456',
        remarks:'助人为乐'
      }],
      AnnualAssessmentData: [{
        year:'2021',
        result:'合格',
        remarks:'考核通过',
      }],
    };
  },

methods部分

methods: {
    //增加经验行
    handleAddExperienceline() {
      if (this.experienceData == undefined) {
        this.experienceData = new Array();
      }
      let obj = {};
      
      this.experienceData.push(obj);
    },
    //保存经验行
    handlesaveExperience(a, b) {
      console.log(a + b);
    },
    //删除经验行
    handleDeleteExperience(index) {
      console.log(index);
			this.experienceData.splice(index, 1)
    },

    //增加奖惩行
    handleAddBonuspenaltLine() {
      if (this.bonuspenaltyData == undefined) {
        this.bonuspenaltyData = new Array();
      }
      let obj = {};
      
      this.bonuspenaltyData.push(obj);
    },
    //保存奖惩行
    handlesaveBonuspenalt(a, b) {
      console.log(a + b);
    },
    //删除奖惩行
    handleDeleteBonuspenalt(index) {
      console.log(index);
			this.bonuspenaltyData.splice(index, 1)
    },

    //增加年度考核行
    handleAddAnnualAssessmentLine() {
      if (this.AnnualAssessmentData == undefined) {
        this.AnnualAssessmentData = new Array();
      }
      let obj = {};
      
      this.AnnualAssessmentData.push(obj);
    },
    //保存年度考核行
    handlesaveAnnualAssessment(a, b) {
      console.log(a + b);
    },
    //删除年度考核行
    handleDeleteAnnualAssessment(index) {
      console.log(index);
			this.AnnualAssessmentData.splice(index, 1)
    },

  }

注意:
若缺依赖,安装相应依赖。

Vue 框架中实现动态添加表格的功能,可以通过操作数据数组来实现,因为 Vue 的响应式机制会自动更新视图。以下是一个完整的实现方法: ### 实现逻辑 1. **数据定义**:在 `data()` 中定义一个 `tableData` 数组来存储表格数据,并定义一个 `newRow` 对象来表示新的初始数据。 2. **添加方法**:在 `methods` 中定义一个 `addRow()` 方法,通过 `push()` 方法将新的数据添加到 `tableData` 数组中。 3. **表格渲染**:使用 `v-for` 指令循环 `tableData` 数组来渲染表格。 4. **触发按钮**:绑定一个按钮点击事件 `@click="addRow"` 来触发添加的操作。 ### 示例代码 以下是一个完整的 Vue 动态添加表格的示例代码: ```html <template> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td><input v-model="item.id" /></td> <td><input v-model="item.name" /></td> <td><input v-model="item.age" /></td> </tr> </tbody> </table> <button @click="addRow">Add Row</button> </div> </template> <script> export default { data() { return { tableData: [ { id: 1, name: 'John', age: 30 }, { id: 2, name: 'Doe', age: 25 } ], newRow: { id: null, name: '', age: null } // 新的初始数据 }; }, methods: { addRow() { // 添加数据到表格数据数组 this.tableData.push(Object.assign({}, this.newRow)); } } }; </script> <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; text-align: left; } input { width: 100%; } </style> ``` ### 实现说明 - **数据绑定**:通过 `v-model` 双向绑定,用户可以在输入框中修改表格的内容,数据会自动更新到 `tableData` 中。 - **响应式更新**:当调用 `addRow()` 方法时,由于 Vue 的响应式机制,表格会自动更新并显示新增的。 - **对象复制**:使用 `Object.assign({}, this.newRow)` 创建一个新的对象,避免直接引用 `newRow` 导致多个数据共享同一个对象引用的问题[^1]。 ### 拓展功能 - **删除**:可以通过在每一行添加一个删除按钮,并绑定一个 `removeRow(index)` 方法,使用 `splice(index, 1)` 删除指定索引的。 - **动态表单**:如果需要更复杂的动态表单交互,可以结合 Vue 的组件化思想,将每一行封装成一个组件,实现更灵活的数据管理。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值