vue+elementUI實(shí)現(xiàn)內(nèi)嵌table的方法示例
在大四實(shí)習(xí)工作中碰到一個(gè)比較特別的需求,要求在一個(gè) table 表格中點(diǎn)擊一條數(shù)據(jù)的編號(hào),在該條數(shù)據(jù)下方出現(xiàn)一個(gè)新的 table 表格。這個(gè)需求在 element UI 的官方文檔中也有案例,以下是參考了其他博客(找不到博客地址了)和 element UI 之后的最終實(shí)現(xiàn)效果。
效果圖
不知道是公司電腦比較拉,還是軟件問題導(dǎo)致錄制出來的動(dòng)態(tài)圖很卡,但是在實(shí)際操作并不會(huì)卡。

代碼:
頁面顯示代碼:
主要代碼在于 <el-table-column type="expand" width="1" >
type="expand" 官方解釋:
通過設(shè)置 type="expand" 和 Scoped slot 可以開啟展開行功能,el-table-column 的模板會(huì)被渲染成為展開行的內(nèi)容,展開行可訪問的屬性與使用自定義列模板時(shí)的 Scoped slot 相同。
而最后通過 width="1" 將 el-table-column 設(shè)置為 type="expand"之后自帶顯示的 箭頭 圖標(biāo)隱藏。
整個(gè)代碼流程大致如下:
數(shù)據(jù)表分為 主表和子表,頁面最開始加載時(shí)展示主表 table 數(shù)據(jù)(也就是普通的 table 數(shù)據(jù)表格,就叫它主 table )。當(dāng)我點(diǎn)擊主 table 中的一行數(shù)據(jù)的導(dǎo)航名稱時(shí),觸發(fā)一個(gè)點(diǎn)擊事件 toogleExpand() 。這個(gè)事件的主要功能是根據(jù)點(diǎn)擊數(shù)據(jù)的 ID 去后臺(tái)查找與其對(duì)應(yīng)的子表數(shù)據(jù)返回到前端,最后前端在進(jìn)行展示。
這過程比較值得注意的是,拿到對(duì)應(yīng)數(shù)據(jù)的子表數(shù)據(jù)之后,怎么與主表數(shù)據(jù)一一綁定。
這里我是通過使用 Map 的方式實(shí)現(xiàn)的,通過以主表 ID 為 key ,子表數(shù)據(jù)為 value .
// 使用 map 結(jié)構(gòu)的方式保存翻譯列表 this.WebsiteLangMap.set(id,response.rows)
到了這一步還存在一個(gè)問題。
在頁面剛加載的時(shí)候第一次點(diǎn)擊打開對(duì)應(yīng)的內(nèi)嵌 table 是沒有數(shù)據(jù)的,只有第二次點(diǎn)擊打開才會(huì)有數(shù)據(jù)。
因?yàn)槭?table 第一次渲染的時(shí)候 我們保存的子表數(shù)據(jù)的 Map 是沒有數(shù)據(jù)的,所以第一次點(diǎn)擊并不會(huì)顯示數(shù)據(jù)(沒有數(shù)據(jù)怎么渲染)。而我們獲取數(shù)據(jù)是在點(diǎn)擊事件 toogleExpand() 觸發(fā)之后會(huì)請(qǐng)求到的數(shù)據(jù)。也就是先渲染后才有數(shù)據(jù)。原先我以為它能像監(jiān)聽器一樣,能實(shí)時(shí)監(jiān)聽 data 的變化而渲染頁面,然而并不能。
解決辦法是:控制 el-table 的 key 屬性,在子表數(shù)據(jù)發(fā)生變化是改變的 key 的值。
this.websiteLangTableKey = !this.websiteLangTableKey;
以下是核心代碼:
<el-table
v-loading="loading"
:data="websiteList"
@selection-change="handleSelectionChange"
ref="table"
key="websiteTable"
row-key="id"
style="width: 100%; maigin-bottom: 20px;"
border
>
<el-table-column type="selection" width="55" align="center" />
<!-- <el-table-column :label="td('主鍵')" align="center" prop="id" /> -->
<el-table-column :label="td('上級(jí)導(dǎo)航')" align="center" prop="parentId" />
<el-table-column :label="td('導(dǎo)航名稱')" align="center" prop="barName" >
<template slot-scope="scope" >
<el-link type="primary" :underline="false" @click="toogleExpand(scope.row)" >
{{scope.row.barName}}
</el-link>
</template>
</el-table-column>
<el-table-column :label="td('是否鏈接')" align="center" prop="isLink" />
<el-table-column :label="td('鏈接地址')" align="center" prop="url" />
<el-table-column :label="td('創(chuàng)建日期')" align="center" prop="createTime" />
<el-table-column :label="td('創(chuàng)建用戶')" align="center" prop="createBy" />
<el-table-column :label="td('更新時(shí)間')" align="center" prop="updateTime" />
<el-table-column :label="td('更新用戶')" align="center" prop="updateBy" />
<el-table-column :label="td('備注')" align="center" prop="remark" />
<el-table-column :label="td('操作')" align="center" width="130" class-name="small-padding fixed-width" fixed="right">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['cms:website:edit']"
>{{td("修改")}} </el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['cms:website:remove']"
>{{td("刪除")}}</el-button>
</template>
</el-table-column>
<el-table-column type="expand" width="1" >
<template slot-scope="scope">
<el-table
v-loading="loading"
style="width: 100%"
row-key="langId"
:key="websiteLangTableKey"
:data="WebsiteLangMap.get(scope.row.id)"
class="table-in-table"
>
<!-- <el-table-column :label="td('ID主鍵')" align="center" prop="langId" /> -->
<!-- <el-table-column :label="td('導(dǎo)航ID')" align="center" prop="webId" /> -->
<el-table-column :label="td('語言編號(hào)')" align="center" prop="langCode" />
<el-table-column :label="td('語言名稱')" align="center" prop="langName" />
<el-table-column :label="td('中文')" align="center" prop="langCn" />
<el-table-column :label="td('語言翻譯')" align="center" prop="langTranslate" />
<el-table-column :label="td('備注')" align="center" prop="remark" />
<el-table-column :label="td('操作')" align="center" width="130" class-name="small-padding fixed-width" fixed="right">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdateLang(scope.row)"
v-hasPermi="['cms:websiteLang:edit']"
>{{td("修改")}} </el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDeleteLang(scope.row)"
v-hasPermi="['cms:websiteLang:remove']"
>{{td("刪除")}}</el-button>
</template>
</el-table-column>
</el-table>
</template>
</el-table-column>
</el-table>
// 內(nèi)嵌 table
toogleExpand(row) {
this.getListLang(row.id);
let $table = this.$refs.table;
$table.toggleRowExpansion(row)
},
/** 查詢導(dǎo)航翻譯列表 */
getListLang(id) {
// 根據(jù) 導(dǎo)航ID 獲取翻譯列表
this.LangQueryParams.webId = id;
listWebsiteLang(this.LangQueryParams).then(response => {
// 使用 map 結(jié)構(gòu)的方式保存翻譯列表
this.WebsiteLangMap.set(id,response.rows)
this.websiteLangTableKey = !this.websiteLangTableKey;
this.resetLang();
});
},
<style lang="scss" scoped>
.app-container {
::v-deep {
.el-table th {
background: #ddeeff;
}
.el-table__expanded-cell {
border-bottom: 0px;
border-right: 0px;
padding: 0px 0px 0px 47px;
}
}
.table-in-table {
border-top: 0px;
}
}
</style>
到此這篇關(guān)于vue+elementUI實(shí)現(xiàn)內(nèi)嵌table的方法示例的文章就介紹到這了,更多相關(guān)vue element內(nèi)嵌table內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用Proxy監(jiān)聽所有接口狀態(tài)的方法實(shí)現(xiàn)
這篇文章主要介紹了Vue使用Proxy監(jiān)聽所有接口狀態(tài)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
vue中實(shí)現(xiàn)圖片壓縮 file文件的方法
這篇文章主要介紹了vue中實(shí)現(xiàn)圖片壓縮 file文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
vue3中實(shí)現(xiàn)組件通信的方法總結(jié)
在Vue3中,有多種方法可以實(shí)現(xiàn)組件之間的通信,本文就通過代碼示例給大家總結(jié)一些vue3實(shí)現(xiàn)組件通信的常用方法,需要的朋友可以參考下2023-06-06
Vuejs仿網(wǎng)易云音樂實(shí)現(xiàn)聽歌及搜索功能
這篇文章主要介紹了Vuejs仿網(wǎng)易云音樂實(shí)現(xiàn)聽歌及搜索功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Vue 構(gòu)造選項(xiàng) - 進(jìn)階使用說明
這篇文章主要介紹了Vue 構(gòu)造選項(xiàng) - 進(jìn)階使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能
這篇文章主要介紹了.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Vue中引入使用patch-package為依賴打補(bǔ)丁問題
這篇文章主要介紹了Vue中引入使用patch-package為依賴打補(bǔ)丁問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
關(guān)于vue.js中this.$emit的理解使用
本文主要介紹了關(guān)于vue.js中this.$emit的理解使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

