elementui中表格嵌套了input框,数据量很大的情况下,输入框输入会卡顿

elementui中的表格嵌套了太多的input框就会卡顿,导致性能下降;
所以首次渲染可以全部渲染span,当点击某个span的时候再变成input框,这样可以提高一些性能;
设置两个事件:
row-class-name: 点击行的时候触发,可以拿到row和rowIndex,可以把当前行的索引设置给行的对象数据中;
@cell-click: 点击某个单元格时触发;
先定义两个变量:
clickCellIndex: 记录被点击的是哪一行
clickCellLabel: 记录被点击的是哪一列
然后input框中判断:scope.row.index === clickCellIndex && scope.column.label == clickCellLabel来决定是否渲染;
完整代码:

<template>
  <div>
    <el-button type="primary" @click="add">增行2000</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      height="500"
      border
      @cell-click="handleRowClick"
      :row-class-name="tableRowClassName"
    >
      <el-table-column prop="date" label="日期">
        <template slot-scope="scope">
          <el-input
            v-if="
              scope.row.index === clickCellIndex &&
              scope.column.label == clickCellLabel
            "
            v-model="scope.row.date"
            placeholder="请输入内容"
            @blur="inputBlur(scope.row)"
          ></el-input>
          <span v-else>111</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名">
        <template slot-scope="scope">
          <el-input
            v-if="
              scope.row.index === clickCellIndex &&
              scope.column.label == clickCellLabel
            "
            v-model="scope.row.name"
            placeholder="请输入内容"
            @blur="inputBlur(scope.row)"
          ></el-input>
          <span v-else>111</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址">
        <template slot-scope="scope">
          <el-input
            v-if="
              scope.row.index === clickCellIndex &&
              scope.column.label == clickCellLabel
            "
            v-model="scope.row.address"
            placeholder="请输入内容"
            @blur="inputBlur(scope.row)"
          ></el-input>
          <span v-else>111</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      clickCellIndex: null,
      clickCellLabel: ""
    }
  },
  methods: {
    add() {
      for (let i = 0; i < 3000; i++) {
        let obj = {
          date: '',
          name: '',
          address: ''
        }
        this.tableData.push(obj)
      }
    },
    tableRowClassName({ row, rowIndex, columnIndex, column }) {
      // 把每一行的索引放进row
      row.index = rowIndex;
    },
    handleRowClick(row, column) {
      this.clickCellIndex = row.index;
      this.clickCellLabel = column.label;
    },
    inputBlur(row) {
      this.clickCellIndex = null;
      this.clickCellLabel = "";
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 0px;
}
</style>

效果如下:
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值