element?table?點(diǎn)擊某一行中按鈕加載功能實(shí)現(xiàn)
在Element UI中,實(shí)現(xiàn)表格(element-table)中的這種功能通常涉及到數(shù)據(jù)處理和狀態(tài)管理。當(dāng)你點(diǎn)擊某一行的按鈕時(shí),其他行的按鈕需要?jiǎng)討B(tài)地切換為加載狀態(tài),這可以通過以下步驟實(shí)現(xiàn):
1.表格組件:使用el-table組件,并且為每一行的按鈕添加事件監(jiān)聽器,如@click。
<el-table :data="items" @selection-change="handleSelectionChange"> <el-table-column type="index"></el-table-column> <el-table-column prop="action" label="操作"> <template slot-scope="scope"> <el-button v-if="!isLoading[scope.$index]" @click="loadData(scope.$index)" :disabled="isLoading[scope.$index]">加載</el-button> <!-- 加載中狀態(tài) --> <span v-if="isLoading[scope.$index]">加載中...</span> </template> </el-table-column> </el-table>
2.狀態(tài)管理:維護(hù)一個(gè)數(shù)組isLoading,用于跟蹤每行的數(shù)據(jù)加載狀態(tài)。初始設(shè)置所有元素為false。
data() { return { items: [], isLoading: Array.from({ length: this.items.length }, () => false),//此處可以放在接口中 }; },
3.加載方法:在loadData方法中,當(dāng)點(diǎn)擊某一行的按鈕時(shí),更新對(duì)應(yīng)索引處的狀態(tài)為true,然后調(diào)用實(shí)際的數(shù)據(jù)加載API。同時(shí),可以設(shè)置一個(gè)異步操作來模擬加載過程,例如使用axios或Promise。
methods: { loadData(index) { this.isLoading[index] = true; // 設(shè)置為加載狀態(tài) this.fetchData(index).then(() => { // 調(diào)整狀態(tài)為已完成 this.isLoading[index] = false; //如果沒有響應(yīng)式用下面的設(shè)置 //this.$set(this.isLoading, index, false); }).catch(() => { // 處理加載失敗 }); }, fetchData(index) { return new Promise((resolve, reject) => { // 模擬異步加載 setTimeout(() => { // 假設(shè)這里是你獲取數(shù)據(jù)的邏輯 this.items[index].dataToLoad = '數(shù)據(jù)內(nèi)容'; resolve(); }, 2000); // 加載時(shí)間 }); }, },
4.選擇改變事件:使用@selection-change事件監(jiān)聽用戶選擇的行,確保只對(duì)選中的行執(zhí)行加載操作。
handleSelectionChange(selection) { selection.forEach((index) => { this.loadData(index); }); },
到此這篇關(guān)于element table 點(diǎn)擊某一行中按鈕加載功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)element table點(diǎn)擊按鈕加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單談?wù)刯avascript中的變量、作用域和內(nèi)存問題
這篇文章主要介紹了簡單談?wù)刯avascript中的變量、作用域和內(nèi)存問題的相關(guān)資料,需要的朋友可以參考下2015-08-08getElementsByTagName vs selectNodes效率 及兼容的selectNodes實(shí)現(xiàn)
天在csdn上看到有人問 getElementsByTagName 和 selectNodes誰更快 ,這個(gè)還真沒研究過。2010-02-02JS小功能(setInterval實(shí)現(xiàn)圖片效果顯示時(shí)間)實(shí)例代碼
這篇文章主要介紹了setInterval實(shí)現(xiàn)圖片效果顯示時(shí)間實(shí)例代碼,有需要的朋友可以參考一下2013-11-11