el-table表格可编辑

8 篇文章 0 订阅
6 篇文章 0 订阅

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

//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方法里面处理修改数据

    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
    },

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 el-table 表格编辑校验,你可以使用以下方法之一: 1. 在 el-table-column 中使用 scoped slot 的方式自定义编辑组件,并在编辑组件中添加校验逻辑。例如,你可以在编辑组件的 change 事件中进行校验,并通过设置 el-form-item 的 rules 属性实现校验效果。 ```html <el-table :data="tableData"> <el-table-column label="姓名" prop="name"> <template slot-scope="scope"> <el-form-item :prop="'name' + scope.$index" :rules="[{ required: true, message: '请输入姓名', trigger: 'blur' }]"> <el-input v-model="scope.row.name" @change="handleNameChange(scope.row, scope.$index)"></el-input> </el-form-item> </template> </el-table-column> </el-table> ``` 2. 另一种方法是监听 el-table 的 row-change 事件,在事件处理函数中进行校验。你可以在该事件处理函数中获取到当前编辑行的数据,并进行校验逻辑判断。 ```html <el-table :data="tableData" @row-change="handleRowChange"> <el-table-column label="姓名" prop="name"> <template slot-scope="scope"> <el-input v-model="scope.row.name"></el-input> </template> </el-table-column> </el-table> ``` ```javascript methods: { handleRowChange(row) { // 校验逻辑判断 if (!row.name) { this.$message.error('姓名不能为空'); // 可以将错误信息保存在 row 对象的某个属性中,然后在表格中进行展示 row.errorName = '姓名不能为空'; } else { row.errorName = ''; } } } ``` 这些方法可以帮助你在 el-table 表格中实现可编辑校验的功能。根据你的实际需求,选择适合你的方法进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值