uni-table 固定表头与数据导出为xlsx

uni-table表格官方插件不支持表头固定,在原插件的基础上修改样式以达到固定表头目的,要点是在表头、表体外层分别加上 <view class="table_header"> ,<view class="table_body"> 针对这两部分写样式。

css 复制代码
	.table_header {
		position: relative;
		top: 0;
		left: 0;
		background-color: #d4d4d4;
	}
	.table_body{
		overflow: scroll;
		height: 1000rpx;
	}

以下是完整代码:

javascript 复制代码
<template>
	<view class="content">
		<!-- 表格插件 https://uniapp.dcloud.net.cn/component/uniui/uni-table.html -->
		<uni-table border stripe emptyText="暂无更多数据" >
			<!-- 表头行 -->
			<view class="table_header">
				<uni-tr>
					<uni-th align="center" width="190rpx">用户</uni-th>
					<uni-th align="center" width="300rpx">题目</uni-th>
					<uni-th align="left" width="300rpx">选项</uni-th>
				</uni-tr>
			</view>
			<!-- 表格数据行 -->
			<view class="table_body">
				<uni-tr v-for="(item,index) in dataList" :key="index">
					<uni-td> <view style="width:150rpx;text-align:center;">{{item.userName}}</view> </uni-td>
					<uni-td> <view style="width:256rpx;text-align:center;">{{item.subjectContent}}</view> </uni-td>
					<uni-td> <view style="width:256rpx;text-align:left;">{{item.optionContent}}</view> </uni-td>
				</uni-tr>
			</view>
		</uni-table>
		
		<!-- 导出按键 -->
		<view style="width: 100%; height: 150rpx;">
			<button style="width: 50%; margin-top: 20rpx;" type="primary"  @click="exportXlsx">数据导出为.xlsx</button>
		</view>
	</view>
</template>

<script>
	import XLSX from "@/static/xlsx.full.min.js"; //引入XLSX;下载链接https://cdn.sheetjs.com/
	export default {
		data() {
			return {
				baseUrl: '',
				dataList: []
			}
		},

		onLoad() {
			// 获取全局变量 baseUrl
			this.baseUrl = getApp().globalData.baseUrl;
			// 调用方法
			this.getData()
		},

		methods: {
			//从后端获取数据
			getData() {
				uni.request({
					url: this.baseUrl + 'queryAnswer',
					method: "GET",
					data: {},
					success: (res) => {
						let arr = res.data.dataArr
						// console.log(arr)
						this.dataList = arr
					},
					fail: () => {
						uni.showToast({
							title: "网络请求失败!",
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			
			// 数据导出为xlsx
			exportXlsx() {
				// 数据
				let dataList = this.dataList
				let sheet = []
				// 表头
				let title = ['用户', '题目','选项']
				sheet.push(title)
				
				// 从源数组循环提取需要的字段追加到新数组
				dataList.forEach(item => {
					let rowcontent = []
					rowcontent.push(item.userName)
					rowcontent.push(item.subjectContent)
					rowcontent.push(item.optionContent)
					sheet.push(rowcontent)
				})
				
				// XLSX插件使用
				var worksheet = XLSX.utils.aoa_to_sheet(sheet);
				var workbook = XLSX.utils.book_new();
				XLSX.utils.book_append_sheet(workbook, worksheet, "导出信息");
				
				// H5导出
				XLSX.writeFile(workbook, '导出.xlsx');
				
			}
			
		}
	}
</script>

<style>
	.content{
		width: 100%;
	}	
	.table_header {
		position: relative;
		top: 0;
		left: 0;
		background-color: #d4d4d4;
	}
	.table_body{
		overflow: scroll;
		height: 1000rpx;
	}
</style>
相关推荐
kyle~2 小时前
C++--- override 关键字 强制编译器验证当前函数是否重写基类的虚函数
java·前端·c++
Light602 小时前
像素退场,曲线登场:现代响应式 CSS 全家桶 | 领码课堂
前端·css·响应式设计·css函数·布局系统·相对单位·设计令牌
爱生活的苏苏3 小时前
elementUI 表单验证-联动型校验
前端·javascript·elementui
一只小风华~5 小时前
Vue Router 路由元信息(meta)详解
前端·javascript·vue.js
*且听风吟5 小时前
html 实现鼠标滑动点亮横轴
前端·javascript·html
iCoding917 小时前
前端分页 vs 后端分页:技术选型
前端·后端·系统架构
mingtianyihou337 小时前
使用 Service Worker 限制请求并发数
前端
张可爱7 小时前
20251017-Vue2八股文整理(上篇)
前端
FanetheDivine7 小时前
ts中如何描述一个复杂函数的类型
前端·typescript
lightgis7 小时前
chrome中的axure插件提示无法不受支持
前端·chrome