基于a-table的横向表头合并和纵向表头合并

(一)需求:需要实现纵向和横向的标题合并

(二)方案

     使用antd的a-table组件,分别对data和column进行操作,使用api文档里的合并属性。

(1)横向表头合并/包含:使用columns[n] 内嵌 children

const columns = ref([
    { title: '一级表头1', dataIndex: 'value0', key: 'key', align: 'center', width: 50 },
    {
        title: '一级表头2',
        align: 'center',
        children: [
            { title: '二级表头1', dataIndex: 'value1', key: 'key', align: 'center', width: 50 },
            { title: '二级表头2', dataIndex: 'value2', key: 'key', align: 'center', width: 50 },
            { title: '二级表头3', dataIndex: 'value3', key: 'key', align: 'center', width: 50 },
        ]
    }
])

(2)纵向标题合并:在column中设置对应列下rowSpan的值

const columns = ref([
		{
			title: '用电分类',
			dataIndex: 'firstName',
			key: 'key',
			align: 'center',
			colSpan: 2,
			customCell: (row, index) => {
				if (row.firstName === '工商业用电') {
					return { rowSpan: 6 }
				}
				return { rowSpan: 0 }
			},
			width: 50
		},
		{
			title: '用电分类',
			dataIndex: 'classify',
			key: 'key',
			align: 'center',
			colSpan: 2,
			width: 50,
			customCell: (row, index) => {
				if (row.classify == '单一制') {
					return { rowSpan: 3 }
				}
				if (row.classify == '两部制') {
					return { rowSpan: 4 }
				}
				return { rowSpan: 0 }
			}
		}
])
const data = ref([
		{
			firstName: '工商业用电',
			classify: '单一制',
			voltage: '不满1千伏',
			value: 32,
			key: 1
		},
		{ firstName: '', classify: '', voltage: '1-10千伏', value: -1, key: 2 },
		{ firstName: '', classify: '', voltage: '35千伏', value: 32, key: 3 },
		{
			firstName: '',
			classify: '两部制',
			voltage: '不满1千伏',
			value: 32,
			key: 4
		},
		{ firstName: '', classify: '', voltage: '1-10千伏', value: 42, key: 5 },
		{ firstName: '', classify: '', voltage: '35千伏', value: 32, key: 6 }
	])

完整代码:

<template>
    <a-table
		class="table-style"
		style="margin-top: 10px"
		:columns="columns"
		:dataSource="data"
		bordered
		:pagination="false"
		tableLayout="auto"
		size="small"
	/>
</template>
<script setup name="jsonTable">

const columns = ref([
		{
			title: '用电分类',
			dataIndex: 'firstName',
			key: 'key',
			align: 'center',
			colSpan: 2,
			customCell: (row, index) => {
				if (row.firstName === '工商业用电') {
					return { rowSpan: 6 }
				}
				return { rowSpan: 0 }
			},
			width: 50
		},
		{
			title: '',
			dataIndex: 'classify',
			key: 'key',
			align: 'center',
			colSpan: 0,
			customCell: (row, index) => {
				if (row.classify === '单一制') {
					return { rowSpan: 3 }
				}
				if (row.classify === '两部制') {
					return { rowSpan: 3 }
				}
				return { rowSpan: 0 }
			},
			width: 50
		},
		{ title: '电压等级', dataIndex: 'voltage', key: 'key', align: 'center', width: 50 },
		{ title: '电度用电价格', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
		{
			title: '其他',
			align: 'center',
			children: [
				{ title: '代理购电价格', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '上网环节折损', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '电度输配电价', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '系统运行费用折价', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '政府性基金及附加', dataIndex: 'value', key: 'key', align: 'center', width: 50 }
			]
		},
		{
			title: '分时用电价格',
			align: 'center',
			children: [
				{ title: '尖峰时段', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '高峰时段', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '平时段', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '低谷时段', dataIndex: 'value', key: 'key', align: 'center', width: 50 }
			]
		},
		{
			title: '容(需)量用电价格',
			align: 'center',
			children: [
				{ title: '最大需量', dataIndex: 'value', key: 'key', align: 'center', width: 50 },
				{ title: '变压器容', dataIndex: 'value', key: 'key', align: 'center', width: 50 }
			]
		}
	])

const data = ref([
		{
			firstName: '工商业用电',
			classify: '单一制',
			voltage: '不满1千伏',
			value: 32,
			key: 1
		},
		{ firstName: '', classify: '', voltage: '1-10千伏', value: -1, key: 2 },
		{ firstName: '', classify: '', voltage: '35千伏', value: 32, key: 3 },
		{
			firstName: '',
			classify: '两部制',
			voltage: '不满1千伏',
			value: 32,
			key: 4
		},
		{ firstName: '', classify: '', voltage: '1-10千伏', value: 42, key: 5 },
		{ firstName: '', classify: '', voltage: '35千伏', value: 32, key: 6 }
	])

</script>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于 el-table 组件,可以通过设置表头的 column 对象的 span 属性来合并单元格。span 属性可以设置为一个对象,包含两个属性:row 和 col,分别表示纵向横向合并单元格数量。 以下是一个示例代码,演示了如何在 el-table 中实现多级表头的单元格合并: ```html <template> <el-table :data="tableData" style="width: 100%"> <el-table-column label="一级表头" prop="name" :span="{ row: 2, col: 1 }"></el-table-column> <el-table-column label="二级表头" :span="{ row: 1, col: 2 }"></el-table-column> <el-table-column label="三级表头" prop="age" :span="{ row: 1, col: 2 }"></el-table-column> <el-table-column label="四级表头" prop="address" :span="{ row: 1, col: 2 }"></el-table-column> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="地址" prop="address"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 20, address: 'New York' }, { name: 'Jane', age: 25, address: 'London' } ] }; } }; </script> ``` 在这个示例中,我们使用了 el-table 组件,并设置了四个表头,分别是"一级表头"、"二级表头"、"三级表头"和"四级表头"。通过设置每个表头的 span 属性,指定了单元格的合并方式。 注意,设置 span 属性的时候,row 和 col 的值分别代表纵向横向合并单元格数量。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值