element-ui表格滚动效果,el-table滚动条样式重置

文章介绍了如何在项目首页使用ElementUI构建一个带有动态滚动功能的表格,通过监听鼠标事件控制表格内容的滚动,以及相应的CSS样式调整。

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

项目首页需要展示一个表格滚动区域,特此来记录一下

HTML

<div class="table-box" @mouseenter="mouseenter" @mouseleave="mouseleave">
	<el-table 
		:data="tableList" 
		border 
		height="400px" 
		v-loading="tableLoading">
		
		<el-table-column prop="id" label="编号">
			<template slot-scope="scope">
				<el-link type="primary" @click="openNewTab(scope.row)">
					{{scope.row.id}}
				</el-link>
			</template>
		</el-table-column>
		
		<el-table-column prop="name" label="名称" show-overflow-toolltip></el-table-column>
		
		<el-table-column prop="desc" label="描述" show-overflow-toolltip></el-table-column>
		
		<el-table-column prop="createTime" label="创建时间" show-overflow-toolltip>
		</el-table-column>
		
		<el-table-column prop="createBy" label="创建人" show-overflow-toolltip>
		</el-table-column>
	</el-table>
</div>

JS

data(){
	return {
		tableLoading : false,
		timer: null,
		tableList: []
	}
},
watch: {
	currentData: {
		handler(val){
			this.clearTimer()
			if(val){
				this.getList()
			}
		}
	}
},
beforeDestory(){
	this.clearTimer()
},
methods: {
	clearTimer(){
		this.mouseenter()
		this.tableList = [];
	},
	async getList(){
		this.tableLoading = true;
		let params = {
			page: {
				current: 1,
				size: 100
			},
			vo: {
				currentData: this.currentData
			}
		}
		const res = await installServer.getDetailList(params);
		this.tableLoading = false;
		if(res.success){
			this.clearTime()
			this.tableList = res.data.resords || [];
			this.$nextTick(()=>{
                 if(this.dataList.length) this.handleScroll(true)
             })
		}
	},
	
	handleScroll(isReset){
		this.$nextTick(()=>{
			const EL = document.getElementsByClassName('el-table__body-wrapper')[0];
			const innerEL = document.getElementsByClassName('el-table__body')[0];
			const clientHeight = EL.clientHeight;//容器高度
			const innerHeight = innerEL.clientHeight;//内部table高度
			if(isReset) EL.scrollTop = 0;
			//内部高度大于容器高度,需要滚动
			if(innerHeight > clientHeight ){
				let scrollTop = EL.scrollTop;
				this.timer = setInterval(()=>{
					if(scrollTop < innerHeight  - clientHeight ){
						scrollTop += parseFloat((1 / window.devicePixelRatio).toFixed(2)) + 0.01;
					}else {
						scrollTop = 0;
					}
					EL.scrollTop = scrollTop;
				}, 50)
			}
		})
	},
	mouseenter(){
		if(this.timer) clearInterval(this.timer);
		this.timer = null;
	},
	mouseleave(){
		if(this.tableList.length && !this.timer){
			this.handleScroll()
		}
	}
}

CSS(修改滚动条样式)

.el-table__body-wrapper {
	&::-webkit-scrollbar {
		width: 6px !important;
		height: 6px !important;
	}
	&::-webkit-scrollbar-thumb {
		background-color: #ccc;
		border-radius: 3px;
	}
	&::-webkit-scrollbar-track {
		background-color: transparent !important;
	}
}
/* element ui为原生滚动条预留宽度17px, 重置滚动条样式后会距离右边有空白  */
.el-table__body-wrapper .el-table__body {
	width: 100% !important;
}
.el-tablecolgroup col[name='gutter'] {
    width: 6px !important;/*这个是table-header距离右边的距离,设置为滚动条宽*/
}
.el-table__header-wrapper .el-table__header tr th {
    background-color: #5197E6 !important;
    color: #fff;
    border-color: #5ca7fd;
}

在这里插入图片描述

### element-ui 表格多选框不显示解决方案 对于element-ui表格中的多选框无法正常显示的问题,通常是因为对话框未能正确初始化加载所引起的[^1]。为了确保多选框能够按照预期工作,在打开对话框之前应当重置与之关联的数据源。 #### 数据绑定校验 确认用于控制多选状态的变量已经正确定义,并且在每次展示对话框前都进行了必要的初始化处理。如果存在缓存的选择项,则需清空这些缓存以便于重新获取最新的选项列表。 ```javascript // 假设 data.selectedItems 是存储已选择项目的数组 data() { return { selectedItems: [] }; }, methods: { openDialog() { this.selectedItems = []; // 清空之前的选定项目 // 执行其他逻辑... } } ``` #### 组件生命周期钩子函数的应用 利用Vue.js提供的组件生命周期方法来管理对话框的状态变化。可以在`beforeMount`或`mounted`阶段执行一次性的初始化任务;而在`activated`/`deactivated`(针对keep-alive场景)或者自定义事件触发时刷新数据。 ```javascript created() { // 初始化配置或其他一次性设置可以放在这里 }, activated() { // 当组件被激活时调用此方法,可用于清理旧数据并准备新数据 this.resetSelection(); } methods: { resetSelection() { this.$refs.table.clearSelection(); // 清除所有行的选中状态 }, } ``` 通过上述措施可有效防止由于历史遗留数据导致的新一轮交互过程中出现问题的情况发生。另外需要注意的是,确保HTML模板内的表单控件名称唯一性以及v-model指令指向正确的属性名也非常重要。 #### 关联代码片段示例 下面是一个简单的例子展示了如何在一个包含多选功能的Element UI Table内实现这一点: ```html <template> <div id="app"> <el-button @click="dialogVisible=true">Open Dialog</el-button> <!-- 对话框 --> <el-dialog :visible.sync="dialogVisible" width="80%"> <el-table ref="multipleTable" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"> <el-table-column type="selection"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> </el-table> <span slot="footer" class="dialog-footer"> <el-button @click="closeDialog()">Cancel</el-button> <el-button type="primary" @click="confirmSelection()">Confirm</el-button> </span> </el-dialog> </div> </template> <script> export default { name: 'App', data(){ return{ dialogVisible:false, tableData:[{id:1,name:'Alice'},{id:2,name:'Bob'}], multipleSelection:[], } }, methods:{ handleSelectionChange(val){ this.multipleSelection=val; }, closeDialog(){ this.dialogVisible=false; this.$nextTick(()=>{ this.$refs['multipleTable'].clearSelection(); }); }, confirmSelection(){ console.log(this.multipleSelection); this.closeDialog(); } } }; </script> ``` 该实例说明了怎样监听到用户选择了不同的行之后更新内部维护的选择集合,并且提供了关闭按钮用来清除当前所有的选择记录从而不影响下一次的操作体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值