Vue 用原生table写一个,表格数据能够自动滚动的组件

本文介绍了一位开发者如何使用JavaScript和CSS创建一个美观且可滚动的大屏表格,避免了第三方UI库的局限性,通过动态控制表格内容的显示和延迟动画效果。

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

问题描述:

最近在开发一个大屏数据显示,需要一个表格,能够上下滚动显示数据。发现用第三方的UI库实现起来不太好看。所以自己动手写。

在这里插入图片描述

效果就是红色框圈起来的部分,表头信息不动,然后表格内容上下滚动

方法实现:

HTML部分

// 我这边的数据是后端返回的,具体情况可以自己看着配置
<div class="table-container">
  <table class="table">
  	// 这个是表头部分的设置,
     <thead>
       <th
         v-for="head in data[0].TodaysPromotionInfoList[index]
           .TodaysPromotionInfoTableHead"
         :key="head.Field"
       >
         {{ head.Descriptions }}
       </th>
     </thead>
     
     // 这个地方是表格数据部分
     // 在这里我根据数组的长度做了判断,当数组长超过2的时候,添加这个滚动的动画
     // 而且为了显示效果好看,我用js部分做了控制,页面刚显示时,动画会在2秒之后执行,动画时间设置了15秒,每次循环,暂停2秒
     <tbody
       :class="{
         scroll:
           data[0].TodaysPromotionInfoList[index]
             .TodaysPromotionInfo.length > 2 && isAnimating,
       }"
     >
     	// 设置行数
       <tr
         v-for="i in data[0].TodaysPromotionInfoList[index]
           .TodaysPromotionInfo.length"
         :key="i"
         class="trAnimation"
       >
       	// 设置每行每个单元格
         <td
           v-for="(value, key) in data[0].TodaysPromotionInfoList[
             index
           ].TodaysPromotionInfo[i - 1]"
           :key="key"
         >
           {{ value }}
         </td>
       </tr>
     </tbody>
   </table>
 </div>

js部分:

export default {
	// 父组件传来的数据
  props: ["midTopBottomDataWatch"],
  watch: {
    midTopBottomDataWatch(newVal) {
      if (newVal) {
        this.data = this.midTopBottomDataWatch;
        // 设置动画每次循环暂停2秒之后再执行
        this.startAnimation();
      }
    },
  },
  data() {
    return {
      data: [],
      isAnimating: false,
    };
  },
  methods: {
    startAnimation() {
      this.isAnimating = true;
      this.animate();
    },
    animate() {
      setTimeout(() => {
        this.isAnimating = false;
        setTimeout(() => {
          this.isAnimating = true;
          this.animate();
        }, 2000); // 设置停止时间为2秒
      }, 17000); // 动画持续时间为2秒
    },
  },
};

css部分:


.table-container {
  width: 95%;
  margin: 0 auto;
  height: calc(100% - 50px);
  overflow: hidden;
  position: relative;
}

.table {
  width: 95%;
  margin: 0 auto;
  border-collapse: separate;
  border-spacing: 0;
}

.table thead th {
  position: sticky;
  top: 0;
  background-color: #3a8ee6;
  z-index: 2000;
}

.table td {
  border: 1px solid #ccc;
  padding: 8px;
}

.scroll {
  animation: scrollTable 15s linear infinite;
  animation-delay: 2s;
}

@keyframes scrollTable {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-100%);
  }
}

因为在CSS动画中,animation-delay属性只会在动画开始之前设置延迟时间,而不会在每次循环之后重新应用延迟时间。所以,我这边使用js控制类的添加和移出了控制动画每次的显示效果

Vue中,让原生表格实现无限滚动通常需要结合虚拟滚动(Infinite Scroll)技术。以下是一种常见的做法: 1. 安装引入库:首先安装`vue-infinite-loading`这个插件,它可以帮助我们处理分页无限滚动的效果。在项目中添加依赖: ```bash npm install vue-infinite-loading ``` 2. 使用组件:将`vue-infinite-loading`与表格结合,创建一个新的Vue组件,比如`InfiniteTable.vue`: ```html <template> <div> <table :data="tableData" v-infinite-loading="isLoading"> <!-- 表格内容 --> </table> <div v-if="isLoading"> <!-- 加载更多提示 --> </div> </div> </template> <script> import { InfiniteLoading } from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, data() { return { tableData: [], isLoading: false, pageSize: 10, // 每次加载的数据量 currentPage: 1, }; }, methods: { loadMore() { this.isLoading = true; // 设置为加载状态 // 模拟异步加载数据 setTimeout(() => { const newData = // 从服务器获取新的数据 this.tableData.push(...newData); if (newData.length < pageSize) { this.isLoading = false; // 数据加载完毕后设置为非加载状态 } }, 2000); }, }, }; </script> ``` 3. 监听滚动事件:在`mounted`生命周期钩子里,监听表格滚动事件,当用户滚动到底部时触发`loadMore`方法。 4. 添加滚动监听: ```javascript mounted() { window.addEventListener('scroll', this.handleScroll); }, beforeDestroy() { window.removeEventListener('scroll', this.handleScroll); }, methods: { handleScroll() { const isBottom = document.documentElement.scrollTop + document.documentElement.clientHeight === Math.max(document.documentElement.scrollHeight, window.innerHeight); if (isBottom && !this.isLoading) { this.loadMore(); } }, }, ``` 现在,当表格滚动到页面底部时,会自动加载更多数据,并显示加载中提示。记得替换`loadMore`方法中的数据获取部分为实际的网络请求API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值