<…">
当前位置:首页 > 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) {
      // 滚动到底部加载更多数据
    }
  }
}

响应式表格高度

动态计算表格容器高度:

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

vue实现table表格滚动

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

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock-…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身:…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery…