9.8 KiB
9.8 KiB
表格高级模式
目录
- 通用表格 (commonTable)
- 树形表格 (treeTable)
- 分组表格 (groupingTable)
- 合并单元格 (mergeTable)
- 锁定列 (lockTable)
- 批注表格 (postilTable)
- 行内编辑 (redactTable)
- 标记表格 (signTable)
- 打印表格 (printTable)
- 统计表格 (statisticsTable)
- 扩展表格 (extension)
- 复杂表头 (complexHeader)
- 表格树 (tableTree)
源码位置
$ccWorkDir/web/apps/jnpf-web-apps-main/src/views/extend/tableDemo/
通用表格 (commonTable)
源码
tableDemo/commonTable/index.vue
核心模式
const [registerTable, { reload }] = useVxeTable({
api: getTableList, // 数据接口
columns, // 列定义
useSearchForm: true, // 启用搜索表单
formConfig: { // 搜索表单配置
schemas: searchSchemas
},
actionColumn: { // 操作列
width: 100,
title: '操作',
dataIndex: 'action'
}
});
列定义
const columns: BasicColumn[] = [
{ title: '名称', dataIndex: 'name', width: 150 },
{ title: '编码', dataIndex: 'code', width: 120 },
{ title: '状态', dataIndex: 'status', slots: { default: 'status' } },
{ title: '创建时间', dataIndex: 'createTime', width: 180, format: 'date|YYYY-MM-DD' },
{ title: '操作', dataIndex: 'action', width: 120, fixed: 'right' }
];
树形表格 (treeTable)
源码
tableDemo/treeTable/index.vue
核心模式
// 左侧树 + 右侧表格
<BasicLeftTree
ref="leftTreeRef"
:tree-data="industryTypeList"
show-toolbar
title="项目分类"
@select="handleTreeSelect"
/>
// 表格搜索参数
const searchInfo = reactive({ typeId: '' });
const [registerTable, { reload }] = useVxeTable({
api: getTableListByType,
searchInfo, // 传入树选择的参数
immediate: false // 延迟加载
});
关键点
BasicLeftTree: 左侧树组件searchInfo: 响应式对象,接收树选择的值immediate: false: 延迟加载,等树选择后再加载
分组表格 (groupingTable)
源码
tableDemo/groupingTable/index.vue
核心模式
const [registerTable, { reload }] = useVxeTable({
api: getTableListAll,
isTreeTable: true, // 开启树形模式
pagination: false, // 禁用分页
afterFetch: (data) => {
// 将扁平数据转换为树结构
let list = cloneDeep(industryTypeList);
for (const e of list) {
e.isParent = true;
e.children = [];
for (const datum of data) {
if (e.id == datum.projectType) e.children.push(datum);
}
}
return list;
}
});
关键点
isTreeTable: true: 启用分组pagination: false: 分组模式下通常禁用分页afterFetch: 数据转换,构造父子关系
合并单元格 (mergeTable)
源码
tableDemo/mergeTable/index.vue
核心模式
const [registerTable, { reload }] = useVxeTable({
spanMethod: ({ row, column, rowIndex, columnIndex }) => {
// 合并相同值的单元格
if (columnIndex === 4 || columnIndex === 6) {
if (rowIndex > 0 && row[column.property] === tableData[rowIndex - 1][column.property]) {
return { rowspan: 0, colspan: 0 };
}
let rspan = 1;
for (let i = rowIndex + 1; i < tableData.length; i++) {
if (tableData[i][column.property] === row[column.property]) {
rspan++;
} else break;
}
return { rowspan: rspan, colspan: 1 };
}
}
});
关键点
spanMethod: 合并算法,返回{rowspan, colspan}- 返回
{rowspan: 0, colspan: 0}表示被合并
锁定列 (lockTable)
源码
tableDemo/lockTable/index.vue
核心模式
const columns: BasicColumn[] = [
{ title: '项目名称', dataIndex: 'projectName', minWidth: 200, fixed: 'left' },
{ title: '项目编码', dataIndex: 'projectCode', width: 160, fixed: 'left' },
// ... 中间列不固定 ...
{ title: '操作', dataIndex: 'action', width: 100, fixed: 'right' }
];
关键点
fixed: 'left': 左侧锁定fixed: 'right': 右侧锁定minWidth: 防止列宽过小
批注表格 (postilTable)
源码
tableDemo/postilTable/index.vue
核心模式
// 列定义
{ title: '批注', dataIndex: 'postilCount', slots: { default: 'postilCount' } }
// 模板
<template #postilCount="{ record }">
<span class="cursor-pointer" @click="addOrUpdateHandle(record.id)">
批注({{ record.postilCount || 0 }})
</span>
</template>
// 打开批注弹窗
function addOrUpdateHandle(id) {
openPostilModal(true, { id });
}
关键点
- 批注数量字段
postilCount - 点击打开批注详情弹窗
行内编辑 (redactTable)
源码
tableDemo/redactTable/index.vue
核心模式
// 点击切换编辑模式
<template v-for="column in columns" #[column.dataIndex]="{ record }">
<template v-if="state.tabClickId == record.id">
<jnpf-date-picker v-if="column.dataIndex === 'interactionDate'" />
<jnpf-select v-else-if="column.dataIndex === 'projectType'" />
<a-input v-else v-model:value="record[column.dataIndex]" />
</template>
</template>
// 点击行开启编辑
function handleRowClick({ record }) {
state.tabClickId = record.id;
}
// 保存编辑
async function handleSave(row) {
await updateRowData(row);
state.tabClickId = null;
}
关键点
tabClickId: 记录当前编辑行- 动态组件切换编辑控件
- 支持多种控件类型
标记表格 (signTable)
源码
tableDemo/signTable/index.vue
核心模式
// 颜色标记下拉
<a-dropdown>
<a class="ant-dropdown-link">
项目标记<DownOutlined />
</a>
<template #overlay>
<a-menu>
<a-menu-item v-for="(item, i) in options" @click="handleSign(record, item.value, i)">
<span :style="{ color: item.color }">{{ item.label }}</span>
</a-menu-item>
</a-menu>
</template>
</a-dropdown>
关键点
- 颜色选项列表
options handleSign: 更新行的标记颜色
打印表格 (printTable)
源码
tableDemo/printTable/index.vue
核心模式
import printJS from 'print-js';
function handlePrint() {
printJS({
printable: state.list, // 数据
properties: columnsList.map((o) => ({
field: o.dataIndex,
displayName: o.title
})),
type: 'json',
gridStyle: 'border: 1px solid lightgray;'
});
}
关键点
- 使用
printJS库 properties: 字段映射
统计表格 (statisticsTable)
源码
tableDemo/statisticsTable/index.vue
核心模式
const [registerTable, { reload }] = useVxeTable({
showFooter: true,
footerMethod: ({ columns, data }) => {
return [columns.map((column, columnIndex) => {
if (![9, 10, 11].includes(columnIndex)) return ''; // 非统计列
let sumVal = data.reduce((sum, d) => {
return sum + Number.parseFloat(d[column.property] || 0);
}, 0);
return Number.isNaN(sumVal) ? '' : sumVal.toFixed(2);
})];
}
});
关键点
showFooter: true: 显示汇总行footerMethod: 返回汇总数据
扩展表格 (extension)
源码
tableDemo/extension/index.vue
核心模式
// 展开行显示嵌套表格
expandConfig: {
lazy: true,
loadMethod({ row }) {
return new Promise((resolve) => {
getCityList(row.id).then((res) => {
row.list = res.data.list;
resolve(row);
});
});
}
}
// 嵌套 VxeGrid
<VxeGrid :data="record.list" :columns="childColumns" :show-header="false" />
关键点
expandConfig.lazy: 懒加载展开数据loadMethod: 返回 Promise- 嵌套
VxeGrid显示子数据
复杂表头 (complexHeader)
源码
tableDemo/complexHeader/index.vue
核心模式
const columns: BasicColumn[] = [
{
title: '相关费用',
width: 240,
align: 'center',
children: [
{ title: '费用金额', dataIndex: 'costAmount', width: 100 },
{ title: '已用金额', dataIndex: 'tunesAmount', width: 100 },
{ title: '预计收入', dataIndex: 'projectedIncome', width: 100 }
]
}
];
关键点
children: 定义子列- 多层表头嵌套
表格树 (tableTree)
源码
tableDemo/tableTree/index.vue
核心模式
const [registerTable, { expandAll }] = useVxeTable({
isTreeTable: true,
immediate: false
});
// 字典数据作为树
state.industryTypeList = await baseStore.getDictionaryData('IndustryType');
关键点
isTreeTable: true: 纯树形表格- 树形数据来自数据字典
通用 API 模式
列表查询
import { getTableList, createRecord, updateRecord, deleteRecord } from '@/api/xxx';
const [registerTable, { reload, deleteTableDataRecord }] = useVxeTable({
api: getTableList,
columns,
actionColumn: { width: 120, title: '操作', dataIndex: 'action' }
});
// 新增
function handleAdd() {
openModal(true, {});
}
// 编辑
function handleEdit(record) {
openModal(true, record);
}
// 删除
async function handleDelete(record) {
await deleteRecord(record.id);
deleteTableDataRecord(record.id);
reload();
}