修复了跨行数为1,导致首行显示异常的问题,代码链接已更新
不多bb,直接上图,上代码,上iview的代码实验田
功能:
1、可自定义配置跨行数
2、可指定从第x列开始合并
3、可配置尾行是否独立展示->做合计项
https://run.iviewui.com/qEntk4Ta
<template>
<Table :columns="columns" :data="data" border :span-method="handleSpan"></Table>
跨行数(colCross):<Input type="number" style="margin: 20px 0; width:80px" v-model="colCross"></Input>
从第 {{colStart}} 行开始(colStart):<Input type="number" style="margin: 10px 0; width:80px" v-model="colStart"></Input>
尾行是否独立展示(isSetTotalCol):<Switch v-model="isSetTotalCol" />
</template>
<script>
export default {
data () {
return {
isSetTotalCol: true,//是否设置最后一行为 合计项(独立展示)
colCross: 3,//表示跨3行合并
colStart: 1,//表示【跨行合并操作】从第一列开始
columns: [
{
title: 'Date',
key: 'date'
},
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Address',
key: 'address'
}
],
data: [
{
name: 'John Brown',
age: 18,
address: 'New York No. 1 Lake Park',
date: '2016-10-01'
},
{
name: 'Jim Green',
age: 24,
address: 'London No. 1 Lake Park',
date: '2016-10-02'
},
{
name: 'Joe Black',
age: 30,
address: 'Sydney No. 1 Lake Park',
date: '2016-10-03'
},
{
name: 'Jon Snow',
age: 26,
address: 'Ottawa No. 2 Lake Park',
date: '2016-10-04'
},
{
name: 'John Brown',
age: 18,
address: 'New York No. 1 Lake Park',
date: '2016-10-05'
},
{
name: 'Jim Green',
age: 24,
address: 'London No. 1 Lake Park',
date: '2016-10-06'
},
{
name: 'Joe Black',
age: 30,
address: 'Sydney No. 1 Lake Park',
date: '2016-10-07'
},
{
name: '合计',
age: 26,
address: 'Ottawa No. 2 Lake Park',
date: '/'
},
{
name: '合计',
age: 26,
address: 'Ottawa No. 2 Lake Park',
date: '/'
},
]
}
},
computed: {
tabDataLen () {
return this.data.length
}
},
methods: {
handleSpan ({ row, column, rowIndex, columnIndex }) {
//将index从0开始变为从1开始,方便进行取余计算
return this.colCross!=1?this.handleSpanData(rowIndex+1, columnIndex+1): ''
},
handleSpanData(rowIndex, columnIndex){
if(this.isSetTotalCol){
//diff表示当前处理行与最后一行的差值
let diff = this.tabDataLen-rowIndex
if(!diff){
//diff=0 表示当前行为最后一行,需要阻止【跨行合并操作】、【删除操作】
return {rowspan: diff==0?1:0,colspan: diff==0?1:0};
}else if (rowIndex%this.colCross == 1 && columnIndex == this.colStart) {
//当前行数%跨行数 等于1的 时候:进行【跨行合并操作】
//同时不能让它跨行到最后一行
return {rowspan: diff<=(this.colCross-1)?diff:this.colCross,colspan: 1};
}else if (rowIndex%this.colCross != 1 && columnIndex == this.colStart) {
//当前行数%跨行数 不等于1的 时候:进行【删除操作】
//同时需要阻止【跨行合并操作】、【删除操作】
return {rowspan: diff==0?1:0,colspan: diff==0?1:0};
}
} else {
if (rowIndex%this.colCross == 1 && columnIndex == this.colStart) {
//当前行数%跨行数 等于1的 时候:进行【跨行合并操作】
return {rowspan: this.colCross,colspan: 1};
}else if (rowIndex%this.colCross != 1 && columnIndex == this.colStart) {
//当前行数%跨行数 不等于1的 时候:进行【删除操作】
return {rowspan: 0,colspan:0};
}
}
}
}
}
</script>