elementui 表格鼠标点击单元格编辑,鼠标离开则保存

<template>
  <div class="app-container">
    <div class="box-card" ref="top" style="border-bottom: 1px solid #eee">
      <el-form :inline="true" size="mini" ref="form" :model="pageFilter">
        <el-form-item label="日期">
            <el-date-picker
                v-model="dateValue"
                type="date"
                size="small"
                placeholder="选择日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd">
            </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button round size="mini" icon="el-icon-search" @click="onSearch"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            round
            size="mini"
            icon="el-icon-refresh-left"
            @click="onReset"
            >重置</el-button
          >
        </el-form-item>
        <el-form-item>
            <el-button round size="mini" icon="el-icon-setting" @click="setIndex()">设置指标</el-button>
        </el-form-item>
      </el-form>
    </div>
      <el-card class="box-card" style="margin-bottom: 15px" shadow="always">
          <el-table  style="width: 100%;margin-top: 8px;margin-bottom: 15px" v-loading="listLoading"  :height="tableHeight" :data="tableData" row-key="index" element-loading-text="拼命加载中" border fit highlight-current-row  >
              <template v-for="item in tablelist" :key>
                  <el-table-column
                      align="center"
                      show-overflow-tooltip
                      :width="120"
                      :label="item.label"
                  >
                      <template slot-scope="scope"><span :class="item.class ? item.class : scope.row.class">{{ scope.row[item.value] }}</span></template>
                  </el-table-column>
              </template>
          </el-table>
      </el-card>
      <el-dialog title="设置指标" :visible.sync="dialogTableVisible" :close-on-click-modal="false" @open="open">
          <el-table ref="myTable" :data="gridData" :cell-style="{ textAlign: 'center' }" :span-method="objectSpanMethod" :header-cell-style="{textAlign: 'center'}"  stripe border highlight-current-row @cell-mouse-leave="loseFcous">
              <el-table-column property="chargePerson" label="" width="150"></el-table-column>
              <el-table-column property="region" label="大区" width="150"></el-table-column>
              <el-table-column property="campus" label="校区" width="200"></el-table-column>
              <el-table-column property="dailyOperationIndex" label="日经营A指标">
                  <template slot-scope="scope">
                      <div @click.stop="changeNum(scope.$index,'editDailyOperationIndex')">
                          <el-input v-if="editable[scope.$index]" v-model="scope.row.dailyOperationIndex" size="mini" class="editDailyOperationIndex" @change="changeInput(scope.row)"/>
                          <span v-else>{{ scope.row.dailyOperationIndex?scope.row.dailyOperationIndex:0 }}</span>
                      </div>
                  </template>
              </el-table-column>
              <el-table-column property="monthlyMemberIndex" label="月会员指标">
                  <template slot-scope="scope">
                      <div @click.stop="changeNumMonthly(scope.$index,'editMonthlyMemberIndex')">
                          <el-input v-if="editableMonthly[scope.$index]" v-model="scope.row.monthlyMemberIndex" size="mini" class="editMonthlyMemberIndex" @change="changeInput(scope.row)"/>
                          <span v-else>{{ scope.row.monthlyMemberIndex?scope.row.monthlyMemberIndex:0 }}</span>
                      </div>
                  </template>
              </el-table-column>
          </el-table>
      </el-dialog>
    </div>
</template>

