el-table点击单元格可编辑

需求:表格的每一个单元格点击都可编辑
el-table点击单元格可编辑_第1张图片

<template>
  <div>
    <el-table
      @cell-click="cellClick"
      :row-class-name="tableRowClassName"
      :cell-class-name="tableCellClassName"
      :data="tableData"
      style="width: 500px"
      border
    >
      <el-table-column prop="weightName" label="权重项">
        <template slot-scope="{ row, column }">
          <el-input
            v-if="row.index == rowIndex && column.index == columnIndex"
            v-model="row.weightName"
            @blur="inputBlur"
          >el-input>
          <span v-else>{{ row.weightName }}span>
        template>
      el-table-column>
      <el-table-column prop="weightPer" label="比例">
        <template slot-scope="{ row, column }">
          <el-input
            v-if="row.index == rowIndex && column.index == columnIndex"
            v-model.number="row.weightPer"
            type="number"
            @blur="inputBlur(row)"
          >el-input>
          <span v-else>{{ row.weightPer }}span>
        template>
      el-table-column>
    el-table>
  div>
template>

<script>
export default{
	data() {
	    return {
	      tableData: [],
	      rowIndex: -1, //行索引
	      columnIndex: -1, //列索引
	    };
	  },
	 methods:{
	 	tableRowClassName({ row, rowIndex }) {
	      // 把每一行的索引放到row里
	      row.index = rowIndex;
	    },
	    tableCellClassName({ column, columnIndex }) {
	      // 把每一列的索引放到column里
	      column.index = columnIndex;
	    },
	    cellClick(row, column) {
	      this.rowIndex = row.index;
	      this.columnIndex = column.index;
	    },
	    // 失去焦点
	    inputBlur() {
	      this.rowIndex = -1;
	      this.columnIndex = -1;
	    },
	  }
}
script>

你可能感兴趣的:(vue,vue.js,前端,javascript)