<…">
当前位置:首页 > 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 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…