2025-06-08 17:53:55 +08:00
|
|
|
|
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
|
2025-05-09 13:52:20 +08:00
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
import { h } from 'vue';
|
|
|
|
|
|
|
|
|
|
|
|
import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table';
|
2025-10-16 11:27:50 +08:00
|
|
|
|
import {
|
|
|
|
|
|
erpCountInputFormatter,
|
|
|
|
|
|
erpNumberFormatter,
|
|
|
|
|
|
fenToYuan,
|
|
|
|
|
|
formatPast2,
|
|
|
|
|
|
} from '@vben/utils';
|
2025-05-09 13:52:20 +08:00
|
|
|
|
|
2025-10-16 11:27:50 +08:00
|
|
|
|
import { NButton, NImage, NImageGroup, NSwitch, NTag } from 'naive-ui';
|
2024-10-04 23:05:28 +08:00
|
|
|
|
|
2025-10-16 11:27:50 +08:00
|
|
|
|
import { $t } from '#/locales';
|
|
|
|
|
|
|
|
|
|
|
|
import DictTag from '../components/dict-tag/dict-tag.vue';
|
2024-10-04 23:05:28 +08:00
|
|
|
|
import { useVbenForm } from './form';
|
|
|
|
|
|
|
|
|
|
|
|
setupVbenVxeTable({
|
|
|
|
|
|
configVxeTable: (vxeUI) => {
|
|
|
|
|
|
vxeUI.setConfig({
|
|
|
|
|
|
grid: {
|
|
|
|
|
|
align: 'center',
|
2024-10-24 22:51:04 +08:00
|
|
|
|
border: false,
|
|
|
|
|
|
columnConfig: {
|
|
|
|
|
|
resizable: true,
|
|
|
|
|
|
},
|
2024-10-04 23:05:28 +08:00
|
|
|
|
minHeight: 180,
|
2024-11-02 15:46:19 +08:00
|
|
|
|
formConfig: {
|
|
|
|
|
|
// 全局禁用vxe-table的表单配置,使用formOptions
|
|
|
|
|
|
enabled: false,
|
|
|
|
|
|
},
|
2025-10-16 11:27:50 +08:00
|
|
|
|
toolbarConfig: {
|
|
|
|
|
|
import: false, // 是否导入
|
|
|
|
|
|
export: false, // 是否导出
|
|
|
|
|
|
refresh: true, // 是否刷新
|
|
|
|
|
|
print: false, // 是否打印
|
|
|
|
|
|
zoom: true, // 是否缩放
|
|
|
|
|
|
custom: true, // 是否自定义配置
|
|
|
|
|
|
},
|
|
|
|
|
|
customConfig: {
|
|
|
|
|
|
mode: 'modal',
|
|
|
|
|
|
},
|
2024-10-04 23:05:28 +08:00
|
|
|
|
proxyConfig: {
|
|
|
|
|
|
autoLoad: true,
|
|
|
|
|
|
response: {
|
2025-10-16 11:27:50 +08:00
|
|
|
|
result: 'list',
|
2024-10-04 23:05:28 +08:00
|
|
|
|
total: 'total',
|
|
|
|
|
|
},
|
|
|
|
|
|
showActiveMsg: true,
|
|
|
|
|
|
showResponseMsg: false,
|
|
|
|
|
|
},
|
2025-10-16 11:27:50 +08:00
|
|
|
|
pagerConfig: {
|
|
|
|
|
|
enabled: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
sortConfig: {
|
|
|
|
|
|
multiple: true,
|
|
|
|
|
|
},
|
2024-10-04 23:05:28 +08:00
|
|
|
|
round: true,
|
2024-10-24 22:51:04 +08:00
|
|
|
|
showOverflow: true,
|
2024-10-04 23:05:28 +08:00
|
|
|
|
size: 'small',
|
2025-06-08 17:53:55 +08:00
|
|
|
|
} as VxeTableGridOptions,
|
2024-10-04 23:05:28 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 表格配置项可以用 cellRender: { name: 'CellImage' },
|
|
|
|
|
|
vxeUI.renderer.add('CellImage', {
|
2024-11-05 11:25:57 +08:00
|
|
|
|
renderTableDefault(_renderOpts, params) {
|
2024-10-04 23:05:28 +08:00
|
|
|
|
const { column, row } = params;
|
|
|
|
|
|
return h(NImage, { src: row[column.field] });
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 11:27:50 +08:00
|
|
|
|
vxeUI.renderer.add('CellImages', {
|
|
|
|
|
|
renderTableDefault(_renderOpts, params) {
|
|
|
|
|
|
const { column, row } = params;
|
|
|
|
|
|
if (column && column.field && row[column.field]) {
|
|
|
|
|
|
return h(NImageGroup, { srcList: row[column.field] });
|
|
|
|
|
|
}
|
|
|
|
|
|
return '';
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
// 表格配置项可以用 cellRender: { name: 'CellLink' },
|
|
|
|
|
|
vxeUI.renderer.add('CellLink', {
|
2024-11-05 11:25:57 +08:00
|
|
|
|
renderTableDefault(renderOpts) {
|
2024-10-04 23:05:28 +08:00
|
|
|
|
const { props } = renderOpts;
|
|
|
|
|
|
return h(
|
|
|
|
|
|
NButton,
|
|
|
|
|
|
{ size: 'small', type: 'primary', quaternary: true },
|
|
|
|
|
|
{ default: () => props?.text },
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-10-16 11:27:50 +08:00
|
|
|
|
// 表格配置项可以用 cellRender: { name: 'CellTag' },
|
|
|
|
|
|
vxeUI.renderer.add('CellTag', {
|
|
|
|
|
|
renderTableDefault(renderOpts, params) {
|
|
|
|
|
|
const { props } = renderOpts;
|
|
|
|
|
|
const { column, row } = params;
|
|
|
|
|
|
return h(NTag, { color: props?.color }, () => row[column.field]);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
vxeUI.renderer.add('CellTags', {
|
|
|
|
|
|
renderTableDefault(renderOpts, params) {
|
|
|
|
|
|
const { props } = renderOpts;
|
|
|
|
|
|
const { column, row } = params;
|
|
|
|
|
|
if (!row[column.field] || !Array.isArray(row[column.field])) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
return h(
|
|
|
|
|
|
'div',
|
|
|
|
|
|
{ class: 'flex items-center justify-center' },
|
|
|
|
|
|
{
|
|
|
|
|
|
default: () =>
|
|
|
|
|
|
row[column.field].map((item: any) =>
|
|
|
|
|
|
h(NTag, { color: props?.color }, { default: () => item }),
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 表格配置项可以用 cellRender: { name: 'CellDict', props:{dictType: ''} },
|
|
|
|
|
|
vxeUI.renderer.add('CellDict', {
|
|
|
|
|
|
renderTableDefault(renderOpts, params) {
|
|
|
|
|
|
const { props } = renderOpts;
|
|
|
|
|
|
const { column, row } = params;
|
|
|
|
|
|
if (!props) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
// 使用 DictTag 组件替代原来的实现
|
|
|
|
|
|
return h(DictTag, {
|
|
|
|
|
|
type: props.type,
|
|
|
|
|
|
value: row[column.field]?.toString(),
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 表格配置项可以用 cellRender: { name: 'CellSwitch', props: { beforeChange: () => {} } },
|
|
|
|
|
|
// add by 芋艿:from https://github.com/vbenjs/vue-vben-admin/blob/main/playground/src/adapter/vxe-table.ts#L97-L123
|
|
|
|
|
|
vxeUI.renderer.add('CellSwitch', {
|
|
|
|
|
|
renderTableDefault({ attrs, props }, { column, row }) {
|
|
|
|
|
|
const loadingKey = `__loading_${column.field}`;
|
|
|
|
|
|
const finallyProps = {
|
|
|
|
|
|
inlinePrompt: true,
|
2025-10-16 16:35:02 +08:00
|
|
|
|
checkedValue: 0,
|
|
|
|
|
|
uncheckedValue: 1,
|
2025-10-16 11:27:50 +08:00
|
|
|
|
...props,
|
2025-10-16 16:35:02 +08:00
|
|
|
|
value: row[column.field],
|
2025-10-16 11:27:50 +08:00
|
|
|
|
loading: row[loadingKey] ?? false,
|
2025-10-16 16:35:02 +08:00
|
|
|
|
'onUpdate:value': onChange,
|
2025-10-16 11:27:50 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async function onChange(newVal: any) {
|
|
|
|
|
|
row[loadingKey] = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = await attrs?.beforeChange?.(newVal, row);
|
|
|
|
|
|
if (result !== false) {
|
|
|
|
|
|
row[column.field] = newVal;
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
row[loadingKey] = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-16 16:35:02 +08:00
|
|
|
|
return h(NSwitch, finallyProps, {
|
2025-10-17 11:33:49 +08:00
|
|
|
|
checked: () => h('p', $t('common.enabled')),
|
|
|
|
|
|
unchecked: () => h('p', $t('common.disabled')),
|
2025-10-16 16:35:02 +08:00
|
|
|
|
});
|
2025-10-16 11:27:50 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
// 这里可以自行扩展 vxe-table 的全局配置,比如自定义格式化
|
|
|
|
|
|
// vxeUI.formats.add
|
2025-10-16 11:27:50 +08:00
|
|
|
|
vxeUI.formats.add('formatPast2', {
|
|
|
|
|
|
tableCellFormatMethod({ cellValue }) {
|
|
|
|
|
|
return formatPast2(cellValue);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// add by 星语:数量格式化,保留 3 位
|
|
|
|
|
|
vxeUI.formats.add('formatAmount3', {
|
|
|
|
|
|
tableCellFormatMethod({ cellValue }) {
|
|
|
|
|
|
if (cellValue === null || cellValue === undefined) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
return erpCountInputFormatter(cellValue);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
// add by 星语:数量格式化,保留 2 位
|
|
|
|
|
|
vxeUI.formats.add('formatAmount2', {
|
|
|
|
|
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
|
|
|
|
|
return `${erpNumberFormatter(cellValue, digits)}`;
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
vxeUI.formats.add('formatFenToYuanAmount', {
|
|
|
|
|
|
tableCellFormatMethod({ cellValue }, digits = 2) {
|
|
|
|
|
|
return `${erpNumberFormatter(fenToYuan(cellValue), digits)}`;
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2024-10-04 23:05:28 +08:00
|
|
|
|
},
|
|
|
|
|
|
useVbenForm,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export { useVbenVxeGrid };
|
2025-10-16 10:16:45 +08:00
|
|
|
|
|
2025-10-16 16:35:02 +08:00
|
|
|
|
export * from '#/components/table-action';
|
2024-10-04 23:05:28 +08:00
|
|
|
|
export type * from '@vben/plugins/vxe-table';
|