el-table表格可编辑

需求:使用elementui的表格组件,实现某些列可以输入或者下拉选择修改行数据

html 复制代码
//tabClickLabel 只是作为区分是否可以修改列的条件
<el-table
                ref="table"
                :data="tableData"
                :header-cell-style="{ background: '#F5F7FA', height: '30px' }"
                style="width: 100%; margin: 0 auto"
                height="100%"
                align="center"
                stripe
                row-key="id"
                border
                :row-class-name="tableRowClassName"
                @cell-click="tabClick"
              >
                <el-table-column
                  v-for="(item, index) in viewColumns"
                  :key="index"
                  :fixed="item.fixed"
                  :prop="item.prop"
                  :align="item.align"
                  :label="item.label"
                  :min-width="item.width"
                  :show-overflow-tooltip="true"
                >
                  <template slot-scope="scope">
                    <!-- <span v-if="scope.row.index === tabClickIndex && tabLabel == item.label && tabClickLabel == '1'">
                      <el-input v-model="scope.row[item.prop]" v-focus size="small" @blur="inputBlur(scope.row)" />
                    </span> -->
                    <span v-if="scope.row.index === tabClickIndex && tabLabel == item.label && tabClickLabel == '2'">
                      <el-select v-model="scope.row[item.prop]" v-focus size="small" style="width: 100%" @change="handleChange($event, scope.row,item.prop)">
                        <el-option v-for="and in dt_stylishly" :key="and.id" :label="and.text" :value="and.id" />
                      </el-select>
                    </span>
                    <span v-else-if="scope.row.index === tabClickIndex && tabLabel == item.label && tabClickLabel == '4'">
                      <el-select v-model="scope.row[item.prop]" v-focus size="small" style="width: 100%" multiple @visible-change="(v) => handleBulr(v,scope.row)">
                        <el-option v-for="and in dt_stylishlyFour" :key="and.id" :label="and.text" :value="and.id" />
                      </el-select>
                    </span>
                    <span v-else v-html="intFormatter(scope.row[item.prop], item.prop)" />
                  </template>
                </el-table-column>
              </el-table>

tabClick方法里面处理修改数据

javascript 复制代码
    tabClick(row, column, cell, event) {
      if (this.viewColumns.some((ele) => ele.label == column.label)) {
        this.tabClickIndex = row.index
      }
      if (column.label == '部品表编号') {
        this.tabClickLabel = '0'
      } else if (column.label == '初始式样') {
        this.tabClickLabel = '2'
      } else if (column.label == '初始区域') {
         //里面的逻辑具体按照 后端需要的数据进行获取
        this.tabClickLabel = '4'
        // 这个地方给的是中文
        if (row.divType) {
          if (!Array.isArray(row.divType)) {
            let str = []
            if (row.divType.includes('/')) {
              str = row.divType.split('/')
            } else {
              str = [row.divType]
            }
            const texts = str.map(text => {
              const item = this.dt_stylishlyFour.find(obj => obj.text === text) // 查找符合条件的对象
              return item ? item.id : '' // 如果找到对象,返回其 text 字段;否则返回空字符串
            })
            row.divType = texts
          }
        }
      } else {
        this.tabClickLabel = '1'
      }
      this.tabLabel = column.label
    },
相关推荐
We་ct42 分钟前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
qq_406176141 小时前
深入浅出 Pinia:Vue3 时代的状态管理新选择
javascript·vue.js·ecmascript
wefly20171 小时前
m3u8live.cn 在线M3U8播放器,免安装高效验流排错
前端·后端·python·音视频·前端开发工具
C澒2 小时前
微前端容器标准化 —— 公共能力篇:通用打印
前端·架构
德育处主任Pro2 小时前
前端元素转图片,dom-to-image-more入门教程
前端·javascript·vue.js
木斯佳2 小时前
前端八股文面经大全:小红书前端一二面OC(下)·(2026-03-17)·面经深度解析
前端·vue3·proxy·八股·响应式
陈天伟教授2 小时前
人工智能应用- 预测新冠病毒传染性:04. 中国:强力措施遏制疫情
前端·人工智能·安全·xss·csrf
zayzy3 小时前
前端八股总结
开发语言·前端·javascript
今天减肥吗3 小时前
前端面试题
开发语言·前端·javascript
Rabbit_QL3 小时前
【前端UI行话】前端 UI 术语速查表
前端·ui·状态模式