uniapp 展示表格并且前端实现分页

本文介绍了如何通过DCloud插件中的table样式创建可定制的表格,包括基本表格和带有固定列的布局,以及如何在Vue组件中灵活运用各种CSS类来调整表格样式。通过实例演示了如何设置表头、单元格和不同状态下的背景色,适用于数据展示和UI设计。
摘要由CSDN通过智能技术生成

实现效果

实现表格:                                                                                                                                 我是直接使用了该插件的样式table表格 - DCloud 插件市场 此插件里有更多的用法 

1.首先复制下面这些table的样式到一个文件里 并在app.vue里引入此scss 注意 文件的格式要为scss

page {
	font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
}
@for $i from 0 through 100 {
	.w-#{$i} {
		width: $i + '%';
	}
	.h-#{$i} {
		height: $i + '%';
	}
	.padding-#{$i} {
		padding: $i + rpx;
	}
	.padding-left-#{$i} {
		padding-left: $i + rpx;
	}
	.padding-right-#{$i} {
		padding-right: $i + rpx;
	}
	.padding-top-#{$i} {
		padding-top: $i + rpx;
	}
	.padding-bottom-#{$i} {
		padding-bottom: $i + rpx;
	}
	.margin-#{$i} {
		margin: $i + rpx;
	}
	.margin-left-#{$i} {
		margin-left: $i + rpx;
	}
	.margin-right-#{$i} {
		margin-right: $i + rpx;
	}
	.margin-top-#{$i} {
		margin-top: $i + rpx;
	}
	.margin-bottom-#{$i} {
		margin-bottom: $i + rpx;
	}
}
.d-table {
	display: table;
	width: 100%;
	vertical-align: middle;
	text-align: center;
	table-layout: fixed;
	font-size: 28rpx;
	word-break: break-all;
	background-color: white;
	border-collapse: collapse;
	&.primary {
		&.dark {
			.d-thead {
				background-color: #2b85e4;
			}
		}
		&.disabled {
			.d-thead {
				background-color: #a0cfff;
			}
		}
		&.light {
			.d-thead {
				background-color: #ecf5ff;
			}
		}
		.d-thead {
			background-color: #2979ff;
			color: #fff;
		}
	}
	&.success {
		&.dark {
			.d-thead {
				background-color: #18b566;
			}
		}
		&.disabled {
			.d-thead {
				background-color: #71d5a1;
			}
		}
		&.light {
			.d-thead {
				background-color: #dbf1e1;
			}
		}
		.d-thead {
			background-color: #19be6b;
			color: #fff;
		}
	}
	&.warning {
		&.dark {
			.d-thead {
				background-color: #f29100;
			}
		}
		&.disabled {
			.d-thead {
				background-color: #fcbd71;
			}
		}
		&.light {
			.d-thead {
				background-color: #fdf6ec;
			}
		}
		.d-thead {
			background-color: #ff9900;
			color: #fff;
		}
	}
	&.danger {
		&.dark {
			.d-thead {
				background-color: #dd6161;
			}
		}
		&.disabled {
			.d-thead {
				background-color: #fab6b6;
			}
		}
		&.light {
			.d-thead {
				background-color: #fef0f0;
			}
		}
		.d-thead {
			background-color: #fa3534;
			color: #fff;
		}
	}
	&.info {
		&.dark {
			.d-thead {
				background-color: #82848a;
			}
		}
		&.disabled {
			.d-thead {
				background-color: #c8c9cc;
			}
		}
		&.light {
			.d-thead {
				background-color: #f4f4f5;
			}
		}
		.d-thead {
			background-color: #909399;
			color: #fff;
		}
	}
	&.gray {
		.d-thead {
			background-color: #aaa;
			color: #fff;
		}
	}
	&.border-under {
		.d-td,
		.d-th {
			@extend .noBorder;
		}
		.d-tr {
			@extend .border-bottom;
		}
		.d-table {
			.d-tr {
				@extend .noBorder;
			}
		}
	}
	&.large {
		.d-td,
		.d-th {
			height: 80rpx;
		}
	}
	&.middle {
		.d-td,
		.d-th {
			height: 60rpx;
		}
	}
	.d-td,
	.d-th {
		@extend .border;
	}
	.d-table {
		height: 100%;
		.d-td,
		.d-th {
			@extend .noBorder;
		}
		// .d-tr {
		// 	&:first-child {
		// 		.d-td{
		// 			&.border-bottom {
		// 				border-bottom: none;
		// 			}
		// 			&.border-top {
		// 				border-top: none;
		// 			}
		// 		}
		// 	}
		// }
	}
	input.borderInput {
		border: 1px solid #ccc;
		border-radius: 6rpx;
		width: 80%;
		height: 80%;
		display: inline-flex;
	}
}
.d-caption {
	display: table-caption;
	background-color: inherit;
}
.d-thead {
	display: table-header-group;
	font-weight: 600;
}
.d-tbody {
	display: table-row-group;
}
.d-tfoot {
	display: table-footer-group;
}
.d-tr {
	display: table-row;
	width: 100%;
}
.d-td,
.d-th {
	display: table-cell;
	vertical-align: middle;
	height: 50rpx;
	font-size: 28rpx;
}
.d-th {
	font-weight: 600;
}
.border-top {
	border-top: 1px solid #efefef !important;
}
.border-left {
	border-left: 1px solid #efefef !important;
}
.border-bottom {
	border-bottom: 1px solid #efefef !important;
}
.border-right {
	border-right: 1px solid #efefef !important;
}
.border {
	border: 1px solid #efefef;
}
.noBorder {
	border: 0;
}

