el-table雙擊編輯單格-出現(xiàn)input框

效果如下:
1.png
2.png
3.png
代碼:
<template>
  <div class="app-container">
    <el-button
      type="primary"
      icon="el-icon-check"
      v-if="saveBoolen"
      @click="handleSave"
      >保存</el-button
    >
    <el-table border stripe v-loading="loading" :data="list">
      <el-table-column label="名稱" prop="name">
        <template slot-scope="scope">
          <!-- 主要代碼 start -->
          <div @dblclick="chengenum(scope)">
            <el-input
              ref="saveTableInput"
              v-if="
                tableRowIndex == scope.$index &&
                  tableColumnIndex == scope.column.id
              "
              v-model="scope.row.name"
              @blur="onInputTableBlur"
            ></el-input>
            <span v-else>{{ scope.row.name }}</span>
            <!-- 主要代碼 end -->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="字符" prop="key">
        <template slot-scope="scope">
          <div @dblclick="chengenum(scope)">
            <el-input
              ref="saveTableInput"
              v-if="
                tableRowIndex == scope.$index &&
                  tableColumnIndex == scope.column.id
              "
              v-model="scope.row.key"
              @blur="onInputTableBlur"
            ></el-input>
            <span v-else>{{ scope.row.key }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList, save } from "./api";

export default {
  data() {
    return {
      // 表格行
      tableRowIndex: undefined,
      // 表格列
      tableColumnIndex: undefined,
      // 保存按鈕顯現(xiàn)
      saveBoolen: false,
      loading: true,
      list: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 雙擊處理表格input框 
    chengenum(scope) {
      this.saveBoolen = true;
      // 找到單個(gè)格子獨(dú)有的屬性 - 這里是用所在行跟列id區(qū)別顯示
      this.tableRowIndex = scope.$index;
      this.tableColumnIndex = scope.column.id;
      // 獲取input框焦點(diǎn) - 為了能進(jìn)行行列清空操作(不然無法精準(zhǔn)定位)
      this.$nextTick(() => {
        this.$refs.saveTableInput.$refs.input.focus();
      });
    },
    // 取消焦點(diǎn) 選中行列清空
    onInputTableBlur() {
      this.tableRowIndex = undefined;
      this.tableColumnIndex = undefined;
    },
    // 保存修改的列表
    handleSave() {
      save(this.list).then(res => {
        this.getList();
        this.saveBoolen = false;
        this.onInputTableBlur();
        this.msgSuccess("保存成功");
      });
    },
    // 查詢列表
    getList() {
      this.loading = true;
      getList().then(res => {
        this.list = res.rows;
        this.loading = false;
      });
    }
  }
};
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容