使用element-ui el-table 中有這樣一個需求,須要將合計放在表格內容的第一行,而且點擊合計可跳轉到其它頁面!
框架中提供了合計的屬性方法,這樣能夠進行數值求和及自定義求和,可是,合計那一欄不能添加點擊跳轉功能,結構默認排到最底行,不知足需求element-ui
經過給table傳入span-method方法能夠實現合併行或列,方法的參數是一個對象,裏面包含當前行row、當前列column、當前行號rowIndex、當前列號columnIndex四個屬性。該函數能夠返回一個包含兩個元素的數組,第一個元素表明rowspan,第二個元素表明colspan。 也能夠返回一個鍵名爲rowspan和colspan的對象。數組
解決思路:框架
- 調節樣式;
- 將合計的數據單獨計算出來/接口返回,添加到數組第一個位置,這樣就能夠很好的控制合計這一行了。
HTML
<el-table ref="tableData" tooltip-effect="dark" style="width: 100%" border :data="tableData" v-loading="loading" :span-method="arraySpanMethod"> <el-table-column type="index" label="序號" align="center" width="55" :index="indexFun"></el-table-column> <el-table-column prop="name" align="center" label="姓名"> <template slot-scope="scope"> <el-button type="text" size="small" @click="goLink(scope.row)">{{scope.row.name}}</el-button> </template> </el-table-column> </el-table>
JS
// 插入合計的數據 summaryFun(){ var obj = [「合計」,......]; this.tableData.unshift(obj); }, // 合併合計第一行 arraySpanMethod({ row, column, rowIndex, columnIndex }) { if (rowIndex === 0) { if (columnIndex === 0) { return [0, 0]; } else if (columnIndex === 1) { return [1, 2]; } } }, // 表格序號 除去合計從第一開始,以下圖 indexFun (index) { return index; }, // 點擊合計進行跳轉 goLink(row){ if(row.id == "合計"){window.loaction.href=""} }
合計三行合併並可點擊
arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合併第一行 if (rowIndex === 0) { if (columnIndex === 0) { return [0, 0]; } else if (columnIndex === 1) { return [1, 3]; } else if (columnIndex === 2) { return [0, 0]; } } },
合併第一行四列 取 第三個值(寫了這麼多案例,能看出合併的方法了吧)
arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合併第一行 // if (rowIndex === 0) { // if (columnIndex === 0) { // return [0, 0]; // } else if (columnIndex === 1) { // return [0, 0]; // }else if (columnIndex === 2) { // return [1, 3]; // } // } if (rowIndex === 0) { if (columnIndex === 0) { return [0, 0]; } else if (columnIndex === 1) { return [0, 0]; }else if (columnIndex === 2) { return [1, 4]; }else if (columnIndex === 3) { return [0, 0]; } } },