el-table實(shí)現(xiàn)嵌套表格的展示功能(完整代碼)
需求
一個(gè)表單中存在子表
列表返回格式
實(shí)現(xiàn)
實(shí)現(xiàn)思路
el-table中在嵌套一個(gè)el-table,這樣數(shù)據(jù)格式就沒(méi)問(wèn)題了,主要就是樣式
將共同的列放到一列中,通過(guò)渲染自定義表頭render-header
,將表頭按照合適的寬度渲染出來(lái)
<el-table-column prop="table" label="子表" class-name="has-child" :render-header="renderHeader" :width="childColumn.length * 120 + 2 + 'px'" > <template slot-scope="scope"> <el-table border :data="scope.row.details" class="child-table" :show-header="false" > <el-table-column prop="startDate" align="center" label="起始日期" width="120px" ></el-table-column> <el-table-column prop="endDate" align="center" label="結(jié)束日期" width="120px" ></el-table-column> <el-table-column prop="applyDay" align="center" label="申請(qǐng)?zhí)鞌?shù)" width="120px" ></el-table-column> <el-table-column prop="destination" align="center" label="目的地" width="120px" ></el-table-column> <el-table-column prop="transportation" align="center" label="交通方式" width="120px" ></el-table-column> <el-table-column prop="businessTripReason" align="center" label="出差事由" width="120px" ></el-table-column> <el-table-column prop="returnDate" align="center" label="返程日期" width="120px" ></el-table-column> <el-table-column prop="actualDay" align="center" label="實(shí)際出差天數(shù)" width="120px" ></el-table-column> <el-table-column prop="subsidyDay" align="center" label="差補(bǔ)天數(shù)" width="120px" ></el-table-column> <el-table-column prop="remark" align="center" label="備注" width="120px" ></el-table-column> </el-table> </template> </el-table-column>
根據(jù)數(shù)據(jù)渲染表頭
childColumn: [ { label: "起始日期", width: "120px", }, { label: "結(jié)束日期", width: "120px", }, { label: "申請(qǐng)?zhí)鞌?shù)", width: "120px", }, { label: "目的地", width: "120px", }, { label: "交通方式", width: "120px", }, { label: "出差事由", width: "120px", }, { label: "返程日期", width: "120px", }, { label: "實(shí)際出差天數(shù)", width: "120px", }, { label: "差補(bǔ)天數(shù)", width: "120px", }, { label: "備注", width: "120px", }, ], /** * @description 渲染子表 */ renderHeader(h, { column, $index }) { const childTable = this.childColumn.map((item) => { return h( "div", { style: { width: item.width, padding: "0 10px", textAlign: "center", flexShrink: 0, flexGrow: 0, }, }, item.label ); }); return h( "div", { class: ["child-head"], }, childTable ); },
調(diào)樣式,慢慢調(diào)到合適就行
.has-child { padding: 0px !important; // display: none; & > .cell { padding: 0 !important; } ::before { height: 0; } .child-table { background-color: transparent; .cell{ line-height: 34px; } } .el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell { background-color: transparent; } .el-table__body tr.current-row > td.el-table__cell, .el-table__body tr.selection-row > td.el-table__cell { background-color: transparent; } tr { background-color: transparent; } .child-head { display: flex; } }
完整代碼
<template> <div> <Search :config="searchConfig" @search="search"> <el-button type="primary" @click="addItem" icon="el-icon-plus" >新增</el-button > </Search> <el-table border :data="tableData" v-loading="loading" class="el-child-table" > <el-table-column prop="applyDate" label="申請(qǐng)日期" align="center" width="120px" > </el-table-column> <el-table-column prop="nickName" label="申請(qǐng)人" align="center" width="140px" > </el-table-column> <el-table-column prop="deptName" label="申請(qǐng)部門" align="center" width="120px" > </el-table-column> <el-table-column prop="table" label="子表" class-name="has-child" :render-header="renderHeader" :width="childColumn.length * 120 + 2 + 'px'" > <template slot-scope="scope"> <el-table border :data="scope.row.details" class="child-table" :show-header="false" > <el-table-column prop="startDate" align="center" label="起始日期" width="120px" ></el-table-column> <el-table-column prop="endDate" align="center" label="結(jié)束日期" width="120px" ></el-table-column> <el-table-column prop="applyDay" align="center" label="申請(qǐng)?zhí)鞌?shù)" width="120px" ></el-table-column> <el-table-column prop="destination" align="center" label="目的地" width="120px" ></el-table-column> <el-table-column prop="transportation" align="center" label="交通方式" width="120px" ></el-table-column> <el-table-column prop="businessTripReason" align="center" label="出差事由" width="120px" ></el-table-column> <el-table-column prop="returnDate" align="center" label="返程日期" width="120px" ></el-table-column> <el-table-column prop="actualDay" align="center" label="實(shí)際出差天數(shù)" width="120px" ></el-table-column> <el-table-column prop="subsidyDay" align="center" label="差補(bǔ)天數(shù)" width="120px" ></el-table-column> <el-table-column prop="remark" align="center" label="備注" width="120px" ></el-table-column> </el-table> </template> </el-table-column> <el-table-column prop="processStatusName" width="120px" label="狀態(tài)" align="center" > </el-table-column> <el-table-column label="操作" align="center" width="100px" fixed="right"> <template slot-scope="{ row }"> <!-- 1、狀態(tài)為被退回 2、申請(qǐng)人為當(dāng)前登陸人 --> <el-button v-if="row.approveJurisdiction == 1" type="text" @click="approval(row)" >審批</el-button > <el-button type="text" @click="check(row)" v-if="row.checkJurisdiction == 1" >核定</el-button > <el-button v-if=" row.processStatusName == '被退回' && row.nickName === $store.state.user.nickName " type="text" @click="editItem(row)" >修改</el-button > <el-button type="text" @click="viewItem(row)">查看</el-button> </template> </el-table-column> </el-table> <pagination :total="pageInfo.total" :page.sync="pageInfo.pageNum" :limit.sync="pageInfo.pageSize" @pagination="getList" /> <DialogForm ref="dialogFormRef" @success="submitSuccess"></DialogForm> </div> </template> <script> import Search from "@/components/Search/index.vue"; import DialogForm from "./components/form.vue"; import { checkRole } from "@/utils/permission"; import { apiGetBusinessTripList, apiGetStatusOption, } from "@/api/businessTrip/index"; export default { name: "BusinessTrip", components: { Search, DialogForm, }, data() { return { searchConfig: [ { label: "申請(qǐng)日期", prop: "publishTime", type: "daterange", }, { type: "select", label: "狀態(tài)", prop: "processStatus", option: [], }, { type: "input", label: "模糊查詢", prop: "queryCondition", }, ], pageInfo: { pageNum: 1, pageSize: 10, total: 0, }, tableData: [], childColumn: [ { label: "起始日期", width: "120px", }, { label: "結(jié)束日期", width: "120px", }, { label: "申請(qǐng)?zhí)鞌?shù)", width: "120px", }, { label: "目的地", width: "120px", }, { label: "交通方式", width: "120px", }, { label: "出差事由", width: "120px", }, { label: "返程日期", width: "120px", }, { label: "實(shí)際出差天數(shù)", width: "120px", }, { label: "差補(bǔ)天數(shù)", width: "120px", }, { label: "備注", width: "120px", }, ], disabled: true, currentItem: {}, searchForm: {}, loading: false, }; }, mounted() { this.getStatusOption(); this.toApproval(); }, activated() { this.getList(); this.toApproval(); }, methods: { checkRole, /** * @description 從首頁(yè)過(guò)來(lái)自動(dòng)打開(kāi)彈窗 */ toApproval() { const id = this.$route.query.approvalId; const jumpType = this.$route.query.jumpType; this.$nextTick(() => { if (jumpType == 21) { this.$refs.dialogFormRef.open("審批", id); } else if (jumpType == 22) { this.$refs.dialogFormRef.open("核定", id); } }); }, search(form) { this.searchForm = form; this.getList(); }, getList() { this.loading = true; const params = { ...this.searchForm, ...this.pageInfo, startApplyDate: this.searchForm.publishTime && this.searchForm.publishTime[0], endApplyDate: this.searchForm.publishTime && this.searchForm.publishTime[1], }; Reflect.deleteProperty(params, "publishTime"); apiGetBusinessTripList(params) .then((response) => { this.tableData = response.data.records; this.pageInfo.total = response.data.total; }) .finally(() => { this.loading = false; }); }, /** * @description 獲取流程狀態(tài)下拉 */ getStatusOption() { apiGetStatusOption().then((response) => { this.searchConfig[1].option = response.data.map((item) => { return { label: item.name, value: item.id, }; }); }); }, addItem() { this.$refs.dialogFormRef.open("新增"); }, approval(row) { this.$refs.dialogFormRef.open("審批", row.id); }, check(row) { this.$refs.dialogFormRef.open("核定", row.id); }, editItem(row) { this.$refs.dialogFormRef.open("修改", row.id); }, viewItem(row) { this.$refs.dialogFormRef.open("詳情", row.id); }, /** * @description 渲染子表 */ renderHeader(h, { column, $index }) { const childTable = this.childColumn.map((item) => { return h( "div", { style: { width: item.width, padding: "0 10px", textAlign: "center", flexShrink: 0, flexGrow: 0, }, }, item.label ); }); return h( "div", { class: ["child-head"], }, childTable ); }, submitSuccess() { this.$router.replace({ path: "/businessTrip", query: {} }); this.getList(); }, }, }; </script> <style lang="scss" scoped></style>
到此這篇關(guān)于el-table實(shí)現(xiàn)嵌套表格的展示的文章就介紹到這了,更多相關(guān)el-table嵌套表格的展示內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3數(shù)據(jù)可視化實(shí)現(xiàn)數(shù)字滾動(dòng)特效代碼
這篇文章主要介紹了vue3數(shù)據(jù)可視化實(shí)現(xiàn)數(shù)字滾動(dòng)特效,實(shí)現(xiàn)思路是使用Vue.component定義公共組件,使用window.requestAnimationFrame(首選,次選setTimeout)來(lái)循環(huán)數(shù)字動(dòng)畫,詳細(xì)代碼跟隨小編一起看看吧2022-09-09Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格功能
這篇文章主要介紹了Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格功能,演示如何在Vue中使用Element UI的Table組件實(shí)現(xiàn)嵌套表格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01前端vue如何通過(guò)URL訪問(wèn)存儲(chǔ)在服務(wù)器或磁盤的圖片
在Vue中,通常需要將圖片存儲(chǔ)在服務(wù)器端,并通過(guò)url地址來(lái)訪問(wèn),下面這篇文章主要給大家介紹了前端vue如何通過(guò)URL訪問(wèn)存儲(chǔ)在服務(wù)器或磁盤的圖片的相關(guān)資料,需要的朋友可以參考下2024-02-02Vue3+Element-Plus?實(shí)現(xiàn)點(diǎn)擊左側(cè)菜單時(shí)顯示不同內(nèi)容組件展示在Main區(qū)域功能
這篇文章主要介紹了Vue3+Element-Plus?實(shí)現(xiàn)點(diǎn)擊左側(cè)菜單時(shí)顯示不同內(nèi)容組件展示在Main區(qū)域功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01vue使用Split封裝通用拖拽滑動(dòng)分隔面板組件
這篇文章主要介紹了vue使用Split封裝通用拖拽滑動(dòng)分隔面板組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03