uniapp 实现表格滚动、固定列

1.用table的样式写,推荐用这种写比较方便

<template>
	<view>
		<h3 style="text-align: center;">用 table 样式写</h3>
		<view style="margin: 20upx;">
			<scroll-view scroll-x="true" scroll-y="true"  
				style="border: 1upx solid #eee; width: 100%;height: 400upx;" @scrolltolower="lower">
				<view class="table" :style="[{'width':tableWidth()}]">
					<!-- 表头 -->
					<!-- 固定列样式用title-cell-left 或 title-cell-right -->
					<view class="table-header">
						<view :class="item1.type" :style="[{'width':item1.width + 'upx'}]"  v-for="(item1,index1) in headerList" :key="index1">
							<view class="cell ">{{item1.title}}</view>
						</view>
					</view>
					<!-- 数据行 -->
					<view class="table-body">
						<view class="table-tr" v-for="(item2,index2) in dataList" :key="index2">
							<view :class="headerList[0].type" :style="[{'width':headerList[0].width + 'upx'}]">
								<view class="cell ">{{item2.id}}</view>
							</view>
							<view :class="headerList[1].type" :style="[{'width':headerList[1].width + 'upx'}]">
								<view class="cell " style="text-align: center;">{{item2.name}}</view>
							</view>
							<view :class="headerList[2].type" :style="[{'width':headerList[2].width + 'upx'}]">
								<view class="cell ">{{item2.address}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headerList:[
					{
						title:'id',
						width: 100,
						type:'title-cell-left'
					},
					{
						title:'姓名',
						width: 200,
						type:'content-cell'
					},
					{
						title:'地址',
						width: 600,
						type:'content-cell'
					}
				],
				dataList:[
					{
						id:'001',
						name:'张三',
						address:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'002',
						name:'张三',
						address:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'003',
						name:'张三',
						address:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'004',
						name:'张三',
						address:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
					}
				],
				
			}
		},
		created() {
			this.tableWidth()
		},
		methods: {
			//计算表格宽度
			tableWidth() {
				let width = 0;
				this.headerList.forEach(item => {
					width += item.width ;
				})
				return width + 'upx';
			},
            //滚动到底部加载下一页
            lower(){
            }
		}
	}
</script>

<style lang="scss" scoped>
	$border-color: #E4E4E4;
	$border: 1rpx solid $border-color;
	$white: #FFFFFF;
	$title-color: #f8f8f8;
	
	.table {
		display: table;
		.table-header {
			background-color: $title-color;
			position: sticky;
			top: 0;
			z-index: 2;
			
			.title-cell{
				display: table-cell;
				border: $border;
				text-align: center;
				position: relative;
				vertical-align: middle;
			}
	
			.title-cell-left {
				border: $border;
				position: relative;
				position: sticky;
				left: 0;
				z-index: 1;
				text-align: center;
				background-color: $title-color;
			}
			.title-cell-right{
				border: $border;
				position: relative;
				position: sticky;
				right: 0;
				z-index: 1;
				text-align: center;
				background-color: $title-color;
			}
	
			.content-cell {
				border-top: $border;
				border-bottom: $border;
				border-right: $border;
				text-align: center;
				
			}
		}
	
		.table-body {
			.table-tr {
				display: table-row;
				.title-cell{
					display: table-cell;
					border-left: $border;
					border-right: $border;
					border-bottom: $border;
					vertical-align: middle;
				}
				
				.title-cell-left {
					border-left: $border;
					border-right: $border;
					border-bottom: $border;
					position: sticky;
					left: 0;
					z-index: 1;
					background-color: white;
				}
				
				.title-cell-right {
					border-left: $border;
					border-right: $border;
					border-bottom: $border;
					position: sticky;
					right: 0;
					z-index: 1;
					background-color: white;
				}
	
				.content-cell {
					border-bottom: $border;
					border-right: $border;
				
				}
			}
		}
	
		.title-cell-left,.title-cell-right{
			display: table-cell;
			vertical-align: middle;
		}
	
		.content-cell {
			display: table-cell;
			vertical-align: middle;
		}
	
		.cell {
			padding: 10rpx;
			line-height: 1.6;
			word-wrap: break-word;
			word-break: break-all;
		}
	}
	
	

	

</style>

2.table

(1)固定列需要计算下left多少或者right多少

(2)固定列后border会变成透明,可以用css的outline(轮廓线)样式实现,outline是不占空间的

