<…">
当前位置:首页 > VUE

vue实现table表格滚动

2026-01-23 01:38:54VUE

实现固定表头滚动

使用CSS固定表头,表格内容区域滚动:

<template>
  <div class="scroll-table-container">
    <table>
      <thead>
        <tr>
          <th v-for="col in columns" :key="col.key">{{ col.title }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, index) in data" :key="index">
          <td v-for="col in columns" :key="col.key">{{ row[col.key] }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped>
.scroll-table-container {
  height: 400px;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead {
  position: sticky;
  top: 0;
  background: white;
  z-index: 10;
}

th, td {
  padding: 8px 16px;
  border: 1px solid #ddd;
}
</style>

使用第三方组件库

Element UI的el-table组件自带滚动功能:

<template>
  <el-table
    :data="tableData"
    height="400"
    style="width: 100%">
    <el-table-column
      prop="date"
      label="日期"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
  </el-table>
</template>

横向滚动实现

为表格容器添加横向滚动条:

.table-container {
  width: 100%;
  overflow-x: auto;
  white-space: nowrap;
}

虚拟滚动优化性能

对于大数据量表格,使用vue-virtual-scroller:

<template>
  <RecycleScroller
    class="scroller"
    :items="data"
    :item-size="50"
    key-field="id">
    <template v-slot="{ item }">
      <div class="row">
        <div v-for="col in columns" :key="col.key">
          {{ item[col.key] }}
        </div>
      </div>
    </template>
  </RecycleScroller>
</template>

<style>
.scroller {
  height: 400px;
}

.row {
  display: flex;
  border-bottom: 1px solid #eee;
  height: 50px;
  align-items: center;
}

.row > div {
  flex: 1;
  padding: 0 10px;
}
</style>

自定义滚动事件监听

通过监听滚动事件实现特殊效果:

mounted() {
  const tableBody = document.querySelector('.table-container tbody')
  tableBody.addEventListener('scroll', this.handleScroll)
},
methods: {
  handleScroll(event) {
    const { scrollTop, clientHeight, scrollHeight } = event.target
    if (scrollHeight - scrollTop === clientHeight) {
      // 滚动到底部加载更多数据
    }
  }
}

响应式表格高度

动态计算表格容器高度:

vue实现table表格滚动

computed: {
  tableHeight() {
    return window.innerHeight - 200 // 减去页眉页脚等固定高度
  }
},
mounted() {
  window.addEventListener('resize', this.handleResize)
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize)
}

标签: 表格vue
分享给朋友:

相关文章

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件: <…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…