element-ui?el-table表格固定表頭代碼示例
前提:table內(nèi)容過高時頁面滾動到下方后,表頭看不見無法明確各列的含義
1. 官網(wǎng)給出兩種種屬性來固定表頭 height 以及 max-height;但是有個問題就是 height高度會被定死,max-height無法適應(yīng)不同分辨率的情況

<el-table ref="multipleTable" :data="listData" height="200" class="img-tab">
<el-table-column label="序號" prop="pagingCustomOrder" align="center" width="80" />
<el-table-column align="center" label="報警時間" 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="序號" prop="pagingCustomOrder" align="center" width="80" />
<el-table-column align="center" label="報警時間" prop="warningTime" />
<el-table-column align="center" label="企業(yè)名稱" prop="companyName" />
</el-table>2. 可以進行樣式控制表格高度,達到固定表頭的需求,就可以避免高度定死的情況
<el-table ref="multipleTable" :data="listData" class="scroll-tab">
<el-table-column label="序號" prop="pagingCustomOrder" align="center" width="80" />
<el-table-column align="center" label="報警時間" 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;//這個值根據(jù)實際情況而定
z-index: 10;
}3. 若是表格寬度也超出內(nèi)容,需要橫向滾動+豎向滾動
// table表頭固定
.el-table.scroll-tab {
overflow: hidden;
height: calc(100% - 200px);
}
.scroll-tab .el-table__header-wrapper {
position: sticky;
top: 0;//這個值根據(jù)實際情況而定
z-index: 10;
}
.scroll-tab .el-table__body-wrapper {
height: calc(100% - 60px);
overflow: auto;
}附:el-table固定表頭(設(shè)置height)出現(xiàn)內(nèi)容過多時不能滾動問題
主要原因是el-table沒有div包裹

解決:加一個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="序號"
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="活動措施和設(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用來加滾動條的?。。?/strong>
頁面所有代碼:
<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="序號"
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="活動措施和設(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: {
//打開窗口初始化
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 3 中watch 和watchEffect 的新用法
本篇文章主要通過 Options API 和 Composition API 對比 watch 的使用方法,讓大家快速掌握 vue3 中 watch 新用法,需要的朋友可以參考一下哦,希望對大家有所幫助2021-11-11
vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
這篇文章主要介紹了vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理,使用element-plus el-tree組件快速開發(fā)樹形菜單結(jié)構(gòu),el-tree組件中filter-node-method事件便可以實現(xiàn)樹形菜單篩選過濾功能,需要的朋友可以參考下2022-04-04
Vue實現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法
這篇文章主要介紹了Vue實現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
VUE2舊項目重新安裝依賴后@vue/compiler-sfc編譯報錯問題
在VUE2舊項目中重新安裝依賴后,如果遇到@vue/compiler-sfc編譯報錯,首先需要檢查package.json文件中的Vue版本是否升級到2.7版本,2.7版本的編譯插件不再支持/deep/這種樣式穿透,版本^和~的區(qū)別在于^只能鎖住第一位數(shù)2025-01-01