<template>
	<view>
		<h3 style="text-align: center;">	table</h3>
		<view style="margin: 20upx;">
			<scroll-view scroll-x="true" scroll-y="true"
				style="border: 1upx solid #eee; width: 100%;height: 250upx;" >
				<view class="table-y">
					<table :style="[{'width':tableWidth()}]"  >
						<thead>
							<tr  >
								<th class="table-th" 
									:style="[{'width':item3.width + 'upx',
										'left':(item3.left ? item3.left + 'upx':''),
										'z-index':(item3.left ? 80 :20)
										}]"  
									v-for="(item3,index3) in headerList1" :key="index3">
									{{item3.title}}
								</th>
							</tr>
						</thead>
						<tbody>
							<tr  v-for="(item4,index4) in dataList1" :key="index4">
								<td :style="[{'width':headerList1[0].width + 'upx',
											'left':(headerList1[0].left ? headerList1[0].left + 'upx':''),}]"
									:class="headerList1[0].left ?  'table-td-left':''">
									{{item4.id}}
								</td>
								<td :style="[{'width':headerList1[1].width + 'upx',
											'left':(headerList1[1].left ? headerList1[1].left + 'upx':''),}]"
									:class="headerList1[1].left ?  'table-td-left':''">
									{{item4.name}}
								</td>
								<td :style="[{'width':headerList1[2].width + 'upx'}]">
									{{item4.address}}
								</td>
							</tr>
						</tbody>
					</table>
				</view>
				
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headerList1:[
					{
						title:'id',
						width: 100,
						type:'title-cell-left',
						left:'1'
					},
					{
						title:'姓名',
						width: 120,
						type:'content-cell',
						left:'101' //table自带的样式导致由111变成了117
					},
					{
						title:'地址',
						width: 600,
						type:'content-cell'
					}
				],
				dataList1:[
					{
						id:'001',
						name:'李四',
						address:'xxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'002',
						name:'李四',
						address:'xxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'003',
						name:'李四',
						address:'xxxxxxxxxxxxxxxxxxxxxx'
					},
					{
						id:'004',
						name:'李四',
						address:'xxxxxxxxxxxxxxxxxxxxxx'
					}
				]
			}
		},
		created() {
			this.tableWidth()
		},
		methods: {
			//计算表格宽度
			tableWidth() {
				let width = 0;
				this.headerList1.forEach(item => {
					width += item.width ;
				})
				return width + 'upx';
			},
		}
	}
</script>

<style lang="scss" scoped>
	.table-y{
		table{
			border-right: 1upx solid #000;
			border-bottom: 1upx solid #000;
			border-spacing: 0;
			border-collapse: collapse;
		}
		table td {
			border-left: 1upx solid #000;
			border-top: 1upx solid #000;
			text-align: center;
			border-right: 1upx solid #000;
			height: 100%;
		}
		
		
		.table-th{
			position: sticky;
			top: 1upx;
			background-color: #eee;
			border: none;
			outline-color: #000;
			outline-style: solid;
			outline-width: 1upx;
		}
		
		.table-td-left{
			position: sticky;
			background-color: white;
			border: none;
			outline-color: #000;
			outline-style: solid;
			outline-width: 1upx;
			z-index: 70;
		}
	}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app中,可以使用 `uni-list` 和 `uni-table-view` 组件实现固定滚动的效果。 首先,你需要安装 `uni-list` 和 `uni-table-view` 组件。然后,在页面中引入组件: ```html <template> <view> <uni-list> <!-- 固定部分 --> <uni-table-view :tableData="fixedColumns" :columnWidths="fixedWidths" :height="height"></uni-table-view> <!-- 滚动部分 --> <scroll-view class="scroll-view" style="height: {{height}}px;"> <uni-table-view :tableData="scrollingColumns" :columnWidths="scrollingWidths" :height="height"></uni-table-view> </scroll-view> </uni-list> </view> </template> ``` 在上面的示例中,`uni-table-view` 组件被用来呈现表格数据。`fixedColumns` 和 `scrollingColumns` 分别是固定滚动的数据。`fixedWidths` 和 `scrollingWidths` 分别是固定滚动宽度。`height` 是表格的高度。 接下来,你需要为固定滚动设置样式。你可以在 `style` 中添加以下样式: ```css <style> .scroll-view { overflow-x: scroll; white-space: nowrap; -webkit-overflow-scrolling: touch; margin-top: -1px; } .scroll-view::-webkit-scrollbar { display: none; } .scroll-view .uni-table-view { display: inline-block; margin-left: -1px; margin-top: -1px; } .uni-table-view td { border-top: 1px solid #e5e5e5; border-left: 1px solid #e5e5e5; } .uni-table-view td:first-child { border-left: none; } </style> ``` 在上面的示例中,`.scroll-view` 设置了滚动部分的样式。`white-space: nowrap;` 和 `display: inline-block;` 使得滚动部分的表格可以水平滚动。`.uni-table-view td` 和 `.uni-table-view td:first-child` 设置了单元格的样式,`.scroll-view::-webkit-scrollbar` 则隐藏了滚动条。 最后,你需要在页面中绑定数据,这里提供一个示例数据: ```javascript <script> export default { data() { return { fixedColumns: [ { name: '固定1', value: 'A1' }, { name: '固定2', value: 'B1' }, ], scrollingColumns: [ { name: '滚动1', value: 'A2' }, { name: '滚动2', value: 'B2' }, { name: '滚动3', value: 'C2' }, { name: '滚动4', value: 'D2' }, { name: '滚动5', value: 'E2' }, { name: '滚动6', value: 'F2' }, { name: '滚动7', value: 'G2' }, { name: '滚动8', value: 'H2' }, { name: '滚动9', value: 'I2' }, { name: '滚动10', value: 'J2' }, ], fixedWidths: { '固定1': '100rpx', '固定2': '200rpx', }, scrollingWidths: { '滚动1': '100rpx', '滚动2': '100rpx', '滚动3': '100rpx', '滚动4': '100rpx', '滚动5': '100rpx', '滚动6': '100rpx', '滚动7': '100rpx', '滚动8': '100rpx', '滚动9': '100rpx', '滚动10': '100rpx', }, height: 500, } } } </script> ``` 在上面的示例中,`fixedColumns` 和 `scrollingColumns` 分别是固定滚动的数据。`fixedWidths` 和 `scrollingWidths` 分别是固定滚动宽度。`height` 是表格的高度。 这样就可以实现固定滚动的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值