feat: use table action

pull/108/head^2
xingyu4j 2025-05-19 11:21:04 +08:00
parent 2ea9b4781e
commit 0af58f8f9a
3 changed files with 80 additions and 123 deletions

View File

@ -1,16 +1,10 @@
import type { VbenFormSchema } from '#/adapter/form'; import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table'; import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemDictDataApi } from '#/api/system/dict/data';
import type { SystemDictTypeApi } from '#/api/system/dict/type';
import { useAccess } from '@vben/access';
import { z } from '#/adapter/form'; import { z } from '#/adapter/form';
import { getSimpleDictTypeList } from '#/api/system/dict/type'; import { getSimpleDictTypeList } from '#/api/system/dict/type';
import { CommonStatusEnum, DICT_TYPE, getDictOptions } from '#/utils'; import { CommonStatusEnum, DICT_TYPE, getDictOptions } from '#/utils';
const { hasAccessByCodes } = useAccess();
// ============================== 字典类型 ============================== // ============================== 字典类型 ==============================
/** 类型新增/修改的表单 */ /** 类型新增/修改的表单 */
@ -96,9 +90,7 @@ export function useTypeGridFormSchema(): VbenFormSchema[] {
} }
/** 类型列表的字段 */ /** 类型列表的字段 */
export function useTypeGridColumns<T = SystemDictTypeApi.DictType>( export function useTypeGridColumns(): VxeTableGridOptions['columns'] {
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [ return [
{ {
field: 'id', field: 'id',
@ -136,29 +128,10 @@ export function useTypeGridColumns<T = SystemDictTypeApi.DictType>(
formatter: 'formatDateTime', formatter: 'formatDateTime',
}, },
{ {
minWidth: 120,
title: '操作', title: '操作',
field: 'operation', width: 160,
fixed: 'right', fixed: 'right',
align: 'center', slots: { default: 'actions' },
cellRender: {
attrs: {
nameField: 'type',
nameTitle: '字典类型',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:dict:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:dict:delete']),
},
],
},
}, },
]; ];
} }
@ -310,9 +283,7 @@ export function useDataGridFormSchema(): VbenFormSchema[] {
/** /**
* *
*/ */
export function useDataGridColumns<T = SystemDictDataApi.DictData>( export function useDataGridColumns(): VxeTableGridOptions['columns'] {
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [ return [
{ {
field: 'id', field: 'id',
@ -360,29 +331,10 @@ export function useDataGridColumns<T = SystemDictDataApi.DictData>(
formatter: 'formatDateTime', formatter: 'formatDateTime',
}, },
{ {
minWidth: 120,
title: '操作', title: '操作',
field: 'operation', width: 160,
fixed: 'right', fixed: 'right',
align: 'center', slots: { default: 'actions' },
cellRender: {
attrs: {
nameField: 'label',
nameTitle: '字典数据',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:dict:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:dict:delete']),
},
],
},
}, },
]; ];
} }

View File

@ -1,8 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { import type { VxeTableGridOptions } from '#/adapter/vxe-table';
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemDictDataApi } from '#/api/system/dict/data'; import type { SystemDictDataApi } from '#/api/system/dict/data';
import { watch } from 'vue'; import { watch } from 'vue';
@ -19,6 +16,7 @@ import {
exportDictData, exportDictData,
getDictDataPage, getDictDataPage,
} from '#/api/system/dict/data'; } from '#/api/system/dict/data';
import { ACTION_KEY, TableAction } from '#/components/table-action';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { useDataGridColumns, useDataGridFormSchema } from '../data'; import { useDataGridColumns, useDataGridFormSchema } from '../data';
@ -53,41 +51,22 @@ function onCreate() {
} }
/** 编辑字典数据 */ /** 编辑字典数据 */
function onEdit(row: any) { function onEdit(row: SystemDictDataApi.DictData) {
dataFormModalApi.setData(row).open(); dataFormModalApi.setData(row).open();
} }
/** 删除字典数据 */ /** 删除字典数据 */
async function onDelete(row: any) { async function onDelete(row: SystemDictDataApi.DictData) {
const hideLoading = message.loading({ message.loading({
content: $t('common.processing'), content: $t('ui.actionMessage.deleting', [row.label]),
duration: 0, key: ACTION_KEY,
key: 'process_message',
}); });
try { await deleteDictData(row.id as number);
await deleteDictData(row.id); message.success({
message.success({ content: $t('ui.actionMessage.deleteSuccess', [row.label]),
content: $t('common.operationSuccess'), key: ACTION_KEY,
key: 'process_message', });
}); onRefresh();
onRefresh();
} finally {
hideLoading();
}
}
/** 表格操作按钮回调 */
function onActionClick({ code, row }: OnActionClickParams) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
} }
const [Grid, gridApi] = useVbenVxeGrid({ const [Grid, gridApi] = useVbenVxeGrid({
@ -95,7 +74,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
schema: useDataGridFormSchema(), schema: useDataGridFormSchema(),
}, },
gridOptions: { gridOptions: {
columns: useDataGridColumns(onActionClick), columns: useDataGridColumns(),
height: 'auto', height: 'auto',
keepSource: true, keepSource: true,
proxyConfig: { proxyConfig: {
@ -155,6 +134,30 @@ watch(
{{ $t('ui.actionTitle.export') }} {{ $t('ui.actionTitle.export') }}
</Button> </Button>
</template> </template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: 'ant-design:edit-outlined',
auth: ['system:dict:update'],
onClick: onEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: 'ant-design:delete-outlined',
auth: ['system:dict:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.label]),
confirm: onDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid> </Grid>
</div> </div>
</template> </template>

View File

@ -1,6 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { import type {
OnActionClickParams,
VxeGridListeners, VxeGridListeners,
VxeTableGridOptions, VxeTableGridOptions,
} from '#/adapter/vxe-table'; } from '#/adapter/vxe-table';
@ -18,6 +17,7 @@ import {
exportDictType, exportDictType,
getDictTypePage, getDictTypePage,
} from '#/api/system/dict/type'; } from '#/api/system/dict/type';
import { ACTION_KEY, TableAction } from '#/components/table-action';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { useTypeGridColumns, useTypeGridFormSchema } from '../data'; import { useTypeGridColumns, useTypeGridFormSchema } from '../data';
@ -53,38 +53,16 @@ function onEdit(row: any) {
/** 删除字典类型 */ /** 删除字典类型 */
async function onDelete(row: SystemDictTypeApi.DictType) { async function onDelete(row: SystemDictTypeApi.DictType) {
const hideLoading = message.loading({ message.loading({
content: $t('common.processing'), content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0, key: ACTION_KEY,
key: 'process_message',
}); });
try { await deleteDictType(row.id as number);
await deleteDictType(row.id as number); message.success({
message.success({ content: $t('ui.actionMessage.deleteSuccess', [row.name]),
content: $t('common.operationSuccess'), key: ACTION_KEY,
key: 'process_message', });
}); onRefresh();
onRefresh();
} finally {
hideLoading();
}
}
/** 表格操作按钮回调 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemDictTypeApi.DictType>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
} }
/** 表格事件 */ /** 表格事件 */
@ -99,7 +77,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
schema: useTypeGridFormSchema(), schema: useTypeGridFormSchema(),
}, },
gridOptions: { gridOptions: {
columns: useTypeGridColumns(onActionClick), columns: useTypeGridColumns(),
height: 'auto', height: 'auto',
keepSource: true, keepSource: true,
proxyConfig: { proxyConfig: {
@ -150,6 +128,30 @@ const [Grid, gridApi] = useVbenVxeGrid({
{{ $t('ui.actionTitle.export') }} {{ $t('ui.actionTitle.export') }}
</Button> </Button>
</template> </template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: 'ant-design:edit-outlined',
auth: ['system:dict:update'],
onClick: onEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: 'ant-design:delete-outlined',
auth: ['system:dict:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: onDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid> </Grid>
</div> </div>
</template> </template>