.flex {
	display: flex;
	align-items: center;
	&-wrap {
		flex-wrap: wrap;
	}
	&-nowrap {
		flex-wrap: nowrap;
	}
	&-direction {
		flex-direction: column;
	}
}

@each $type in center, space-between, space-around, flex-start, flex-end {
	.row-#{$type} {
		justify-content: $type;
	}
}

@each $type in center, stretch, flex-start, flex-end {
	.col-#{$type} {
		align-items: $type;
	}
}

@each $type in center, left, right {
	.text-#{$type} {
		text-align: $type!important;
	}
}

2.引入之后可以选择直接需要的表格来进行编写

2.1普通表格

<template>
    //普通的表格 devList数组是表格data数据  columns数组是表头名
    //先循环表格data数组再循环表头名数组 如果data数组下标等于0 就使用表头
    <view class="d-table scroll-x">
        <!--表头-->
        <view class="d-tr right" v-for="(item,i) in devList" :key="i">
				<view class="d-th" v-for="(columnsObj,j) in columns" :key="j">
						{{columnsObj.title}}
				</view>
		</view>

        <!--表格-->
		<view class="d-tr right" v-for="(item,i) in devList" :key="i">
				<view class="'d-td'" v-for="(columnsObj,j) in columns" :key="j">
						{{item[columnsObj.dataIndex]}}
				</view>
		</view>
    </view>
</template>

<script>
	export default {
		data() {
			return {
				//table表头
				columns: [{
					title: '进人', //表格标题
					dataIndex: 'enterPeople' //当前列渲染的data名
				}, {
					title: '出人',
					dataIndex: 'outPeople'
				}, {
					title: '入口异常',
					dataIndex: 'entranceException'
				}],
                //表格Data数据
                devList:[{
                    id:1,
                    enterPeople:'假数据',
                    outPeople:'假数据',
                    entranceException:'假数据'
                },{
                    id:1,
                    enterPeople:'假数据',
                    outPeople:'假数据',
                    entranceException:'假数据'
                }]
			}
		}
	}
</script>

<style>

	.d-table .right>view {
		border: 0;
		border-bottom: 1px solid #efefef;
	}

	.d-table .d-th,
	.d-table .left>view {
		border: 0 !important;
	}
</style>

2.2 固定列表格

<template>
    //带固定列的表格 devList数组是表格data数据  columns数组是表头名
    <view style="display: flex;">
        <!--左侧固定列-->
		<view class="d-table" style="width: 30%;">
            <!--固定列的表头-->
			<view class="d-tr left">
				<view class='d-th'>{{'设备序列号'}}</view>
			</view>
            <!--固定列的表格-->
		    <view class="d-tr left" v-for="(item,i) in devList" :key="i">
			    <view class="d-td">{{item.devSn}}</view>
		    </view>
	    </view>

        <!--剩余未固定的列-->
	    <scroll-view scroll-x class="border-right" style="width: 70%;">
		    <view class="d-table scroll-x">
                 <!--表头-->
			    <view class="d-tr right">
			    	<view style="width:150rpx" class="d-th" 
                    v-for="(columnsObj,j) in columns" :key="j">
					{{columnsObj.title}}
				    </view>
			    </view>
                 <!--表格-->
		        <view class="d-tr right" 
                    v-for="(item,i) in devList" :key="i">
			        <view style="width:150rpx" class="d-td" 
                        v-for="(columnsObj,j) in columns"  :key="j">
				        {{item[columnsObj.dataIndex]}}
			        </view>
		        </view>
	        </view>
	    </scroll-view>
    </view>
</template>

<script>
	export default {
		data() {
			return {
				//table表头
				columns: [{
					title: '进人', //表格标题
					dataIndex: 'enterPeople' //当前列渲染的data名
				}, {
					title: '出人',
					dataIndex: 'outPeople'
				}, {
					title: '入口异常',
					dataIndex: 'entranceException'
				}],
                //表格Data数据
                devList:[{
                    id:1,
                    enterPeople:'假数据',
                    outPeople:'假数据',
                    entranceException:'假数据'
                },{
                    id:1,
                    enterPeople:'假数据',
                    outPeople:'假数据',
                    entranceException:'假数据'
                }]
			}
		}
	}
</script>

<style>

	.d-table .right>view {
		border: 0;
		border-bottom: 1px solid #efefef;
	}

	.d-table .d-th,
	.d-table .left>view {
		border: 0 !important;
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值