123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import { computed, nextTick, reactive } from 'vue'
- import { SizeType, VxeGridProps, VxeTablePropTypes } from 'vxe-table'
- import { useAppStore } from '@/store/modules/app'
- import { VxeAllSchemas } from './useVxeCrudSchemas'
- import { useI18n } from '@/hooks/web/useI18n'
- import { useMessage } from '@/hooks/web/useMessage'
- import download from '@/utils/download'
- const { t } = useI18n()
- const message = useMessage()
- interface UseVxeGridConfig<T = any> {
- allSchemas: VxeAllSchemas
- topActionSlots?: boolean
- treeConfig?: VxeTablePropTypes.TreeConfig
- isList?: boolean
- getListApi: (option: any) => Promise<T>
- deleteApi?: (option: any) => Promise<T>
- exportListApi?: (option: any) => Promise<T>
- exportName?: string
- queryParams?: any
- }
- const appStore = useAppStore()
- const currentSize = computed(() => {
- 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
- }
- return resSize
- })
- export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
-
- const gridOptions = reactive<VxeGridProps<any>>({
- loading: true,
- size: currentSize as any,
- height: 730,
- rowConfig: {
- isCurrent: true,
- isHover: true
- },
- toolbarConfig: {
- slots:
- !config?.topActionSlots && config?.topActionSlots != false
- ? { buttons: 'toolbar_buttons' }
- : {}
- },
- printConfig: {
- columns: config?.allSchemas.printSchema
- },
- formConfig: {
- enabled: true,
- titleWidth: 100,
- titleAlign: 'right',
- items: config?.allSchemas.searchSchema
- },
- columns: config?.allSchemas.tableSchema,
- proxyConfig: {
- seq: true,
- form: true,
- props: { result: 'list', total: 'total' },
- ajax: {
- query: ({ page, form }) => {
- let queryParams: any = Object.assign({}, JSON.parse(JSON.stringify(form)))
- if (config?.queryParams) {
- queryParams = Object.assign(queryParams, config.queryParams)
- }
- if (!config?.treeConfig) {
- queryParams.pageSize = page.pageSize
- queryParams.pageNo = page.currentPage
- }
- gridOptions.loading = false
- return new Promise(async (resolve) => {
- resolve(await config?.getListApi(queryParams))
- })
- },
- queryAll: ({ form }) => {
- const queryParams = Object.assign({}, JSON.parse(JSON.stringify(form)))
- return new Promise(async (resolve) => {
- if (config?.exportListApi) {
- resolve(await config?.exportListApi(queryParams))
- } else {
- resolve(await config?.getListApi(queryParams))
- }
- })
- }
- }
- },
- exportConfig: {
- filename: config?.exportName,
-
- type: 'csv',
-
- modes: ['current', 'all'],
- columns: config?.allSchemas.printSchema
- }
- })
- if (config?.treeConfig) {
- gridOptions.treeConfig = config.treeConfig
- } else if (config?.isList) {
- gridOptions.proxyConfig = {
- seq: true,
- form: true,
- props: { result: 'data' },
- ajax: {
- query: ({ form }) => {
- let queryParams: any = Object.assign({}, JSON.parse(JSON.stringify(form)))
- if (config?.queryParams) {
- queryParams = Object.assign(queryParams, config.queryParams)
- }
- gridOptions.loading = false
- return new Promise(async (resolve) => {
- resolve(await config?.getListApi(queryParams))
- })
- }
- }
- }
- } else {
- gridOptions.pagerConfig = {
- border: false,
- background: true,
- perfect: false,
- pageSize: 10,
- pagerCount: 7,
- autoHidden: false,
- pageSizes: [5, 10, 20, 30, 50, 100],
- layouts: [
- 'PrevJump',
- 'PrevPage',
- 'JumpNumber',
- 'NextPage',
- 'NextJump',
- 'Sizes',
- 'FullJump',
- 'Total'
- ]
- }
- }
-
- const getList = async (ref) => {
- if (!ref) {
- console.error('未传入gridRef')
- return
- }
- await nextTick()
- ref.value.commitProxy('query')
- }
-
- const getSearchData = async (ref) => {
- if (!ref) {
- console.error('未传入gridRef')
- return
- }
- await nextTick()
- const queryParams = Object.assign(
- {},
- JSON.parse(JSON.stringify(ref.value.getProxyInfo()?.form))
- )
- return queryParams
- }
-
- const deleteData = async (ref, ids: string | number) => {
- if (!ref) {
- console.error('未传入gridRef')
- return
- }
- if (!config?.deleteApi) {
- console.error('未传入delListApi')
- return
- }
- await nextTick()
- return new Promise(async () => {
- message.delConfirm().then(async () => {
- await (config?.deleteApi && config?.deleteApi(ids))
- message.success(t('common.delSuccess'))
-
- ref.value.commitProxy('query')
- })
- })
- }
-
- const exportList = async (ref, fileName?: string) => {
- if (!ref) {
- console.error('未传入gridRef')
- return
- }
- if (!config?.exportListApi) {
- console.error('未传入exportListApi')
- return
- }
- await nextTick()
- const queryParams = Object.assign(
- {},
- JSON.parse(JSON.stringify(ref.value?.getProxyInfo()?.form))
- )
- message.exportConfirm().then(async () => {
- const res = await (config?.exportListApi && config?.exportListApi(queryParams))
- download.excel(res as unknown as Blob, fileName ? fileName : 'excel.xls')
- })
- }
-
- const zoom = async (ref) => {
- if (!ref) {
- console.error('未传入gridRef')
- return
- }
- await nextTick()
- ref.value.zoom(!ref.value.isMaximized())
- }
- return {
- gridOptions,
- getList,
- getSearchData,
- deleteData,
- exportList,
- zoom
- }
- }
|