feat: use table actions

This commit is contained in:
xingyu4j
2025-05-15 14:47:19 +08:00
parent ff5358da4b
commit 007faab168
6 changed files with 104 additions and 168 deletions

View File

@@ -1,8 +1,5 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemTenantApi } from '#/api/system/tenant';
import { useAccess } from '@vben/access';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { z } from '#/adapter/form';
import { getTenantPackageList } from '#/api/system/tenant-package';
@@ -13,8 +10,6 @@ import {
getRangePickerDefaultProps,
} from '#/utils';
const { hasAccessByCodes } = useAccess();
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
@@ -162,8 +157,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
}
/** 列表的字段 */
export function useGridColumns<T = SystemTenantApi.Tenant>(
onActionClick: OnActionClickFn<T>,
export function useGridColumns(
getPackageName?: (packageId: number) => string | undefined,
): VxeTableGridOptions['columns'] {
return [
@@ -227,29 +221,10 @@ export function useGridColumns<T = SystemTenantApi.Tenant>(
formatter: 'formatDateTime',
},
{
field: 'operation',
title: '操作',
minWidth: 130,
align: 'center',
width: 130,
fixed: 'right',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '租户',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:tenant:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:tenant:delete']),
},
],
},
slots: { default: 'actions' },
},
];
}

View File

@@ -1,8 +1,5 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemTenantApi } from '#/api/system/tenant';
import type { SystemTenantPackageApi } from '#/api/system/tenant-package';
@@ -75,29 +72,12 @@ async function onDelete(row: SystemTenantApi.Tenant) {
}
}
/** 表格操作按钮的回调函数 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemTenantApi.Tenant>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(onActionClick, getPackageName),
columns: useGridColumns(getPackageName),
height: 'auto',
keepSource: true,
proxyConfig: {
@@ -153,6 +133,30 @@ onMounted(async () => {
{{ $t('ui.actionTitle.export') }}
</Button>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: 'ant-design:edit-outlined',
auth: ['system:role:update'],
onClick: onEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: 'ant-design:delete-outlined',
auth: ['system:role:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: onDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>