Vue ElementUI中el-table表格嵌套樣式問題小結(jié)
一、表格嵌套要求:
- 兩個表格嵌套,當(dāng)父表格有children數(shù)組時子表格才展示;
- 子表格數(shù)據(jù)少于父表格展示字段,且對應(yīng)固定操作列不同;
二、嵌套問題:
當(dāng)使用el-table的type='expand'實現(xiàn)表格嵌套時,樣式出現(xiàn)以下問題:
- 展開圖標(biāo)每條數(shù)據(jù)都展示了,實際上接口數(shù)據(jù)并不是都有children數(shù)組;
- 在表格嵌套后,打開子表格,高亮顯示在經(jīng)過子表格后對應(yīng)不上;
- 父表格的操作列固定在右側(cè)影響了子表格的顯示;
- 滑動到表格底部時,父子表格的固定列對不齊;
修改前效果如下:
修改后效果如下:
三、代碼實現(xiàn):
1、表格定義唯一值row-key="indexId"和類名:row-class-name="getRowClass"
<el-table :row-class-name="getRowClass" ref="table" v-loading="tableLoading" size="mini" height="100%" :data="tableData" row-key="indexId" tooltip-effect="dark" :header-cell-style="{ background: '#f5f7fa', fontWeight: 'bold', color: '#303133' }" @expand-change="expandChange" border > <el-table-column type="expand"> <template slot-scope="props"> <!-- 表格嵌套第二層 --> <el-table ref="sonTable" :style="{ height: `${(props.row.children.length + 1) * 36 + 1}px`, width: '100%' }" row-key="indexId" :data="props.row.children" tooltip-effect="dark" :header-cell-style="{ background: '#f5f7fa', fontWeight: 'bold', color: '#303133' }" border > <!-- 子表格字段 --> <el-table-column> XXX </el-table-column> </el-table> <!-- 父表格字段 --> <el-table-column> XXX </el-table-column> </el-table>
2、類名判斷
// 表格類名方法 getRowClass({ row, rowIndex }) { // 把每一行的索引放進row row.index = rowIndex // 判斷當(dāng)前行是否有子數(shù)據(jù) if ( row.children === null || row.children === undefined || row.children.length === 0 ) { return 'row-hidden-expand-icon' } else { return 'row-show-icon' } },
3、表格樣式
<style lang="scss" scoped> // 子表格覆蓋右側(cè)fix ::v-deep .el-table__body-wrapper { .el-table__expanded-cell { z-index: 100; } } // 有子表格才顯示展開箭頭 :deep(.row-hidden-expand-icon) { td { &:first-child { .el-icon { visibility: hidden; } } .el-table__expand-icon { pointer-events: none; } } } // 去掉表格的第三、第四個單元格出現(xiàn)的展開圖標(biāo) :deep(.el-table__row) { .el-table__cell { &:nth-child(3), &:nth-child(4) { .el-table__expand-icon { pointer-events: none; display: none; } } } } // 子表格樣式 :deep(.el-table__expanded-cell) { padding: 10px !important; } // 修復(fù)hover高亮不同步 ::v-deep .el-table__body tr.hover-row > td.el-table__cell { background-color: transparent; } ::v-deep .el-table .el-table__row:hover { background-color: #f5f7fa; } ::v-deep .el-table__expanded-cell:hover { background-color: transparent; } // 修復(fù)滾到下面對不齊 ::v-deep .el-table__fixed-body-wrapper .el-table__body { padding-bottom: 12px; } // 使得每一行都為36px高度 ::v-deep .row-show-icon { td { &:first-child { .cell { height: 24px; } } } } :deep(.el-table .el-table__cell) { height: 36px !important; } </style>
到此這篇關(guān)于Vue ElementUI中el-table表格嵌套樣式問題的文章就介紹到這了,更多相關(guān)Vue ElementUI中el-table表格嵌套內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2 設(shè)置router-view默認(rèn)路徑的實例
今天小編就為大家分享一篇vue2 設(shè)置router-view默認(rèn)路徑的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09詳解mpvue實現(xiàn)對蘋果X安全區(qū)域的適配
這篇文章主要介紹了詳解mpvue實現(xiàn)對蘋果X安全區(qū)域的適配,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07詳解Vue項目在其他電腦npm run dev運行報錯的解決方法
這篇文章主要介紹了詳解Vue項目在其他電腦npm run dev運行報錯的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10vue項目使用electron-builder庫打包成桌面程序的過程
這篇文章主要介紹了vue項目使用electron-builder庫打包成桌面程序的過程,本文給大家介紹如何使用electron-builder這個庫結(jié)合實例代碼給大家講解的非常詳細,感興趣的朋友一起看看吧2024-02-02