使用element表格開發(fā)時(shí)需要用到expand展開,官網(wǎng)示例沒有手風(fēng)琴展開效果目尖,那就自己想辦法實(shí)現(xiàn)
Table Methods中有這么一個(gè)方法馒吴,應(yīng)該可以實(shí)現(xiàn),現(xiàn)在就是改造table
Snipaste_2021-03-05_11-09-06.png
- 要實(shí)現(xiàn)展開瑟曲,需要設(shè)置type=expand,這是默認(rèn)的展開行饮戳,這個(gè)現(xiàn)在肯定是不滿足我們的需求,所以設(shè)置width=1(隱藏原本的展開列)测蹲,這樣設(shè)置完后頁面中會(huì)有展開圖標(biāo),css設(shè)置一下display:none即可
<el-table-column type="expand" width="1">
<template slot-scope="scope">
<physics-data :row="scope.row"></physics-data>
</template>
</el-table-column>
- 自定義實(shí)現(xiàn)鬼吵,那么我們就需要有一列來充當(dāng)原本展開行的角色
<el-table-column width="60">
<template slot-scope="scope">
<span
class="table-expand-default el-icon-arrow-right"
:class="{'current-row_expanded': currentRowId === scope.row.id && scope.row.expanded}"
@click="expanedChange(scope.row)"></span>
</template>
</el-table-column>
- 自定義展開/收起事件扣甲,這里需要注意,因?yàn)槲覀兪鞘褂米远x列代替原本展開的角色,所以為了保持原有功能琉挖,這里需要注意以下幾點(diǎn)启泣,(如果使用文字/按鈕作為展開點(diǎn)擊按鈕,以下步驟可以忽略)
- 需要?jiǎng)討B(tài)綁定一個(gè)class來處理當(dāng)前行展開/收起時(shí)按鈕圖標(biāo)顯示示辈,
- toggleRowExpansion()實(shí)現(xiàn)展開/收起寥茫,要實(shí)現(xiàn)手風(fēng)琴,我們需要先遍歷所有數(shù)據(jù)矾麻,收起展開行纱耻,將當(dāng)前點(diǎn)擊行展開,注意遍歷時(shí)里面的操作险耀;
- 前面兩步完成后弄喘,發(fā)現(xiàn)當(dāng)我們點(diǎn)擊自定義展開按鈕,可以實(shí)現(xiàn)手風(fēng)琴效果甩牺,但是在當(dāng)前行點(diǎn)擊時(shí)會(huì)有bug出現(xiàn)蘑志,按鈕圖標(biāo)的展示是亂的,這里就需要再加一層判斷贬派,代碼在下面急但,這里只做記錄
export default = {
methods:{
expanedChange(row){
let appTable = this.$refs.appTable
if(!this.currentRowId){
this.currentRowId = row.id
row.expanded = true;
appTable.toggleRowExpansion(row)
}else if(this.currentRowId === row.id){
if(row.expanded){
appTable.toggleRowExpansion(row,false)
}else{
appTable.toggleRowExpansion(row)
}
row.expanded = !row.expanded
}else if(row.id !== this.currentRowId){
this.currentRowId = row.id
row.expanded = true
this.tableData.map(item => {
if(row.id !== item.id){
appTable.toggleRowExpansion(item,false)
}
})
appTable.toggleRowExpansion(row)
}
},
}
}
注意點(diǎn):
在開發(fā)過程中,剛開始的實(shí)現(xiàn)是使用table原有的方法 expand-change 搞乏,但是在這個(gè)方法中去實(shí)現(xiàn)手風(fēng)琴的效果會(huì)造成程序卡死波桩,控制臺一看是有無限循環(huán)造成zhan