2025-02-25 19:47:45 +08:00
|
|
|
|
import type { Recordable } from '@vben/types';
|
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
import { h } from 'vue';
|
|
|
|
|
|
2025-02-25 19:47:45 +08:00
|
|
|
|
import { IconifyIcon } from '@vben/icons';
|
|
|
|
|
import { $te } from '@vben/locales';
|
2024-10-04 23:05:28 +08:00
|
|
|
|
import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table';
|
2025-02-27 01:22:25 +08:00
|
|
|
|
import { get, isFunction, isString } from '@vben/utils';
|
2025-02-25 19:47:45 +08:00
|
|
|
|
|
|
|
|
|
import { objectOmit } from '@vueuse/core';
|
2025-03-07 16:03:08 +08:00
|
|
|
|
import { Button, Image, Popconfirm, Switch, Tag } from 'ant-design-vue';
|
2024-10-04 23:05:28 +08:00
|
|
|
|
|
2025-02-25 19:47:45 +08:00
|
|
|
|
import { $t } from '#/locales';
|
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-11-02 15:46:19 +08:00
|
|
|
|
|
|
|
|
|
formConfig: {
|
|
|
|
|
// 全局禁用vxe-table的表单配置,使用formOptions
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
2024-10-04 23:05:28 +08:00
|
|
|
|
minHeight: 180,
|
|
|
|
|
proxyConfig: {
|
|
|
|
|
autoLoad: true,
|
|
|
|
|
response: {
|
|
|
|
|
result: 'items',
|
|
|
|
|
total: 'total',
|
2025-02-25 19:47:45 +08:00
|
|
|
|
list: '',
|
2024-10-04 23:05:28 +08:00
|
|
|
|
},
|
|
|
|
|
showActiveMsg: true,
|
|
|
|
|
showResponseMsg: false,
|
|
|
|
|
},
|
|
|
|
|
round: true,
|
2024-10-24 22:51:04 +08:00
|
|
|
|
showOverflow: true,
|
2024-10-04 23:05:28 +08:00
|
|
|
|
size: 'small',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-25 19:47:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 解决vxeTable在热更新时可能会出错的问题
|
|
|
|
|
*/
|
|
|
|
|
vxeUI.renderer.forEach((_item, key) => {
|
|
|
|
|
if (key.startsWith('Cell')) {
|
|
|
|
|
vxeUI.renderer.delete(key);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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(Image, { src: row[column.field] });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 表格配置项可以用 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(
|
|
|
|
|
Button,
|
|
|
|
|
{ size: 'small', type: 'link' },
|
|
|
|
|
{ default: () => props?.text },
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-25 19:47:45 +08:00
|
|
|
|
// 单元格渲染: Tag
|
|
|
|
|
vxeUI.renderer.add('CellTag', {
|
|
|
|
|
renderTableDefault({ options, props }, { column, row }) {
|
2025-02-27 01:22:25 +08:00
|
|
|
|
const value = get(row, column.field);
|
|
|
|
|
const tagOptions = options ?? [
|
2025-02-25 19:47:45 +08:00
|
|
|
|
{ color: 'success', label: $t('common.enabled'), value: 1 },
|
|
|
|
|
{ color: 'error', label: $t('common.disabled'), value: 0 },
|
|
|
|
|
];
|
|
|
|
|
const tagItem = tagOptions.find((item) => item.value === value);
|
|
|
|
|
return h(
|
|
|
|
|
Tag,
|
|
|
|
|
{
|
|
|
|
|
...props,
|
2025-02-27 01:22:25 +08:00
|
|
|
|
...objectOmit(tagItem ?? {}, ['label']),
|
2025-02-25 19:47:45 +08:00
|
|
|
|
},
|
|
|
|
|
{ default: () => tagItem?.label ?? value },
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-07 16:03:08 +08:00
|
|
|
|
vxeUI.renderer.add('CellSwitch', {
|
|
|
|
|
renderTableDefault({ attrs, props }, { column, row }) {
|
|
|
|
|
const loadingKey = `__loading_${column.field}`;
|
|
|
|
|
const finallyProps = {
|
|
|
|
|
checkedChildren: $t('common.enabled'),
|
|
|
|
|
checkedValue: 1,
|
|
|
|
|
unCheckedChildren: $t('common.disabled'),
|
|
|
|
|
unCheckedValue: 0,
|
|
|
|
|
...props,
|
|
|
|
|
checked: row[column.field],
|
|
|
|
|
loading: row[loadingKey] ?? false,
|
|
|
|
|
'onUpdate:checked': onChange,
|
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return h(Switch, finallyProps);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-25 19:47:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 注册表格的操作按钮渲染器
|
|
|
|
|
*/
|
|
|
|
|
vxeUI.renderer.add('CellOperation', {
|
|
|
|
|
renderTableDefault({ attrs, options, props }, { column, row }) {
|
|
|
|
|
const defaultProps = { size: 'small', type: 'link', ...props };
|
|
|
|
|
let align = 'end';
|
|
|
|
|
switch (column.align) {
|
|
|
|
|
case 'center': {
|
|
|
|
|
align = 'center';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'left': {
|
|
|
|
|
align = 'start';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
align = 'end';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const presets: Recordable<Recordable<any>> = {
|
|
|
|
|
delete: {
|
|
|
|
|
danger: true,
|
|
|
|
|
text: $t('common.delete'),
|
|
|
|
|
},
|
|
|
|
|
edit: {
|
|
|
|
|
text: $t('common.edit'),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const operations: Array<Recordable<any>> = (
|
|
|
|
|
options || ['edit', 'delete']
|
|
|
|
|
)
|
|
|
|
|
.map((opt) => {
|
|
|
|
|
if (isString(opt)) {
|
|
|
|
|
return presets[opt]
|
|
|
|
|
? { code: opt, ...presets[opt], ...defaultProps }
|
|
|
|
|
: {
|
|
|
|
|
code: opt,
|
|
|
|
|
text: $te(`common.${opt}`) ? $t(`common.${opt}`) : opt,
|
|
|
|
|
...defaultProps,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return { ...defaultProps, ...presets[opt.code], ...opt };
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.map((opt) => {
|
|
|
|
|
const optBtn: Recordable<any> = {};
|
|
|
|
|
Object.keys(opt).forEach((key) => {
|
|
|
|
|
optBtn[key] = isFunction(opt[key]) ? opt[key](row) : opt[key];
|
|
|
|
|
});
|
|
|
|
|
return optBtn;
|
|
|
|
|
})
|
|
|
|
|
.filter((opt) => opt.show !== false);
|
|
|
|
|
|
|
|
|
|
function renderBtn(opt: Recordable<any>, listen = true) {
|
|
|
|
|
return h(
|
|
|
|
|
Button,
|
|
|
|
|
{
|
|
|
|
|
...props,
|
|
|
|
|
...opt,
|
|
|
|
|
icon: undefined,
|
|
|
|
|
onClick: listen
|
|
|
|
|
? () =>
|
|
|
|
|
attrs?.onClick?.({
|
|
|
|
|
code: opt.code,
|
|
|
|
|
row,
|
|
|
|
|
})
|
|
|
|
|
: undefined,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
default: () => {
|
|
|
|
|
const content = [];
|
|
|
|
|
if (opt.icon) {
|
|
|
|
|
content.push(
|
|
|
|
|
h(IconifyIcon, { class: 'size-5', icon: opt.icon }),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
content.push(opt.text);
|
|
|
|
|
return content;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderConfirm(opt: Recordable<any>) {
|
|
|
|
|
return h(
|
|
|
|
|
Popconfirm,
|
|
|
|
|
{
|
2025-03-07 16:03:08 +08:00
|
|
|
|
getPopupContainer(el) {
|
|
|
|
|
return el.closest('tbody') || document.body;
|
|
|
|
|
},
|
2025-02-25 19:47:45 +08:00
|
|
|
|
placement: 'topLeft',
|
|
|
|
|
title: $t('ui.actionTitle.delete', [attrs?.nameTitle || '']),
|
|
|
|
|
...props,
|
|
|
|
|
...opt,
|
|
|
|
|
icon: undefined,
|
|
|
|
|
onConfirm: () => {
|
|
|
|
|
attrs?.onClick?.({
|
|
|
|
|
code: opt.code,
|
|
|
|
|
row,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
default: () => renderBtn({ ...opt }, false),
|
|
|
|
|
description: () =>
|
|
|
|
|
h(
|
|
|
|
|
'div',
|
|
|
|
|
{ class: 'truncate' },
|
|
|
|
|
$t('ui.actionMessage.deleteConfirm', [
|
|
|
|
|
row[attrs?.nameField || 'name'],
|
|
|
|
|
]),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const btns = operations.map((opt) =>
|
|
|
|
|
opt.code === 'delete' ? renderConfirm(opt) : renderBtn(opt),
|
|
|
|
|
);
|
|
|
|
|
return h(
|
|
|
|
|
'div',
|
|
|
|
|
{
|
|
|
|
|
class: 'flex table-operations',
|
|
|
|
|
style: { justifyContent: align },
|
|
|
|
|
},
|
|
|
|
|
btns,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-04 23:05:28 +08:00
|
|
|
|
// 这里可以自行扩展 vxe-table 的全局配置,比如自定义格式化
|
|
|
|
|
// vxeUI.formats.add
|
|
|
|
|
},
|
|
|
|
|
useVbenForm,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export { useVbenVxeGrid };
|
2025-02-25 19:47:45 +08:00
|
|
|
|
export type OnActionClickParams<T = Recordable<any>> = {
|
|
|
|
|
code: string;
|
|
|
|
|
row: T;
|
|
|
|
|
};
|
|
|
|
|
export type OnActionClickFn<T = Recordable<any>> = (
|
|
|
|
|
params: OnActionClickParams<T>,
|
|
|
|
|
) => void;
|
2024-10-04 23:05:28 +08:00
|
|
|
|
export type * from '@vben/plugins/vxe-table';
|