vue2+element-ui新增编辑表格+删除行

实现效果:

代码实现 :

javascript 复制代码
          <el-table :data="dataForm.updateData"
                    border
                    :header-cell-style="{'text-align':'center'}"
                    :cell-style="{'text-align':'center'}">
            <el-table-column label="选项字段"
                             align="center"
                             prop="name">
              <template slot-scope="scope">
                <el-form-item :prop="'updateData.' + scope.$index + '.formName'"
                              :rules="[
                                { required: true, message: '请输入', trigger: 'blur' },
                                { min: 1, max: 20, message: '长度在1到 20个字符', trigger: 'blur' }
                              ]">
                  <el-input v-model="scope.row.formName"
                            clearable></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column fixed="right"
                             label="操作">
              <template slot-scope="scope">
                <el-button @click.native.prevent="addRow(scope.$index,scope.row,dataForm.updateData)"
                           type="text"
                           size="small">
                  新增
                </el-button>
                <el-button @click.native.prevent="deleteRow(scope.$index,scope.row,dataForm.updateData)"
                           type="text"
                           size="small"
                           v-if="dataForm.updateData.length!=1">
                  移除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

<script>
export default {
      data () {
        return {
            dataForm: {
                // 自定义字段
                updateData: [
                  {
                    // id: '',
                    formName: ''
                  }
                ]
                // 其他...    
            }
        }
      },
      methods: {
    // addRow 新增 自定义字段表格行
    addRow (index, rows, item) {
      // console.log(index, rows, item)
      // this.dataForm.updateData.push({
      //   // sort: this.dataForm.updateData && this.dataForm.updateData.length > 0 ? this.dataForm.updateData.length + 1 : 1,
      //   id: null,
      //   formName: ''
      // })
      // 数组中添加新元素
      item.splice(index + 1, 0, { formName: '' })
    },
    // deleteRow 删除 自定义字段表格行
    deleteRow (index, rows, item) {
      // console.log(index, '当前行索引', rows, '删除的目标行')
      // 从index这个位置开始删除数组后的1个元素
      item.splice(index, 1)
      // this.$confirm('删除当前行, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   item.splice(index, 1)
      //   // this.delArrId.push(rows.id) // 被删除的id数组集合
      //   // rows.isDelete = 1
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除'
      //   })
      // })
    },
      }
}
</script>
相关推荐
子兮曰38 分钟前
把 649 种文件格式塞进一个网页里:一个「文件不出浏览器」的预览器
前端·javascript·后端
sm_926787054 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
IMPYLH4 小时前
HTML 的 <video> 元素
前端·html
溪语流沙5 小时前
【每天一个CSS | Day19】流光文字与一笔写成的手写签名
前端·javascript·css
逐光者9335 小时前
STM32——SPI 屏 · Flash · 高速
java·前端·stm32
To_OC5 小时前
同样是让 AI 写全栈,为什么别人一句话就够了
前端·前端框架·next.js
CDwenhuohuo5 小时前
electron pc项目打包成桌面端
前端·javascript·electron
前端大斗师5 小时前
「大于 1000」把 1000 元那单也算进去了:我在 Vue3 订单页对了 10 句话
前端·人工智能·typescript·大模型·原力计划
Jmyd01236 小时前
用 Web VR 引擎搭建 3D 虚拟展厅:从素材上云到多端实时渲染的完整数据链路
前端·3d·vr·三维数字化
刘天远6 小时前
Agent成本核算实现:事件表、状态分布与Python归集
前端·数据库·人工智能·python