vue elementui el-table实现增加行,行内编辑修改

需求:

前端进行新增表单时,同时增加表单的明细数据。明细数据部分,可进行行编辑。

效果图:

javascript 复制代码
        <el-card>
          <div slot="header">
            <span style="font-weight: bold">外来人员名单2</span>
            <el-button
              style="float: right"
              type="primary"
              @click="insertDetailRow"
              >添加</el-button
            >
          </div>
          <el-table
            ref="detailTable"
            align="center"
            highlight-cell
            keep-source
            stripe
            border
            style="width: 100%"
            max-height="400"
            :data="detailTableData"
          >
            <el-table-column prop="userName" label="姓名" align="center">
              <template slot-scope="{ row, $index }">
                <span v-if="!showEdit[$index]">{{ row.userName }}</span>
                <el-input
                  v-model="detailTableData[$index].userName"
                  v-if="showEdit[$index]"
                  placeholder="请输入姓名"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="sex" label="性别" align="center">
              <template slot-scope="{ row, $index }">
                <span v-if="!showEdit[$index]">{{ row.sex }}</span>
                <el-select
                  v-model="detailTableData[$index].sex"
                  v-if="showEdit[$index]"
                  placeholder="请选择性别"
                >
                  <el-option
                    v-for="item in sexs"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="telPhone" label="手机号" align="center">
              <template slot-scope="{ row, $index }">
                <span v-if="!showEdit[$index]">{{ row.telPhone }}</span>
                <el-input
                  v-model="detailTableData[$index].telPhone"
                  v-if="showEdit[$index]"
                  placeholder="请输入手机号"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="220">
              <template slot-scope="{ row, $index }">
                <el-button
                  v-if="!showEdit[$index]"
                  @click="editDetailRow($index, row)"
                  type="primary"
                  icon="el-icon-edit"
                  circle
                />
                <el-button
                  v-if="showEdit[$index]"
                  @click="confirmDetailRow($index, row)"
                  type="success"
                  icon="el-icon-check"
                  circle
                />
                <el-button
                  v-if="!showEdit[$index]"
                  @click="removeDetailRow($index, row)"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                />
              </template>
            </el-table-column>
          </el-table>
        </el-card>

export default {
  data() {
    return {
      detailTableData: [],   
      showEdit: [], //控制显示及隐藏
      sexs: [{ label: '女', value: '女' }, { label: '男', value: '男' }]
    }
  },
  methods: {
    //添加一行
    insertDetailRow() {
      console.info(this.detailTableData)
      if (this.detailTableData != null && this.detailTableData.length > 0) {
        if (this.detailTableData[this.detailTableData.length - 1].userName === null ||
          this.detailTableData[this.detailTableData.length - 1].userName === undefined ||
          this.detailTableData[this.detailTableData.length - 1].userName.length <= 0) {
          this.$message({ type: 'error', message: '请将数据填写完整后再新增数据行!' })
          return false
        }
        //现有行取消编辑 
        this.detailTableData.forEach((ele, i) => {
          this.$set(this.showEdit, i, false)
        })
      }
      var obj = {
        userName: '',
        sex: '',
        telPhone: ''
      }
      this.detailTableData.push(obj)
    },
    // 编辑一行
    editDetailRow(index, row) {
      this.$set(this.showEdit, index, true)
    },
    // 确认编辑
    confirmDetailRow(index, row) {
      this.$set(this.showEdit, index, false)
    },
    //删除一行
    removeDetailRow(index, row) {
      this.$confirm('此操作将永久删除当前信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          this.detailTableData.splice(index, 1)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
相关推荐
JAVA老架构AI智能化3 分钟前
多图叠加比对
前端·javascript·chrome
ShineWinsu5 分钟前
对于HTML:HTML常见标签的解析—上
前端·面试·html·浏览器·标签·图片·超链接
百度一下吧40 分钟前
HTML5 新增标签与 Web Components 使用指南
前端·html·html5
志尊宝1 小时前
Vue3 零基础每日笔记(081):富文本编辑器集成 wangEditor 5——封装
前端·javascript·vue.js·笔记·信息可视化·echarts
deli0071 小时前
纯向量检索漏掉的召回,BM25 加向量混合能补回来吗?5 组实测
前端
志尊宝1 小时前
Vue3 零基础每日笔记(080):Vue3 + ECharts 数据可视化——封装可复用图表组件
前端·javascript·vue.js·笔记·信息可视化·echarts
阿狗童鞋1 小时前
前端工程化实战指南
前端
CappuccinoRose1 小时前
CSS 动效基础
前端·css·动画·animation·transition
用户4402818278071 小时前
我用 TRAE 一晚上从零撸了个微信投票小程序: 17 个云函数 + 11 个页面的全链路实战
前端
BullSmall1 小时前
SonarQube 9.9.8-community 适配 Jenkins 版本说明
java·前端·jenkins