element-ui?el-table表格固定表頭代碼示例
前提:table內(nèi)容過(guò)高時(shí)頁(yè)面滾動(dòng)到下方后,表頭看不見(jiàn)無(wú)法明確各列的含義
1. 官網(wǎng)給出兩種種屬性來(lái)固定表頭 height 以及 max-height;但是有個(gè)問(wèn)題就是 height高度會(huì)被定死,max-height無(wú)法適應(yīng)不同分辨率的情況
<el-table ref="multipleTable" :data="listData" height="200" class="img-tab"> <el-table-column label="序號(hào)" prop="pagingCustomOrder" align="center" width="80" /> <el-table-column align="center" label="報(bào)警時(shí)間" prop="warningTime" /> <el-table-column align="center" label="企業(yè)名稱" prop="companyName" /> </el-table>
<el-table ref="multipleTable" :data="listData" max-height="200" class="img-tab"> <el-table-column label="序號(hào)" prop="pagingCustomOrder" align="center" width="80" /> <el-table-column align="center" label="報(bào)警時(shí)間" prop="warningTime" /> <el-table-column align="center" label="企業(yè)名稱" prop="companyName" /> </el-table>
2. 可以進(jìn)行樣式控制表格高度,達(dá)到固定表頭的需求,就可以避免高度定死的情況
<el-table ref="multipleTable" :data="listData" class="scroll-tab"> <el-table-column label="序號(hào)" prop="pagingCustomOrder" align="center" width="80" /> <el-table-column align="center" label="報(bào)警時(shí)間" prop="warningTime" /> <el-table-column align="center" label="企業(yè)名稱" prop="companyName" /> </el-table>
// table表頭固定 .el-table.scroll-tab { overflow: auto; max-height: calc(100% - 200px); } .scroll-tab .el-table__header-wrapper { position: sticky; top: 0;//這個(gè)值根據(jù)實(shí)際情況而定 z-index: 10; }
3. 若是表格寬度也超出內(nèi)容,需要橫向滾動(dòng)+豎向滾動(dòng)
// table表頭固定 .el-table.scroll-tab { overflow: hidden; height: calc(100% - 200px); } .scroll-tab .el-table__header-wrapper { position: sticky; top: 0;//這個(gè)值根據(jù)實(shí)際情況而定 z-index: 10; } .scroll-tab .el-table__body-wrapper { height: calc(100% - 60px); overflow: auto; }
附:el-table固定表頭(設(shè)置height)出現(xiàn)內(nèi)容過(guò)多時(shí)不能滾動(dòng)問(wèn)題
主要原因是el-table沒(méi)有div包裹
解決:加一個(gè)div并設(shè)置其高度和overflow
我自己的主要代碼
<div class="contentTable"> <el-table ref="table" :data="tableData" stripe @row-dblclick="onRowDblclick" height="100%" > <el-table-column type="index" align="center" label="序號(hào)" width="50" ></el-table-column> <el-table-column prop="templateName" align="center" label="模板名稱" width="150" ></el-table-column> <el-table-column prop="mainContent" align="center" label="主要內(nèi)容" ></el-table-column> <el-table-column prop="devContent" align="center" label="活動(dòng)措施和設(shè)備狀態(tài)" ></el-table-column> <el-table-column prop="operate" align="center" label="操作" width="80"> <template slot-scope="scope"> <el-button size="mini" class="delete-btn" @click="onDelete(scope.row)" title="刪除" v-isLogin ></el-button> </template> </el-table-column> </el-table> </div>
css代碼:
.contentTable { height: calc(100% - 50px) !important; overflow: scroll; } .contentTable >>> .el-table__body-wrapper::-webkit-scrollbar { width: 10px; height: 8px; }
-webkit-scrollbar用來(lái)加滾動(dòng)條的!?。?/strong>
頁(yè)面所有代碼:
<template> <el-dialog :title="title" :visible.sync="visible" class="template-query" @opened="openInit" append-to-body width="80%" > <el-form :model="form" ref="form" :inline="true" style="text-align: right" size="small" > <el-form-item label="模板名稱:" prop="templateName"> <el-input v-model="form.templateName" maxlength="20" v-special-code ></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="onQuery">查詢</el-button> <el-button type="primary" @click="onReset">重置</el-button> </el-form-item> </el-form> <div class="contentTable"> <el-table ref="table" :data="tableData" stripe @row-dblclick="onRowDblclick" height="100%" > <el-table-column type="index" align="center" label="序號(hào)" width="50" ></el-table-column> <el-table-column prop="templateName" align="center" label="模板名稱" width="150" ></el-table-column> <el-table-column prop="mainContent" align="center" label="主要內(nèi)容" ></el-table-column> <el-table-column prop="devContent" align="center" label="活動(dòng)措施和設(shè)備狀態(tài)" ></el-table-column> <el-table-column prop="operate" align="center" label="操作" width="80"> <template slot-scope="scope"> <el-button size="mini" class="delete-btn" @click="onDelete(scope.row)" title="刪除" v-isLogin ></el-button> </template> </el-table-column> </el-table> </div> </el-dialog> </template> <script> import { listTemplatesByType } from "@/api/template.js"; import { removeTemplate } from "@/api/template.js"; import { getBizcodeList } from "@/api/common.js"; export default { props: { templateQueryVisible: { type: Boolean, default: false, }, type: { type: String, default: "", }, typeName: { type: String, default: "", }, }, data() { return { title: "", form: { templateName: "", }, headField: [], //表頭信息 tableData: [], //表格數(shù)據(jù) }; }, computed: { visible: { get() { return this.templateQueryVisible; }, set(val) { this.$emit("update:templateQueryVisible", val); }, }, }, mounted() {}, methods: { //打開(kāi)窗口初始化 openInit() { this.title = this.typeName + "模板管理"; this.form.templateName = ""; //根據(jù)type查詢表頭信息 // listGridHeadByType({ type: this.type }).then(async (res) => { // var headFieldList = JSON.parse(res.data.data); // for (var i = 0; i < headFieldList.length; i++) { // if ("codeType" in headFieldList[i]) { // await getBizcodeList(headFieldList[i].codeType).then((res) => { // headFieldList[i]["codeList"] = res.data.data; // }); // } // } // this.headField = headFieldList; // }); //查詢模板數(shù)據(jù) this.onQuery(); }, //刪除 onDelete(row) { var that = this; this.$confirm("確定刪除該模板?", "提示", { confirmButtonText: "確定", cancelButtonText: "取消", type: "warning", }).then(() => { removeTemplate(row.id).then((res) => { if (res.data.code == "1") { that.$message({ type: "success", message: "刪除模板成功!", }); that.onQuery(); } else { that.$message({ type: "error", message: "保存模板失??!", }); } }); }); }, //雙擊行加載模板數(shù)據(jù) onRowDblclick(row) { if (row.id) { delete row.id; } if (row.ID) { delete row.ID; } this.$emit("loadTemplateData", row); }, //查詢 onQuery() { //根據(jù)type查詢模板數(shù)據(jù) listTemplatesByType({ type: this.type, name: this.form.templateName, }).then((res) => { var resData = res.data.data; var tableData = []; console.log(resData); if (resData) { for (var i = 0; i < resData.length; i++) { var content = JSON.parse(resData[i].content); let res = { id: resData[i].id, templateName: resData[i].name, mainContent: content.mainContent, devContent: content.devContent, }; tableData.push(res); } this.tableData = tableData; } else { this.tableData = []; } }); }, //重置 onReset() { if (this.$refs.form) { this.$refs.form.resetFields(); this.onQuery(); } }, //渲染表格列 itemFormatter(cellValue, codeList) { if (codeList && cellValue) { // return this.$common.renderCodeId(cellValue, codeList); return this.$common.renderCode(cellValue, "ID", "TEXT", codeList); } else { return cellValue; } }, }, }; </script> <style scoped> .template-query >>> .el-dialog__body { height: 600px; } .template-query >>> .el-form-item__label { width: 85px !important; } .delete-btn { background-image: url("~@/assets/imgs/delete.png"); width: 23px; height: 23px; padding-left: 5px; cursor: pointer; background-repeat: no-repeat; } .contentTable { height: calc(100% - 50px) !important; overflow: scroll; } .contentTable >>> .el-table__body-wrapper::-webkit-scrollbar { width: 10px; height: 8px; } </style>
總結(jié)
到此這篇關(guān)于element-ui el-table表格固定表頭的文章就介紹到這了,更多相關(guān)element-ui el-table固定表頭內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 3 中watch 和watchEffect 的新用法
本篇文章主要通過(guò) Options API 和 Composition API 對(duì)比 watch 的使用方法,讓大家快速掌握 vue3 中 watch 新用法,需要的朋友可以參考一下哦,希望對(duì)大家有所幫助2021-11-11vue3使用element-plus搭建后臺(tái)管理系統(tǒng)之菜單管理功能
這篇文章主要介紹了vue3使用element-plus搭建后臺(tái)管理系統(tǒng)之菜單管理,使用element-plus el-tree組件快速開(kāi)發(fā)樹(shù)形菜單結(jié)構(gòu),el-tree組件中filter-node-method事件便可以實(shí)現(xiàn)樹(shù)形菜單篩選過(guò)濾功能,需要的朋友可以參考下2022-04-04Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法
這篇文章主要介紹了Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07vue3 Vite 進(jìn)階rollup命令行使用詳解
這篇文章主要介紹了vue3 Vite 進(jìn)階rollup命令行使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08vue項(xiàng)目部署上線遇到的問(wèn)題及解決方法
這篇文章主要介紹了vue項(xiàng)目部署上線遇到的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-06-06VUE2舊項(xiàng)目重新安裝依賴后@vue/compiler-sfc編譯報(bào)錯(cuò)問(wèn)題
在VUE2舊項(xiàng)目中重新安裝依賴后,如果遇到@vue/compiler-sfc編譯報(bào)錯(cuò),首先需要檢查package.json文件中的Vue版本是否升級(jí)到2.7版本,2.7版本的編譯插件不再支持/deep/這種樣式穿透,版本^和~的區(qū)別在于^只能鎖住第一位數(shù)2025-01-01