ipms-sjy/yudao-ui-admin-vue3/src/hooks/web/useVxeGrid.ts

110 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-11-07 13:25:23 +08:00
import { computed, reactive } from 'vue'
2022-11-10 13:15:55 +08:00
import { SizeType, VxeGridProps } from 'vxe-table'
2022-11-07 13:25:23 +08:00
import { useAppStore } from '@/store/modules/app'
2022-11-07 18:13:04 +08:00
import { VxeAllSchemas } from './useVxeCrudSchemas'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
const { t } = useI18n()
const message = useMessage() // 消息弹窗
interface UseVxeGridConfig<T = any> {
allSchemas: VxeAllSchemas
getListApi: (option: any) => Promise<T>
delListApi?: (option: any) => Promise<T>
exportListApi?: (option: any) => Promise<T>
}
2022-11-07 13:25:23 +08:00
const appStore = useAppStore()
const currentSize = computed(() => {
2022-11-10 13:15:55 +08:00
let resSize: SizeType = 'small'
const appsize = appStore.getCurrentSize
switch (appsize) {
case 'large':
resSize = 'medium'
break
case 'default':
resSize = 'small'
break
case 'small':
resSize = 'mini'
break
2022-11-07 13:25:23 +08:00
}
2022-11-10 13:15:55 +08:00
return resSize
2022-11-07 13:25:23 +08:00
})
2022-11-03 14:33:30 +08:00
2022-11-07 18:13:04 +08:00
export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
2022-11-03 14:33:30 +08:00
const gridOptions = reactive<VxeGridProps>({
2022-11-07 18:13:04 +08:00
loading: true,
2022-11-10 13:15:55 +08:00
size: currentSize as any,
2022-11-04 16:43:11 +08:00
height: 800,
2022-11-03 14:33:30 +08:00
rowConfig: {
2022-11-07 18:13:04 +08:00
isCurrent: true, // 当鼠标点击行时,是否要高亮当前行
isHover: true // 当鼠标移到行时,是否要高亮当前行
},
showOverflow: 'tooltip', // 当内容溢出时显示为省略号
tooltipConfig: {
showAll: true // 开启全表工具提示
2022-11-03 14:33:30 +08:00
},
toolbarConfig: {
custom: true,
slots: { buttons: 'toolbar_buttons' }
},
printConfig: {
2022-11-07 18:13:04 +08:00
columns: config?.allSchemas.printSchema
2022-11-03 14:33:30 +08:00
},
formConfig: {
titleWidth: 100,
titleAlign: 'right',
2022-11-07 18:13:04 +08:00
items: config?.allSchemas.searchSchema
2022-11-03 14:33:30 +08:00
},
2022-11-07 18:13:04 +08:00
columns: config?.allSchemas.tableSchema,
2022-11-03 14:33:30 +08:00
pagerConfig: {
2022-11-07 18:13:04 +08:00
border: false, // 带边框
background: true, // 带背景颜色
perfect: true, // 配套的样式
pageSize: 10, // 每页大小
pagerCount: 7, // 显示页码按钮的数量
autoHidden: true, // 当只有一页时自动隐藏
pageSizes: [5, 10, 15, 20, 50, 100, 200, 500], // 每页大小选项列表
2022-11-03 14:33:30 +08:00
layouts: [
'PrevJump',
'PrevPage',
'Jump',
'PageCount',
'NextPage',
'NextJump',
'Sizes',
'Total'
]
},
proxyConfig: {
seq: true, // 启用动态序号代理(分页之后索引自动计算为当前页的起始序号)
form: true, // 启用表单代理,当点击表单提交按钮时会自动触发 reload 行为
props: { result: 'list', total: 'total' },
ajax: {
query: ({ page, form }) => {
const queryParams = Object.assign({}, form)
queryParams.pageSize = page.pageSize
queryParams.pageNo = page.currentPage
2022-11-07 15:10:48 +08:00
gridOptions.loading = false
2022-11-03 14:33:30 +08:00
return new Promise(async (resolve) => {
2022-11-07 18:13:04 +08:00
resolve(await config?.getListApi(queryParams))
2022-11-03 14:33:30 +08:00
})
}
}
}
})
2022-11-07 18:13:04 +08:00
const delList = (ids: string | number | string[] | number[]) => {
message.delConfirm().then(() => {
config?.delListApi && config?.delListApi(ids)
message.success(t('common.delSuccess'))
})
}
return {
gridOptions,
delList
}
2022-11-03 14:33:30 +08:00
}