elementui 双击el-table表格展示输入框修改数据

参考:element ui 双击表格展示输入框 修改数据_7ing酒的博客-优快云博客_双击输入框展示: 功能:就是一个简单的table表格 通过双击某一项 进行修改。(其实就是双击之后变成一个input框。。。)实现:1. 在table标签里添加3个方法。 @cell-dblclick="tabClick" :row-class-name="tableRowClassName" :cell-class-name="tableCellClassName"cell-dblclick :当某个单元格被双击击时会触发该事件...https://blog.youkuaiyun.com/TingLisu/article/details/119646500 

 main.js中全局注册指令:

参考:https://blog.youkuaiyun.com/qq_40323256/article/details/125963233https://blog.youkuaiyun.com/qq_40323256/article/details/125963233

// 注册一个全局自定义指令 `v-focus`
Vue.directive('focus', {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: function (el) {
    // 聚焦元素
    // el.focus()
    el.querySelector('input').focus()
  }
})

实现代码:

  <!--main-->
  <template>
  <div class="main">
    <el-table
      :data="tableData"
      style="width: 100%"
      @cell-dblclick="tabClick"
      :row-class-name="tableRowClassName"
      :cell-class-name="tableCellClassName"
    >
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column label="地址" show-overflow-tooltip>
        <template slot-scope="scope">
          <div
            v-if="
              scope.row.index === clickRowIndex &&
              scope.column.index === clickCellIndex
            "
          >
            <el-input
              v-model="scope.row.address"
              @blur="inputBlur"
              size="mini"
              v-focus
            />
          </div>
          <div v-else>{{ scope.row.address }}</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
      ],
      clickRowIndex: null, // 当前点击的行索引
      clickCellIndex: null, // 当前点击的列索引
      tabClickLabel: "", // 当前点击的列名
    };
  },
  props: {},
  created() {},
  mounted() {},
  computed: {},
  methods: {
    // 控制input显示 row 当前行 column 当前列
    tabClick(row, column, cell, event) {
      console.log("row:", row);
      console.log("column:", column);
      console.log("column.index:", column.index);
      console.log("column.label:", column.label);

      this.clickRowIndex = row.index;
      this.clickCellIndex = column.index;
      this.tabClickLabel = column.label;
    },
    // 失去焦点初始化
    inputBlur(row, event, column) {
      this.clickRowIndex = null;
      this.clickCellIndex = null;
      this.tabClickLabel = "";

      console.log("this.tableData:", this.tableData);
    },
    // 把每一行的索引放进row
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },

    // 把每一列的索引放进column
    tableCellClassName({ column, columnIndex }) {
      column.index = columnIndex;
    },
  },
};
</script>
    

### 实现 `el-table` 双击编辑单元格功能 为了实现在 Element UI 的 `el-table` 中通过双击来编辑单元格的功能,可以通过监听单元格的双击事件并切换其状态,在编辑模式下显示输入框或其他表单控件。下面是一个具体的实现方法: #### HTML 部分 创建一个基本结构用于展示表格,并为每个 `<td>` 定义模板。 ```html <template> <div id="app"> <el-table :data="tableData" @cell-dblclick="handleCellDBLClick"> <!-- 假设有一个名为 name 的列 --> <el-table-column prop="name" label="姓名"></el-table-column> <!-- 动态生成其他列 --> ... <!-- 自定义列模板以便处理编辑逻辑 --> <el-table-column v-for="(column, index) in columns" :key="index" :prop="column.prop" :label="column.label"> <template slot-scope="scope"> <span v-if="!editingCells[`${scope.$index}-${column.prop}`]">{{ scope.row[column.prop] }}</span> <el-input v-else v-model="scope.row[column.prop]" @blur="saveEdit(scope.$index, column.prop)" ref="editInput"></el-input> </template> </el-table-column> </el-table> </div> </template> ``` #### JavaScript (Vue.js 组件) 在 Vue 组件内部管理数据和行为逻辑,包括记录哪些单元格处于编辑状态以及保存更改后的值。 ```javascript <script> export default { data() { return { tableData: [ { date: '2016-05-03', name: 'Tom' }, { date: '2016-05-02', name: 'John' } ], editingCells: {}, // 存储正在被编辑的状态对象 columns: [{ prop: 'date', label: '日期' }, { prop: 'name', label: '姓名' }] }; }, methods: { handleCellDBLClick(row, column, cell, event) { const key = `${row.index}-${column.property}`; this.editingCells[key] = true; this.$nextTick(() => { this.$refs['editInput'][0].focus(); }); }, saveEdit(rowIndex, colProp) { delete this.editingCells[rowIndex + '-' + colProp]; } } }; </script> ``` 上述代码展示了如何利用 VueElement UI 来构建具有双击编辑特性的表格[^3]。当用户双击某个单元格时,会触发相应的函数使该位置变为可编辑状态;完成编辑后失去焦点,则自动更新视图并将编辑标志移除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值