当前位置: 首页 > news >正文

前端table表格,零基础入门到精通,收藏这篇就够了

1.写一个简单的table表格框架

<table> <thead class="my-table"> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.address }}</td> </tr> </tbody> </table>

table里的数据在data里返回,以下是实例数据

tableData: [ { name: "张三", age: 30, address: "北京市朝阳区" }, { name: "李四", age: 28, address: "上海市浦东新区" }, { name: "王五", age: 32, address: "广州市天河区" }, { name: "赵六", age: 25, address: "深圳市福田区" }, { name: "孙七", age: 27, address: "成都市武侯区" }, ],

这样我们就拥有了一个没有样式的基础表格

2.为了使表格更好看,我实现了如下几步:

  1. 固定表头,滚动时保留表格上面的标题

将th的 position设置为sticky,固定首行,也就是top=0,为其设置为一个区分的颜色

th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); }
  1. 将表格间的分割线去除
<table cellspacing="0" > <--...table内容--> </table>
  1. 修改表格背景颜色,字体等样式
    为了好看点还设置了隔行的颜色区分也就是 tr:nth-child(even)
th, td { padding: 20px; text-align: center; font-family: '微软雅黑',"times","courier","arial"; font-weight:"medium"; //border: 1px solid #ccc; font-size: 20px; color: #ffffff; } th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); } tr:nth-child(even) { background-color: #3c7ca7; }
  1. 自动滚动效果
    首先为了滚动效果的顺利实现,给table类增加一个外框类table-container
    增加滑动框并隐藏
.table-container { overflow-y: scroll; -webkit-overflow-scrolling: touch; } .table-container::-webkit-scrollbar { display: none; }

然后利用timer定时器和scrollTop 属性设置滚动效果,

mounted() { this.timerId = setInterval(() => { const tableContainer = this.$refs.tableContainer; const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight; tableContainer.scrollTop += rowHeight; if (tableContainer.scrollTop == lasttop) { tableContainer.scrollTop = 0; }else{ lasttop=tableContainer.scrollTop } }, 2000); this.init(); }, beforeDestroy() { // 在组件销毁前清除 setInterval clearInterval(this.timerId);

},
由于上面的滚动效果不好,看起来很卡顿,下面优化了滚动效果,加入缓动

mounted() { this.timerId = setInterval(() => { const tableContainer = this.$refs.tableContainer; const rowHeight = tableContainer.querySelector('tbody tr').offsetHeight; tableContainer.scrollTop += rowHeight/30; if (tableContainer.scrollTop == lasttop) { tableContainer.scrollTop = 0; }else{ lasttop=tableContainer.scrollTop } }, 30); this.init(); // this.tableroll(); },
  1. 最后实现效果

3.全部代码

<div class="table-container" ref="tableContainer"> <table cellspacing="0" > <thead class="my-table"> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.address }}</td> </tr> </tbody> </table> </div> export default { data() { tableData: [ { name: "张三", age: 30, address: "北京市朝阳区" }, { name: "李四", age: 28, address: "上海市浦东新区" }, { name: "王五", age: 32, address: "广州市天河区.dfasfaweorfaiadsfasd" }, { name: "赵六", age: 25, address: "深圳市福田区" }, { name: "孙七", age: 27, address: "成都市武侯区" }, ], }, .table-container::-webkit-scrollbar { display: none; } .table-container { position: absolute; left: 80%; top:70%; height: 300px; overflow-y: scroll; -webkit-overflow-scrolling: touch; background-color:rgba(18, 76, 117, 0.7); .my-table { } th, td { padding: 20px; text-align: center; font-family: '微软雅黑',"times","courier","arial"; font-weight:"medium"; //border: 1px solid #ccc; font-size: 20px; color: #ffffff; } th { background-color: #3c7ca7; font-weight: bold; position: sticky; top: 0; background-color: rgb(110, 138, 163); } tr:nth-child(even) { background-color: #3c7ca7; } }
http://www.cnnetsun.cn/news/166742.html

相关文章:

  • 如何将Open-AutoGLM操作延迟降低85%?资深架构师亲授调优心法
  • 从入门到精通:掌握Open-AutoGLM推理优化的7个关键步骤
  • 【Open-AutoGLM macOS适配终极指南】:手把手教你完成全流程配置与优化
  • 【Open-AutoGLM语义解析突破】:准确率提升35%背后的核心技术揭秘
  • 为什么顶尖团队都在用Open-AutoGLM做高效特征提取?真相曝光
  • 【AI模型预处理新纪元】:Open-AutoGLM如何实现毫秒级特征输出
  • 9 个降AI率工具,专科生必备避坑指南
  • 基于数学模型的疫苗接种策略分析
  • 基于时空图神经网络的交通流量预测方法研究
  • 【Open-AutoGLM GPU加速适配全攻略】:手把手教你7步完成高性能推理优化
  • Open-AutoGLM特征提取提速5倍:背后你不知道的3大关键技术
  • [特殊字符]工业标准文档“消化不良“?LLM+知识图谱三步翻倍表格任务F1,钢铁直男秒变逻辑大师!
  • RAG技术揭秘:如何通过检索增强生成解决大模型知识过时与幻觉问题?
  • 【国产大模型端侧落地新突破】:Open-AutoGLM推理效率提升实战
  • Open-AutoGLM端侧部署性能调优,手把手教你榨干NPU算力
  • 网络安全5大子方向!哪个才是最优选择?
  • macOS m芯片配置python低版本失败解决方案
  • 为什么顶尖工程师都在用Open-AutoGLM做任务提醒?真相曝光
  • 【Open-AutoGLM高分通过率背后的真相】:顶尖学员不愿公开的6个刷题技巧
  • 白话大模型与知识库的基础原理(不费脑版)
  • leetcode 764. Largest Plus Sign 最大加号标志
  • 对于pdf、excel、word、ppt文档如何进行有限的最有效的标注
  • Open-AutoGLM考试进入倒计时:如何用1天时间高效突击拿证?
  • RAG效果大飞跃!详解Rerank核心原理与实战,一文搞定排序优化。
  • 网络安全:是什么?新趋势?为什么值得学?一篇讲透行业红利
  • 零学AI Agent:大模型应用设计与实现全流程,附代码案例,建议收藏!
  • 春节宠物出行方案对比分析
  • 多 Agent 架构:Coordinator + Worker 模式
  • 【AI工程化进阶指南】:基于Open-AutoGLM的智能代理开发学习蓝图
  • 揭秘Open-AutoGLM智能调节系统:如何让您的家居能耗降低40%?