<script>
import { listSignTarget2,getSchoolsRelation, todaySign } from "@/api/report";
import { getRosterId } from "@/utils/auth";
export default {
  data() {
    return {
      dateValue: null,
      tableHeight: 300,
      listLoading: false,
      tableData: [],
      pageFilter: {
        loginId: getRosterId()
      },
      tablelist: [
        {
          value: "deptName",
          label: "网招部",
          class: "none",
        },
        {
          value: "rosterName",
          label: "总负责人",
          class: "none",
        },
        {
          value: "rosterName",
          label: "区域",
          class: "none",
        },
        {
          value: "rosterName",
          label: "校区",
          class: "none",
        },
        {
          value: "rosterName",
          label: "来校",
          class: "none",
        },
        {
          value: "rosterName",
          label: "当日报名",
          class: "none",
        },
        {
          value: "rosterName",
          label: "网汇",
          class: "none",
        },
        {
          value: "rosterName",
          label: "汇款人数",
          class: "none",
        },
        {
          value: "rosterName",
          label: "明天到校",
          class: "none",
        },
      ],
      top: "",
      dialogTableVisible:false,
        gridData: [{
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }],
        saveItem: {}, // 点击单元格可编辑
        editable: [], // 点击单元格可编辑
        editableMonthly: [], // 点击单元格可编辑
    };
  },
  mounted() {
    this.$nextTick((h) => {
      this.top = this.$refs.top.clientHeight + "px";
      this.tableHeight=window.innerHeight-200;
        window.onresize = () =>{
            this.tableHeight=window.innerHeight-200;
        }
    });
    this.loadData();
  },
  methods: {
      loseFcous() {
          this.editable = new Array(this.$refs.myTable.data.length);
          this.editable.forEach(value => value = false);
          this.editableMonthly = new Array(this.$refs.myTable.data.length);
          this.editableMonthly.forEach(value => value = false);
      },
      changeInput(val) {
          console.log(val)
          // this.$ajax.post('bom/upDateMemo ', this.saveItem
          // ).then((res) => {
          //     this.$message({ message: res.getMessage(), type: 'success' });
          // });
      },
      changeNum(index, className) {
          // 设置是否可以编辑
          this.editable = new Array(this.$refs.myTable.data.length);
          this.editable[index] = true;
          this.$set(this.editable, index, true);
          // 让input自动获取焦点
          this.$nextTick(function() {
              let editInputList = document.getElementsByClassName(className);
              editInputList[0].children[0].focus();
          });
      },
      changeNumMonthly(index, className) {
          // 设置是否可以编辑
          this.editableMonthly = new Array(this.$refs.myTable.data.length);
          this.editableMonthly[index] = true;
          this.$set(this.editableMonthly, index, true);
          // 让input自动获取焦点
          this.$nextTick(function() {
              let editInputList = document.getElementsByClassName(className);
              editInputList[0].children[0].focus();
          });
      },
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
          if (columnIndex === 0) {
              return {
                  rowspan: row.rowspan,
                  colspan: 1
              }
          }
          if (columnIndex === 1) {
              return {
                  rowspan: row.rowspan1,
                  colspan: 1
              }
          }
      },
      open(){
          getSchoolsRelation().then((response) => {
              this.gridData = response.data;
              // 先给所有的数据都加一个v.rowspan = 1
              this.gridData.forEach(v => {
                  v.rowspan = 1;
                  v.rowspan1 = 1;
              })
              // 双层循环
              for (let i = 0; i < this.gridData.length; i++) {
                  // 内层循环,上面已经给所有的行都加了v.rowspan = 1
                  // 这里进行判断
                  // 如果当前行的id和下一行的id相等
                  // 就把当前v.rowspan + 1
                  // 下一行的v.rowspan - 1
                  for (let j = i + 1; j < this.gridData.length; j++) {
                      if (this.gridData[i].chargePerson === this.gridData[j].chargePerson) {
                          this.gridData[i].rowspan++
                          this.gridData[j].rowspan--
                      }
                  }
                  // 这里跳过已经重复的数据
                  i = i + this.gridData[i].rowspan - 1;
              }
              for (let i = 0; i < this.gridData.length; i++) {
                  // 内层循环,上面已经给所有的行都加了v.rowspan = 1
                  // 这里进行判断
                  // 如果当前行的id和下一行的id相等
                  // 就把当前v.rowspan + 1
                  // 下一行的v.rowspan - 1
                  for (let j = i + 1; j < this.gridData.length; j++) {
                      if (this.gridData[i].region === this.gridData[j].region) {
                          this.gridData[i].rowspan1++
                          this.gridData[j].rowspan1--
                      }
                  }
                  // 这里跳过已经重复的数据
                  i = i + this.gridData[i].rowspan1 - 1;
              }
          });
      },
      setIndex(){
        this.dialogTableVisible=true
      },
    /**
     * 查找
     */
    onSearch() {
      this.loadData();
    },
    /**
     * 重置
     */
    onReset() {
      this.dateValue = '';
      this.onSearch();
    },

    loadData() {
      this.listLoading = true;
      listSignTarget2(this.pageFilter).then((response) => {
        this.tableData = response.data.a;
        this.tableDatas = response.data.b;
        this.tableData.map((item) => {
          if (
            item.arriveTargetNum &&
            item.arriveCount &&
            item.arriveTargetNum < item.arriveCount
          ) {
            item.class = "reds";
          } else {
            item.class = "grue";
          }
        });
        this.tableDatas.map((item) => {
          if (
            item.arriveTargetNum &&
            item.arriveCount &&
            item.arriveTargetNum < item.arriveCount
          ) {
            item.class = "reds";
          } else {
            item.class = "grue";
          }
        });
        this.listLoading = false;
      });
    },
  },
};
</script>
<!--样式参考:https://www.cnblogs.com/goloving/p/9119460.html-->
<style lang="scss">
.el-table__header tr,
.el-table__header th {
    padding: 4px 0;
    height: 20px;
}
.el-table__body tr,
.el-table__body td {
    padding: 4px 0;
    height: 10px;
}
</style>

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]) } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值