el-table 单元格,双击编辑

el-table 单元格,双击编辑

实现效果

在这里插入图片描述

代码如下

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="name" label="姓名" width="180">
      <template slot-scope="scope">
        <div @dblclick="editCell(scope, 'name')" style="cursor: pointer;">
          <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'name'">
            {{ scope.row.name }}
          </span>
          <el-input
            v-else
            v-model="editingCell.value"
            @blur="saveEdit(scope.row, 'name')"
            @keydown.enter="saveEdit(scope.row, 'name')"
            size="small"
            style="width: 100px;"
          />
        </div>
      </template>
    </el-table-column>

    <el-table-column prop="age" label="年龄" width="180">
      <template slot-scope="scope">
        <div @dblclick="editCell(scope, 'age')" style="cursor: pointer;">
          <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'">
            {{ scope.row.age }}
          </span>
          <el-input
            v-else
            v-model="editingCell.value"
            @blur="saveEdit(scope.row, 'age')"
            @keydown.enter="saveEdit(scope.row, 'age')"
            size="small"
            style="width: 100px;"
          />
        </div>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25 },
        { name: 'Jane', age: 30 },
        { name: 'Doe', age: 28 },
      ],
      isEditingCell: false,
      editingCell: {
        row: null,
        field: null,
        value: null,
      },
    };
  },
  methods: {
    editCell(scope, field) {
      this.isEditingCell = true;
      this.editingCell.row = scope.row;
      this.editingCell.field = field;
      this.editingCell.value = scope.row[field]; // 将原有的值赋给编辑单元格
    },
    saveEdit(row, field) {
      row[field] = this.editingCell.value;  // 将输入的值保存回原数据
      this.isEditingCell = false;
      this.editingCell.row = null;
      this.editingCell.field = null;
      this.editingCell.value = null; // 清空编辑值
    },
  },
};
</script>

<style scoped>
/* 自定义样式 */
</style>

如果值为空无法点击无效的话,可以设置<span>的样式 style="display: inline-block ;

<span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'" style="display: inline-block ;>
            {{ scope.row.age }}
          </span>

这样就不会遇到值为空,但是无法显示的情况了

具体的内容大家可以根据实际需要调整!

要实现双击编辑 el-table 单元格并渲染 HTML 代码,可以使用 el-table 的 edit-config 属性和 edit-template 属性。以下是一个简单的示例代码: ```html <template> <el-table :data="tableData" :edit-config="{trigger: 'dblclick'}" :edit-template="getEditTemplate"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="desc" label="描述"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, desc: '<b>我是张三</b>' }, { name: '李四', age: 25, desc: '<b>我是李四</b>' }, { name: '王五', age: 30, desc: '<b>我是王五</b>' } ], editingRow: null } }, methods: { getEditTemplate({ row, column }) { if (column.property === 'desc') { return ` <div> <el-input type="textarea" v-model="row.desc"></el-input> </div> ` } } } } </script> ``` 在上面的代码中,我们将 el-table 的 edit-config 属性设置为 { trigger: 'dblclick' },表示双击单元格时进入编辑模式。然后,我们使用 el-table 的 edit-template 属性指定一个函数,用于渲染编辑单元格的模板。 在 getEditTemplate 函数中,我们检查当前单元格的属性是否为 'desc',如果是,则返回一个包含文本框的编辑模板。在模板中,我们使用 v-model 指令将文本框的值绑定到当前行的 desc 属性上。 这样,我们就实现了一个可以双击编辑 el-table 单元格并渲染 HTML 代码的组件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值