feat: use table-actions

pull/105/head^2
xingyu4j 2025-05-15 11:50:13 +08:00
parent 18273c42a6
commit e8c87b5a2b
3 changed files with 68 additions and 77 deletions

View File

@ -1,8 +1,5 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemRoleApi } from '#/api/system/role';
import { useAccess } from '@vben/access';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { z } from '#/adapter/form';
import {
@ -13,8 +10,6 @@ import {
SystemDataScopeEnum,
} from '#/utils';
const { hasAccessByCodes } = useAccess();
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
@ -189,9 +184,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
}
/** 列表的字段 */
export function useGridColumns<T = SystemRoleApi.Role>(
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
export function useGridColumns(): VxeTableGridOptions['columns'] {
return [
{
field: 'id',
@ -243,41 +236,10 @@ export function useGridColumns<T = SystemRoleApi.Role>(
formatter: 'formatDateTime',
},
{
field: 'operation',
title: '操作',
width: 240,
fixed: 'right',
align: 'center',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '角色',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:role:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:role:delete']),
},
{
code: 'assign-data-permission',
text: '数据权限',
show: hasAccessByCodes([
'system:permission:assign-role-data-scope',
]),
},
{
code: 'assign-menu',
text: '菜单权限',
show: hasAccessByCodes(['system:permission:assign-role-menu']),
},
],
},
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 { SystemRoleApi } from '#/api/system/role';
import { Page, useVbenModal } from '@vben/common-ui';
@ -14,6 +11,7 @@ import { Button, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteRole, exportRole, getRolePage } from '#/api/system/role';
import { DocAlert } from '#/components/doc-alert';
import { TableAction } from '#/components/table-action';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
@ -84,34 +82,12 @@ function onAssignMenu(row: SystemRoleApi.Role) {
assignMenuFormApi.setData(row).open();
}
/** 表格操作按钮的回调函数 */
function onActionClick({ code, row }: OnActionClickParams<SystemRoleApi.Role>) {
switch (code) {
case 'assign-data-permission': {
onAssignDataPermission(row);
break;
}
case 'assign-menu': {
onAssignMenu(row);
break;
}
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(onActionClick),
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
@ -169,6 +145,41 @@ const [Grid, gridApi] = useVbenVxeGrid({
{{ $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'],
onClick: onDelete.bind(null, row),
},
]"
:drop-down-actions="[
{
label: '数据权限',
type: 'link',
auth: ['system:role:update'],
onClick: onAssignDataPermission.bind(null, row),
},
{
label: '菜单权限',
type: 'link',
auth: ['system:role:update'],
onClick: onAssignMenu.bind(null, row),
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@ -229,15 +229,33 @@ setupVbenVxeTable({
},
},
{
trigger: () => renderBtn({ ...opt }, false),
default: () =>
h(
'div',
{ class: 'truncate' },
$t('ui.actionMessage.deleteConfirm', [
row[attrs?.nameField || 'name'],
]),
),
trigger: () => {
return h(
NButton,
{
...props,
...opt,
icon: undefined,
},
{
default: () => {
const content = [];
if (opt.icon) {
content.push(
h(IconifyIcon, { class: 'size-5', icon: opt.icon }),
);
}
content.push(opt.concent);
return content;
},
},
);
},
default: () => {
return $t('ui.actionMessage.deleteConfirm', [
row[attrs?.nameField || 'name'],
]);
},
},
);
}