element中el-table局部刷新的實(shí)現(xiàn)示例
介紹:有一個(gè)簡單的需求,一個(gè)表格點(diǎn)擊一個(gè)單元格的按鈕后,按鈕變成進(jìn)度條。
問題:如果只是用一個(gè)變量,然后配合v-if使用,一整列都會(huì)變成進(jìn)度條。
想法:把那個(gè)變量渲染進(jìn)去列表的每一行。但是…問題又出現(xiàn)了,值變了,但是表格沒有被重新渲染。
重點(diǎn):this.$set(this.tableData, index, row),更新表格這一行的值。理解這句代碼的意思,后面的可以不看。
隨便放點(diǎn)前端代碼就很長了,不放又清除概況,簡單看看吧。直接看el-progress那個(gè)標(biāo)簽的附近的,其他的瞅一下知道大概就好。
<el-table
ref="theTable"
:data="tableData.filter(data => !search || data.id.toLowerCase().includes(search.toLowerCase()))"
class="content-datasheet-table"
:cell-style="columnStyle"
>
<el-table-column label="開關(guān)名稱" prop="name" width="100px" align="center"> </el-table-column>
<el-table-column label="開關(guān)ID" prop="sensorId" width="100px" align="center"> </el-table-column>
<el-table-column label="電壓值(V)" prop="voltage" width="100px" align="center"> </el-table-column>
<el-table-column label="電流值(A)" prop="current" width="100px" align="center"> </el-table-column>
<el-table-column label="功率值(W)" prop="power" width="100px" align="center"> </el-table-column>
<el-table-column label="狀態(tài)圖" align="center">
<!-- eslint-disable-next-line -->
<template slot-scope="scope">
<viewer style="transform:rotate(-90deg)" rotateTo="-90">
<img :src="viewPicture(scope.row)" min-width="70" height="70" :key="scope.$index" />
</viewer>
</template>
</el-table-column>
<el-table-column label="刷新時(shí)間" prop="statusTime" width="160px" align="center"> </el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-progress
v-if="scope.row.progressBar"
:text-inside="true"
:stroke-width="18"
:percentage="scope.row.speed"
status="success"
></el-progress>
<el-button size="mini" v-else @click="updateConfirm(scope.$index, scope.row)">獲取最新狀態(tài)</el-button>
</template>
</el-table-column>
<el-table-column align="right">
<!-- eslint-disable-next-line -->
<template slot="header" slot-scope="scope">
<el-input v-model="search" size="medium" clearable placeholder="輸入開關(guān)ID搜索" />
</template>
<template slot-scope="scope">
<el-switch
v-model="scope.row.status"
active-color="#2d8cf0"
nactive-color="#ff4949"
:active-value="true"
:inactive-value="false"
active-text="閉合"
inactive-text="斷開"
@change="switchConfirm(scope.row.status, scope.row.sensorId)"
/>
</template>
</el-table-column>
</el-table>前端頁面顯示圖:點(diǎn)擊獲取最新狀態(tài)之后,獲取變成進(jìn)度條,開始跑進(jìn)度。


// 更新當(dāng)前某個(gè)開關(guān)的最新狀態(tài)
handlePicture(index, row) {
let params = "sensor_id=" + row.sensorId;
this.progressSpeed(index, row);
newestStatus(params).then(res => {
row.speed = 100;
this.$set(this.tableData, index, row);
setTimeout(() => {
row.progressBar = false;
this.switchData();
}, 3000);
});
},
// 進(jìn)度條刷新
progressSpeed(index, row) {
row.speed = 0;
row.progressBar = true;
const sp = setInterval(() => {
console.log();
if (row.speed >= 99 ) {
clearInterval(sp);
} else {
row.speed++;
}
this.$set(this.tableData, index, row);
}, 600);
},按鈕變成進(jìn)度條,獲取最新數(shù)據(jù),進(jìn)度條到99的時(shí)候就卡住,信息返回后,進(jìn)度條不管跑了都少都變成100,3S后變回原來的樣子。
到此這篇關(guān)于element中el-table局部刷新的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)element el-table局部刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue簡單練習(xí) 桌面時(shí)鐘的實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了vue簡單練習(xí) 桌面時(shí)鐘的實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值的相關(guān)資料,需要的朋友可以參考下2019-09-09
VUE項(xiàng)目自動(dòng)檢測服務(wù)端是否發(fā)布了新版本
本文主要介紹了VUE項(xiàng)目自動(dòng)檢測服務(wù)端是否發(fā)布了新版本,通過輪詢方式檢測Vue項(xiàng)目新版本并提示用戶刷新頁面,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
vue3(optionApi)使用Element Plus庫沒有效果的解決方式
這篇文章主要介紹了vue3(optionApi)使用Element Plus庫沒有效果的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
手把手教你vue實(shí)現(xiàn)動(dòng)態(tài)路由
動(dòng)態(tài)路由可以根據(jù)不同用戶登錄獲取不一樣的路由層級,可隨時(shí)調(diào)配路由,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

