前端vue中el-table增加合計行及合并單元格代碼示例
更新時間:2023年09月26日 11:32:11 作者:槍白
在有些情況下我們會有合并表頭、合并列、合并尾部合計的需求,這篇文章主要給大家介紹了關(guān)于前端vue中el-table增加合計行及合并單元格的相關(guān)資料,需要的朋友可以參考下
自己總結(jié)的,不好別噴,謝謝~~~
先看效果圖

表格觸發(fā)調(diào)用方法
<el-table :align="rowTableCenter" id="tables" :data="data"
:summary-method="addTotal" :show-summary="true" border>
<el-table-column label="序號" type="index" align="center"/>合計行代碼
在 合計行方法中 直接去修改了表格樣式
//合計行
addTotal(param) {
const { columns, data } = param;
// console.log(columns)
const sums = [];
columns.forEach((column, index) => {
if (index === 0 ) {
console.log(column)
column.colSpan = 4; //行占用格數(shù)
column.rowSpan = 1; //列占用格數(shù)
sums[index] = '權(quán)重得分合計';
}
if(index !== 0 && index !== 4){
column.rowSpan = 0;
column.colSpan = 0;
// column.style = {
// "display":"none" //這里加了但是沒有作用效果
// }
console.log(column)
return;
}
const values = data.map(item => Number(item[column.property]));
if(index === 4){
if (!values.every(value => isNaN(value))) {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr);
if (!isNaN(value)) {
return prev + curr;
} else {
return prev;
}
}, 0);
} else {
// sums[index] = 'N/A';
}
}
});
//在合計行方法中直接去修改了單元格樣式
//合并單元格
this.$nextTick(() => {
const tds = document.querySelectorAll('#tables .el-table__footer tr>td');
console.log(tds)
tds.forEach(function (val, index) {
if (tds[0].innerText === '權(quán)重得分合計') {
if(index !== 0 && index !== 4){
val.style.display = 'none';
}else{
val.style.textAlign = 'center';
}
// val.style.display = 'none';
// val.style.fontSize = '16px';
// val.style.borderColor = '#aaaaaa';
// val.style.color = '#fff';
}
})
}, 1000)
return sums;
},總結(jié)
到此這篇關(guān)于前端vue中el-table增加合計行及合并單元格的文章就介紹到這了,更多相關(guān)vue el-table增加合計行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
van-picker組件default-index屬性設(shè)置不生效踩坑及解決
這篇文章主要介紹了van-picker組件default-index屬性設(shè)置不生效踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue WatchEffect函數(shù)創(chuàng)建高級偵聽器
watchEffect傳入的函數(shù)會被立即執(zhí)行一次,并且在執(zhí)行的過程中會收集依賴;其次,只有收集的依賴發(fā)生變化時,watchEffect傳入的函數(shù)才會再次執(zhí)行2023-03-03

