element-plus表格上下键滚动

文章介绍了在Vue应用中使用ElementUI的el-table组件展示数据,包括如何实现行级别的删除操作以及响应键盘事件(如上下箭头)自动滚动到选中行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <div>
    <el-table
      ref="singleTableRef"
      width="200px"
      :data="tableData"
      highlight-current-row
      height="500"
    >
      <el-table-column label="姓名" prop="name"></el-table-column>
      <!-- 其他列定义 -->
      <el-table-column>
        <template #scope>
          <el-table-column label="操作">
            <el-button @click="deleteRow(scope.$index)">删除</el-button>
          </el-table-column>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'xiaodong1' },
        { name: 'xiaodong2' },
        { name: 'xiaodong3' },
        { name: 'xiaodong4' },
        { name: 'xiaodong5' },
        { name: 'xiaodong6' },
        { name: 'xiaodong7' },
        { name: 'xiaodong8' },
        { name: 'xiaodong9' },
        { name: 'xiaodong10' },
        { name: 'xiaodong11' },
        { name: 'xiaodong12' },
        { name: 'xiaodong13' },
        { name: 'xiaodong14' },
        { name: 'xiaodong15' },
        { name: 'xiaodong16' },
        { name: 'xiaodong17' },
        { name: 'xiaodong18' },
        { name: 'xiaodong19' },
        { name: 'xiaodong20' },
        { name: 'xiaodong21' },
        { name: 'xiaodong22' },
        { name: 'xiaodong23' },
      ], // 表格数据
      selectedRowIndex: 0, // 当前选中行索引
    }
  },
  mounted() {
    document.addEventListener('keydown', this.handleKeyDown)
    this.$refs.singleTableRef.setCurrentRow(this.tableData[0])
  },
  methods: {
    handleKeyDown(event) {
      // 更新选中行索引
      if (event.key === 'ArrowUp') {
        // 向上箭头键,选中上一行
        if (this.selectedRowIndex > 0) {
          this.selectedRowIndex--
        } else {
          this.selectedRowIndex = this.tableData.length - 1
        }
        // // 滚动到选中行
        const rowElement = document.querySelector(
          `.el-table__row:nth-child(${this.selectedRowIndex + 1})`,
        )
        this.$refs.singleTableRef.setCurrentRow(this.tableData[this.selectedRowIndex])

        rowElement.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'smooth' })
      } else if (event.key === 'ArrowDown') {
        // 向下箭头键,选中下一行
        if (this.selectedRowIndex < this.tableData.length - 1) {
          this.selectedRowIndex++
        } else {
          this.selectedRowIndex = 0
        }
        // // 滚动到选中行
        const rowElement = document.querySelector(
          `.el-table__row:nth-child(${this.selectedRowIndex + 1})`,
        )
        this.$refs.singleTableRef.setCurrentRow(this.tableData[this.selectedRowIndex])
        rowElement.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'smooth' })
      }
    },
  },
}
</script>
<style lang="scss">

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值