mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 10:37:41 +08:00 
			
		
		
		
	perf: vben5 代码生成 使用 table action
This commit is contained in:
		| @ -1,5 +1,5 @@ | |||||||
| 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 { ${simpleClassName}Api } from '#/api/${table.moduleName}/${table.businessName}'; | import type { ${simpleClassName}Api } from '#/api/${table.moduleName}/${table.businessName}'; | ||||||
|  |  | ||||||
| import { z } from '#/adapter/form'; | import { z } from '#/adapter/form'; | ||||||
| @ -12,9 +12,6 @@ import { | |||||||
| import { get${simpleClassName}List } from '#/api/${table.moduleName}/${table.businessName}'; | import { get${simpleClassName}List } from '#/api/${table.moduleName}/${table.businessName}'; | ||||||
| import { handleTree } from '@vben/utils'; | import { handleTree } from '@vben/utils'; | ||||||
| #end | #end | ||||||
| import { useAccess } from '@vben/access'; |  | ||||||
|  |  | ||||||
| const { hasAccessByCodes } = useAccess(); |  | ||||||
|  |  | ||||||
| /** 新增/修改的表单 */ | /** 新增/修改的表单 */ | ||||||
| export function useFormSchema(): VbenFormSchema[] { | export function useFormSchema(): VbenFormSchema[] { | ||||||
| @ -189,9 +186,7 @@ export function useGridFormSchema(): VbenFormSchema[] { | |||||||
| } | } | ||||||
|  |  | ||||||
| /** 列表的字段 */ | /** 列表的字段 */ | ||||||
| export function useGridColumns( | export function useGridColumns(): VxeTableGridOptions<${simpleClassName}Api.${simpleClassName}>['columns'] { | ||||||
|   onActionClick?: OnActionClickFn<${simpleClassName}Api.${simpleClassName}>, |  | ||||||
| ): VxeTableGridOptions<${simpleClassName}Api.${simpleClassName}>['columns'] { |  | ||||||
|   return [ |   return [ | ||||||
| #if ($table.templateType != 2 && $deleteBatchEnable) | #if ($table.templateType != 2 && $deleteBatchEnable) | ||||||
|   { type: 'checkbox', width: 40 }, |   { type: 'checkbox', width: 40 }, | ||||||
|  | |||||||
| @ -1,10 +1,9 @@ | |||||||
| <script lang="ts" setup> | <script lang="ts" setup> | ||||||
| import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter/vxe-table'; | import type { VxeTableGridOptions } from '#/adapter/vxe-table'; | ||||||
| import type { ${simpleClassName}Api } from '#/api/${table.moduleName}/${table.businessName}'; | import type { ${simpleClassName}Api } from '#/api/${table.moduleName}/${table.businessName}'; | ||||||
|  |  | ||||||
| import { Page, useVbenModal } from '@vben/common-ui'; | import { Page, useVbenModal } from '@vben/common-ui'; | ||||||
| import { Button, message,Tabs } from 'ant-design-vue'; | import { message,Tabs } from 'ant-design-vue'; | ||||||
| import { Download, Plus, Trash2 } from '@vben/icons'; |  | ||||||
| import Form from './modules/form.vue'; | import Form from './modules/form.vue'; | ||||||
|  |  | ||||||
| ## 特殊:主子表专属逻辑 | ## 特殊:主子表专属逻辑 | ||||||
| @ -16,9 +15,9 @@ import Form from './modules/form.vue'; | |||||||
|     #end |     #end | ||||||
| #end | #end | ||||||
|  |  | ||||||
| import { ref, h, computed } from 'vue'; | import { ref, computed } from 'vue'; | ||||||
| import { $t } from '#/locales'; | import { $t } from '#/locales'; | ||||||
| import { useVbenVxeGrid } from '#/adapter/vxe-table'; | import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table'; | ||||||
| #if (${table.templateType} == 2)## 树表接口 | #if (${table.templateType} == 2)## 树表接口 | ||||||
| import { get${simpleClassName}List, delete${simpleClassName}, export${simpleClassName} } from '#/api/${table.moduleName}/${table.businessName}'; | import { get${simpleClassName}List, delete${simpleClassName}, export${simpleClassName} } from '#/api/${table.moduleName}/${table.businessName}'; | ||||||
| #else## 标准表接口 | #else## 标准表接口 | ||||||
| @ -60,32 +59,34 @@ function onRefresh() { | |||||||
| } | } | ||||||
|  |  | ||||||
| /** 创建${table.classComment} */ | /** 创建${table.classComment} */ | ||||||
| function onCreate() { | function handleCreate() { | ||||||
|   formModalApi.setData({}).open(); |   formModalApi.setData({}).open(); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** 编辑${table.classComment} */ | /** 编辑${table.classComment} */ | ||||||
| function onEdit(row: ${simpleClassName}Api.${simpleClassName}) { | function handleEdit(row: ${simpleClassName}Api.${simpleClassName}) { | ||||||
|   formModalApi.setData(row).open(); |   formModalApi.setData(row).open(); | ||||||
| } | } | ||||||
|  |  | ||||||
| #if (${table.templateType} == 2)## 树表特有:新增下级 | #if (${table.templateType} == 2)## 树表特有:新增下级 | ||||||
| /** 新增下级${table.classComment} */ | /** 新增下级${table.classComment} */ | ||||||
| function onAppend(row: ${simpleClassName}Api.${simpleClassName}) { | function handleAppend(row: ${simpleClassName}Api.${simpleClassName}) { | ||||||
|   formModalApi.setData({ ${treeParentColumn.javaField}: row.id }).open(); |   formModalApi.setData({ ${treeParentColumn.javaField}: row.id }).open(); | ||||||
| } | } | ||||||
| #end | #end | ||||||
|  |  | ||||||
| /** 删除${table.classComment} */ | /** 删除${table.classComment} */ | ||||||
| async function onDelete(row: ${simpleClassName}Api.${simpleClassName}) { | async function handleDelete(row: ${simpleClassName}Api.${simpleClassName}) { | ||||||
|   const hideLoading = message.loading({ |   const hideLoading = message.loading({ | ||||||
|     content: $t('ui.actionMessage.deleting', [row.id]), |     content: $t('ui.actionMessage.deleting', [row.id]), | ||||||
|     duration: 0, |     key: 'action_key_msg', | ||||||
|     key: 'action_process_msg', |  | ||||||
|   }); |   }); | ||||||
|   try { |   try { | ||||||
|     await delete${simpleClassName}(row.id as number); |     await delete${simpleClassName}(row.id as number); | ||||||
|     message.success( $t('ui.actionMessage.deleteSuccess', [row.id]) ); |     message.success({ | ||||||
|  |       content: $t('ui.actionMessage.deleteSuccess', [row.id]), | ||||||
|  |       key: 'action_key_msg', | ||||||
|  |     }); | ||||||
|     onRefresh(); |     onRefresh(); | ||||||
|   } finally { |   } finally { | ||||||
|     hideLoading(); |     hideLoading(); | ||||||
| @ -94,15 +95,17 @@ async function onDelete(row: ${simpleClassName}Api.${simpleClassName}) { | |||||||
|  |  | ||||||
| #if ($table.templateType != 2 && $deleteBatchEnable) | #if ($table.templateType != 2 && $deleteBatchEnable) | ||||||
| /** 批量删除${table.classComment} */ | /** 批量删除${table.classComment} */ | ||||||
| async function onDeleteBatch() { | async function handleDeleteBatch() { | ||||||
|   const hideLoading = message.loading({ |   const hideLoading = message.loading({ | ||||||
|     content: $t('ui.actionMessage.deleting'), |     content: $t('ui.actionMessage.deleting'), | ||||||
|     duration: 0, |     key: 'action_key_msg', | ||||||
|     key: 'action_process_msg', |  | ||||||
|   }); |   }); | ||||||
|   try { |   try { | ||||||
|     await delete${simpleClassName}ListByIds(deleteIds.value); |     await delete${simpleClassName}ListByIds(deleteIds.value); | ||||||
|     message.success( $t('ui.actionMessage.deleteSuccess') ); |     message.success({ | ||||||
|  |       content: $t('ui.actionMessage.deleteSuccess'), | ||||||
|  |       key: 'action_key_msg', | ||||||
|  |     }); | ||||||
|     onRefresh(); |     onRefresh(); | ||||||
|   } finally { |   } finally { | ||||||
|     hideLoading(); |     hideLoading(); | ||||||
| @ -120,40 +123,17 @@ function setDeleteIds({ | |||||||
| #end | #end | ||||||
|  |  | ||||||
| /** 导出表格 */ | /** 导出表格 */ | ||||||
| async function onExport() { | async function handleExport() { | ||||||
|   const data = await export${simpleClassName}(await gridApi.formApi.getValues()); |   const data = await export${simpleClassName}(await gridApi.formApi.getValues()); | ||||||
|   downloadFileFromBlobPart({ fileName: '${table.classComment}.xls', source: data }); |   downloadFileFromBlobPart({ fileName: '${table.classComment}.xls', source: data }); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** 表格操作按钮的回调函数 */ |  | ||||||
| function onActionClick({ |  | ||||||
|   code, |  | ||||||
|   row, |  | ||||||
| }: OnActionClickParams<${simpleClassName}Api.${simpleClassName}>) { |  | ||||||
|   switch (code) { |  | ||||||
|   #if (${table.templateType} == 2)## 树表特有:新增下级 |  | ||||||
|     case 'append': { |  | ||||||
|       onAppend(row); |  | ||||||
|       break; |  | ||||||
|     } |  | ||||||
|   #end |  | ||||||
|     case 'edit': { |  | ||||||
|       onEdit(row); |  | ||||||
|       break; |  | ||||||
|     } |  | ||||||
|     case 'delete': { |  | ||||||
|       onDelete(row); |  | ||||||
|       break; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| const [Grid, gridApi] = useVbenVxeGrid({ | const [Grid, gridApi] = useVbenVxeGrid({ | ||||||
|   formOptions: { |   formOptions: { | ||||||
|     schema: useGridFormSchema(), |     schema: useGridFormSchema(), | ||||||
|   }, |   }, | ||||||
|   gridOptions: { |   gridOptions: { | ||||||
|     columns: useGridColumns(onActionClick), |     columns: useGridColumns(), | ||||||
| #if (${table.templateType} == 11) | #if (${table.templateType} == 11) | ||||||
|     height: '600px', |     height: '600px', | ||||||
| #else | #else | ||||||
| @ -245,36 +225,74 @@ const [Grid, gridApi] = useVbenVxeGrid({ | |||||||
|           </template> |           </template> | ||||||
|         #end |         #end | ||||||
|       <template #toolbar-tools> |       <template #toolbar-tools> | ||||||
| #if (${table.templateType} == 2)## 树表特有:展开/收缩按钮 |         <TableAction | ||||||
|         <Button @click="toggleExpand" class="mr-2"> |           :actions="[ | ||||||
|           {{ isExpanded ? '收缩' : '展开' }} |               #if (${table.templateType} == 2)## 树表特有:展开/收缩按钮 | ||||||
|         </Button> |               { | ||||||
| #end |                 label: isExpanded ? '收缩' : '展开', | ||||||
|         <Button :icon="h(Plus)" type="primary" @click="onCreate" v-access:code="['${table.moduleName}:${simpleClassName_strikeCase}:create']"> |                 type: 'primary', | ||||||
|           {{ $t('ui.actionTitle.create', ['${table.classComment}']) }} |                 onClick: toggleExpand, | ||||||
|         </Button> |               }, | ||||||
|         <Button |               #end | ||||||
|           :icon="h(Download)" |               { | ||||||
|           type="primary" |                 label: $t('ui.actionTitle.create', ['${table.classComment}']), | ||||||
|           class="ml-2" |                 type: 'primary', | ||||||
|           @click="onExport" |                 icon: ACTION_ICON.ADD, | ||||||
|           v-access:code="['${table.moduleName}:${simpleClassName_strikeCase}:export']" |                 auth: ['${table.moduleName}:${simpleClassName_strikeCase}:create'], | ||||||
|         > |                 onClick: handleCreate, | ||||||
|           {{ $t('ui.actionTitle.export') }} |               }, | ||||||
|         </Button> |               { | ||||||
| #if ($table.templateType != 2 && $deleteBatchEnable) |                 label: $t('ui.actionTitle.export'), | ||||||
|         <Button |                 type: 'primary', | ||||||
|             :icon="h(Trash2)" |                 icon: ACTION_ICON.DOWNLOAD, | ||||||
|             type="primary" |                 auth: ['${table.moduleName}:${simpleClassName_strikeCase}:export'], | ||||||
|             danger |                 onClick: handleExport, | ||||||
|             class="ml-2" |               }, | ||||||
|             :disabled="isEmpty(deleteIds)" |               #if ($table.templateType != 2 && $deleteBatchEnable) | ||||||
|             @click="onDeleteBatch" |               { | ||||||
|             v-access:code="['${table.moduleName}:${simpleClassName_strikeCase}:delete']" |                 label: $t('ui.actionTitle.deleteBatch'), | ||||||
|         > |                 type: 'primary', | ||||||
|           批量删除 |                 icon: ACTION_ICON.DELETE, | ||||||
|         </Button> |                 disabled: isEmpty(deleteIds), | ||||||
| #end |                 auth: ['${table.moduleName}:${simpleClassName_strikeCase}:delete'], | ||||||
|  |                 onClick: handleDeleteBatch, | ||||||
|  |               }, | ||||||
|  |               #end | ||||||
|  |             ]" | ||||||
|  |         /> | ||||||
|  |       </template> | ||||||
|  |       <template #actions="{ row }"> | ||||||
|  |         <TableAction | ||||||
|  |           :actions="[ | ||||||
|  |             #if (${table.templateType} == 2)## 树表特有:新增下级 | ||||||
|  |             { | ||||||
|  |               label: '新增下级', | ||||||
|  |               type: 'link', | ||||||
|  |               icon: ACTION_ICON.ADD, | ||||||
|  |               auth: ['${table.moduleName}:${simpleClassName_strikeCase}:create'], | ||||||
|  |               onClick: handleAppend.bind(null, row), | ||||||
|  |             }, | ||||||
|  |             #end | ||||||
|  |             { | ||||||
|  |               label: $t('common.edit'), | ||||||
|  |               type: 'link', | ||||||
|  |               icon: ACTION_ICON.EDIT, | ||||||
|  |               auth: ['${table.moduleName}:${simpleClassName_strikeCase}:update'], | ||||||
|  |               onClick: handleEdit.bind(null, row), | ||||||
|  |             }, | ||||||
|  |             { | ||||||
|  |               label: $t('common.delete'), | ||||||
|  |               type: 'link', | ||||||
|  |               danger: true, | ||||||
|  |               icon: ACTION_ICON.DELETE, | ||||||
|  |               auth: ['${table.moduleName}:${simpleClassName_strikeCase}:delete'], | ||||||
|  |               popConfirm: { | ||||||
|  |                 title: $t('ui.actionMessage.deleteConfirm', [row.id]), | ||||||
|  |                 confirm: handleDelete.bind(null, row), | ||||||
|  |               }, | ||||||
|  |             }, | ||||||
|  |           ]" | ||||||
|  |         /> | ||||||
|       </template> |       </template> | ||||||
|     </Grid> |     </Grid> | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 xingyu4j
					xingyu4j