elementui的表格,单元格变成可编辑的

<template>
  <div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      :row-class-name="tableRowClassName"
      :cell-class-name="tableCellClassName"
      @cell-click="handleCellClick"
    >
      <el-table-column prop="date" label="日期" width="180" align="center">
        <template slot-scope="scope">
          <el-input
            size="small"
            ref="sortNumRef"
            @blur="inputBlur"
            v-if="
              scope.row.index === rowIndex && scope.column.index === columnIndex
            "
            v-model="scope.row.date"
          ></el-input>
          <span v-else>{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="180" align="center">
        <template slot-scope="scope">
          <el-input
            size="small"
            ref="sortNumRef"
            @blur="inputBlur"
            v-if="
              scope.row.index === rowIndex && scope.column.index === columnIndex
            "
            v-model="scope.row.name"
          ></el-input>
          <span v-else>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
        <template slot-scope="scope">
          <el-input
            size="small"
            ref="sortNumRef"
            @blur="inputBlur"
            v-if="
              scope.row.index === rowIndex && scope.column.index === columnIndex
            "
            v-model="scope.row.address"
          ></el-input>
          <span v-else>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="handleSubmit">提交</el-button>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      rowIndex: -1, //行索引
      columnIndex: -1, //列索引
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
    };
  },
  methods: {
    //把每一行的索引加到行数据中
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    //把每一列的索引加到列数据中
    tableCellClassName({ column, columnIndex }) {
      column.index = columnIndex;
    },
    handleCellClick(row, column, cell, event) {
    //   console.log(row.index, "行索引", column.index, "列索引");
      this.rowIndex = row.index;
      this.columnIndex = column.index;
      this.$nextTick(() => {
        this.$refs.sortNumRef.focus();
      });
    },
    // 失去焦点初始化
    inputBlur(row, event, column) {
      this.rowIndex = -1;
      this.columnIndex = -1;
    },
    handleSubmit() {
        console.log(this.tableData);
    },
  },
};
</script>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ElementUI表格组件中,可以通过设置 `:editable="true"` 来开启单元的可编辑模式。当单元进入编辑模式时,会显示一个输入框,用户可以在输入框中修改单元内容。用户完成编辑后,可以通过点击外部区域或按下回车键来结束编辑模式。 以下是一个使用 ElementUI 表格的可编辑单元示例代码: ```html <template> <div> <el-table :data="tableData" :editable="true"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, gender: '男' }, { name: '李四', age: 22, gender: '女' }, { name: '王五', age: 25, gender: '男' } ] } } } </script> ``` 在上面的示例中,我们设置了 `:editable="true"`,开启了表格的可编辑模式。同时,每个表格列设置了 `prop` 属性,表示该列对应数据的属性名。这样,当单元进入编辑模式时,会自动将该单元对应的数据项的属性值更新为用户输入的内容。 另外,如果需要在用户完成编辑后触发回调函数进行处理,可以使用 `@cell-edit-done` 事件来监听单元编辑完成事件,例如: ```html <el-table @cell-edit-done="handleCellEditDone"> <!-- 表格列定义 --> </el-table> ``` ```javascript methods: { handleCellEditDone(row, column, cell, event) { console.log('编辑完成,新值为:', row[column.property]) } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值