当前位置: 首页 > news >正文

Virtualized Table 虚拟化表格 el-table-v2 表头分组 多级表头的简单示例

注意添加这个属性,会影响到有多少个层级的表头: :header-height=“[50, 40]”,即后面的columnIndex

如果有fix的列CustomizedHeader会被调用多次,如果有多个层级的表头,也会被调用多次, 实际被调用次数是(fix数+ 1 * 表头层级数量)

以下代码均删除了JSX

TS版本代码

<template><div  style="width: 100%;height: calc(100vh - 84px)"><el-auto-resizer><template #default="{ height, width }"><el-table-v2fixed:columns="columns":data="data":sort-by="sortBy":header-height="[50, 40]":header-class="headerClass"@column-sort="onSort":width="width":height="height"><template #header="props"><customized-header v-bind="props"></customized-header></template></el-table-v2></template></el-auto-resizer></div>
</template><script lang="ts" setup>
import {h, ref} from 'vue'
import {TableV2FixedDir, TableV2Placeholder, TableV2SortOrder} from 'element-plus'
import type {HeaderClassNameGetter,TableV2CustomizedHeaderSlotParam,
} from 'element-plus'
import type {SortBy} from 'element-plus'// 生成带二级表头的 columns
function generateColumns(length = 10, prefix = 'column-', props?: any) {return Array.from({length}).map((_, columnIndex) => ({...props,key: `${prefix}${columnIndex}`,dataKey: `${prefix}${columnIndex}`,title: `Column ${columnIndex}`,width: 150,}))
}function generateData(columns: ReturnType<typeof generateColumns>,length = 200,prefix = 'row-'
) {return Array.from({ length }).map((_, rowIndex) => {return columns.reduce((rowData, column, columnIndex) => {rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`return rowData},{id: `${prefix}${rowIndex}`,parentId: null,})})
}function CustomizedHeader({cells, columns, headerIndex}) {if (headerIndex === 1) return cellsconst groupCells = []let width = 0let idx = 0columns.forEach((column, columnIndex) => {if (column.placeholderSign === TableV2Placeholder) {groupCells.push(cells[columnIndex])} else {width += cells[columnIndex].props.column.widthidx++const nextColumn = columns[columnIndex + 1]if (columnIndex === columns.length - 1 ||nextColumn?.placeholderSign === TableV2Placeholder ||idx === (headerIndex === 0 ? 4 : 2)) {groupCells.push(h('div',{class: 'flex items-center justify-center custom-header-cell',role: 'columnheader',style: {...cells[columnIndex].props.style,width: `${width}px`,border: `2px solid #fff`}},`Group width ${width}`))width = 0idx = 0}}})return groupCells;
}const headerClass = ({headerIndex,}: Parameters<HeaderClassNameGetter<any>>[0]) => {if (headerIndex === 0) return 'el-primary-color'return ''
}const columns = generateColumns(70)
let data = generateData(columns, 20)columns[0].fixed = TableV2FixedDir.LEFTfor (let i = 0; i < 3; i++) columns[i].sortable = trueconst sortBy = ref<SortBy>({key: 'column-0',order: TableV2SortOrder.ASC,
})const onSort = (_sortBy: SortBy) => {data = data.reverse()sortBy.value = _sortBy
}
</script><style>
.el-el-table-v2__header-row .custom-header-cell {border-right: 1px solid var(--el-border-color);
}.el-el-table-v2__header-row .custom-header-cell:last-child {border-right: none;
}.el-primary-color {background-color: var(--el-color-primary);color: var(--el-color-white);font-size: 14px;font-weight: bold;
}.el-primary-color .custom-header-cell {padding: 0 4px;
}
</style>

JS版本

<template><div style="width: 100%;height: calc(100vh - 84px)"><el-auto-resizer><template #default="{ height, width }"><el-table-v2fixed:columns="columns":data="data":sort-by="sortBy":header-height="[50, 40]":header-class="headerClass"@column-sort="onSort":width="width":height="height"><template #header="props"><CustomizedHeader v-bind="props"></CustomizedHeader></template></el-table-v2></template></el-auto-resizer></div>
</template><script setup>
import { h, ref } from 'vue'
import {TableV2FixedDir,TableV2Placeholder,TableV2SortOrder
} from 'element-plus'// 生成带二级表头的 columns
function generateColumns(length = 10, prefix = 'column-') {return Array.from({ length }).map((_, columnIndex) => ({key: `${prefix}${columnIndex}`,dataKey: `${prefix}${columnIndex}`,title: `Column ${columnIndex}`,width: 150}))
}function generateData(columns, length = 200, prefix = 'row-') {return Array.from({ length }).map((_, rowIndex) => {return columns.reduce((rowData, column, columnIndex) => {rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`return rowData},{id: `${prefix}${rowIndex}`,parentId: null})})
}function CustomizedHeader({ cells, columns, headerIndex }) {if (headerIndex === 1) return cellsconst groupCells = []let width = 0let idx = 0columns.forEach((column, columnIndex) => {if (column.placeholderSign === TableV2Placeholder) {groupCells.push(cells[columnIndex])} else {width += cells[columnIndex].props.column.widthidx++const nextColumn = columns[columnIndex + 1]if (columnIndex === columns.length - 1 ||nextColumn?.placeholderSign === TableV2Placeholder ||idx === (headerIndex === 0 ? 4 : 2)) {groupCells.push(h('div',{class: 'flex items-center justify-center custom-header-cell',role: 'columnheader',style: {...cells[columnIndex].props.style,width: `${width}px`,border: `2px solid #fff`}},`Group width ${width}`))width = 0idx = 0}}})return groupCells
}const headerClass = ({ headerIndex }) => {return headerIndex === 0 ? 'el-primary-color' : ''
}const columns = generateColumns(70)
let data = generateData(columns, 20)columns[0].fixed = TableV2FixedDir.LEFTfor (let i = 0; i < 3; i++) {columns[i].sortable = true
}const sortBy = ref({key: 'column-0',order: TableV2SortOrder.ASC
})const onSort = (_sortBy) => {// 创建新数组以保持响应性data = [...data].reverse()sortBy.value = _sortBy
}</script><style>
.el-el-table-v2__header-row .custom-header-cell {border-right: 1px solid var(--el-border-color);
}.el-el-table-v2__header-row .custom-header-cell:last-child {border-right: none;
}.el-primary-color {background-color: var(--el-color-primary);color: var(--el-color-white);font-size: 14px;font-weight: bold;
}.el-primary-color .custom-header-cell {padding: 0 4px;
}
</style>

文章转载自:

http://Y6HEy99N.kscwt.cn
http://FxRMwPmP.kscwt.cn
http://BFL2SDxz.kscwt.cn
http://nJlKzSRJ.kscwt.cn
http://OCULjRAC.kscwt.cn
http://BF6XVpDs.kscwt.cn
http://4A4I1vBV.kscwt.cn
http://xwxNcTHi.kscwt.cn
http://4GRui5LP.kscwt.cn
http://oqmbKT3l.kscwt.cn
http://tHcuxmT2.kscwt.cn
http://tn3C6X2k.kscwt.cn
http://9oDYNG5B.kscwt.cn
http://T0fukjnV.kscwt.cn
http://rk1aRoFm.kscwt.cn
http://JT35ZG8Z.kscwt.cn
http://oL4jKFQo.kscwt.cn
http://aX8bNLPk.kscwt.cn
http://lYhWV19E.kscwt.cn
http://3L5LyyCd.kscwt.cn
http://zGHf3bmi.kscwt.cn
http://6zpTy1Gw.kscwt.cn
http://EIr7I9qc.kscwt.cn
http://xlv0B1M7.kscwt.cn
http://gC42AiN7.kscwt.cn
http://lMNSfhUN.kscwt.cn
http://ssXYJjMP.kscwt.cn
http://92NzXVk8.kscwt.cn
http://wwtVy7IB.kscwt.cn
http://0Ky6nlkW.kscwt.cn
http://www.dtcms.com/a/190636.html

相关文章:

  • 机器学习基础课程-5-课程实验
  • 使用Docker部署MongoDB
  • AI时代的弯道超车之第七章:如何用AI赋能创业?
  • springboot项目启动报错:找不到或无法加载主类
  • SVNAdmin管理使用教程
  • ECharts:数据可视化的强大引擎
  • springboot + mysql8降低版本到 mysql5.7
  • 智能体制作学习笔记2——情感客服
  • ollama升级
  • QListWedget控件使用指南
  • 远程连接电脑的方法?异地远程桌面连接和三方软件实现
  • 海康平台对接关键类
  • Ubuntu摄像头打开失败
  • 4.重建大师菜单栏介绍
  • Solana数据索引问题与解决方案
  • Nginx+Lua 实战避坑:从模块加载失败到版本冲突的深度剖析
  • spark中的转换算子
  • 固定步长和变步长的LMS自适应滤波器算法
  • Qwen集成clickhouse实现RAG
  • WK-F01和WK-F02以及WK-F01在电动三轮车和休闲三轮上的应用比较
  • JVM 与云原生的完美融合:引领技术潮流
  • Qt file文件操作详解
  • 密西根大学新作——LightEMMA:自动驾驶中轻量级端到端多模态模型
  • 服务器时间发生跳变导致hghac中对应主机状态频繁切换为crash或stop
  • OSS-承载数据的巨轮
  • Qt功能区:简介与安装
  • MCU ESP32-S3+SD NAND(贴片式T卡):智能皮电手环(GSR智能手环)性能与存储的深度评测
  • 基于C#实现中央定位服务器的 P2P 网络聊天系统
  • deepseek梳理java高级开发工程师算法面试题
  • windows文件共享另一台电脑资源管理器网络文件夹无法找到机器