perf: vxe demo

pull/2/head
xingyu4j 2022-11-07 18:13:04 +08:00
parent 1a45526a9d
commit dc84ebde9d
5 changed files with 113 additions and 68 deletions

View File

@ -5,7 +5,7 @@ export type PostVO = {
sort: number sort: number
status: number status: number
remark: string remark: string
createTime: string createTime?: string
} }
export type PostPageReqVO = { export type PostPageReqVO = {

View File

@ -3,6 +3,7 @@ import { getIntDictOptions } from '@/utils/dict'
import { reactive } from 'vue' import { reactive } from 'vue'
import { import {
FormItemRenderOptions, FormItemRenderOptions,
VxeColumnPropTypes,
VxeFormItemProps, VxeFormItemProps,
VxeGridPropTypes, VxeGridPropTypes,
VxeTableDefines VxeTableDefines
@ -10,10 +11,13 @@ import {
import { eachTree } from 'xe-utils' import { eachTree } from 'xe-utils'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { VxeTableColumn } from '@/types/table' import { VxeTableColumn } from '@/types/table'
import { FormSchema } from '@/types/form'
import { ComponentOptions } from '@/types/components'
export type VxeCrudSchema = Omit<VxeTableColumn, 'children'> & { export type VxeCrudSchema = Omit<VxeTableColumn, 'children'> & {
field: string field: string
title?: string title?: string
formatter?: VxeColumnPropTypes.Formatter
search?: CrudSearchParams search?: CrudSearchParams
table?: CrudTableParams table?: CrudTableParams
form?: CrudFormParams form?: CrudFormParams
@ -35,7 +39,7 @@ type CrudTableParams = {
type CrudFormParams = { type CrudFormParams = {
// 是否显示表单项 // 是否显示表单项
show?: boolean show?: boolean
} & Omit<VxeFormItemProps, 'field'> } & Omit<FormSchema, 'field'>
type CrudDescriptionsParams = { type CrudDescriptionsParams = {
// 是否显示表单项 // 是否显示表单项
@ -47,10 +51,10 @@ type CrudPrintParams = {
show?: boolean show?: boolean
} & Omit<VxeTableDefines.ColumnInfo[], 'field'> } & Omit<VxeTableDefines.ColumnInfo[], 'field'>
interface VxeAllSchemas { export type VxeAllSchemas = {
searchSchema: VxeFormItemProps[] searchSchema: VxeFormItemProps[]
tableSchema: VxeGridPropTypes.Columns tableSchema: VxeGridPropTypes.Columns
formSchema: VxeFormItemProps[] formSchema: FormSchema[]
detailSchema: DescriptionsSchema[] detailSchema: DescriptionsSchema[]
printSchema: VxeTableDefines.ColumnInfo[] printSchema: VxeTableDefines.ColumnInfo[]
} }
@ -160,6 +164,9 @@ const filterTableSchema = (crudSchema: VxeCrudSchema[]): VxeGridPropTypes.Column
field: schemaItem.field, field: schemaItem.field,
title: schemaItem.table?.title || schemaItem.title title: schemaItem.table?.title || schemaItem.title
} }
if (schemaItem?.formatter) {
tableSchemaItem.formatter = schemaItem.formatter
}
// 删除不必要的字段 // 删除不必要的字段
delete tableSchemaItem.show delete tableSchemaItem.show
@ -171,32 +178,31 @@ const filterTableSchema = (crudSchema: VxeCrudSchema[]): VxeGridPropTypes.Column
} }
// 过滤 form 结构 // 过滤 form 结构
const filterFormSchema = (crudSchema: VxeCrudSchema[]): VxeFormItemProps[] => { const filterFormSchema = (crudSchema: VxeCrudSchema[]): FormSchema[] => {
const formSchema: VxeFormItemProps[] = [] const formSchema: FormSchema[] = []
const { t } = useI18n()
eachTree(crudSchema, (schemaItem: VxeCrudSchema) => { eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
// 判断是否显示 // 判断是否显示
if (schemaItem?.form?.show !== false) { if (schemaItem?.form?.show !== false) {
let itemRenderName = schemaItem?.form?.itemRender?.name || '$input' let component = schemaItem?.form?.component || 'Input'
let itemRender: FormItemRenderOptions = { const options: ComponentOptions[] = []
name: itemRenderName, let comonentProps = {}
props: { placeholder: t('common.inputText') }
}
if (schemaItem.dictType) { if (schemaItem.dictType) {
if (!(schemaItem.form && schemaItem.form.itemRender?.name)) itemRenderName = '$select' getIntDictOptions(schemaItem.dictType).forEach((dict) => {
itemRender = { options.push(dict)
name: itemRenderName, })
options: getIntDictOptions(schemaItem.dictType), comonentProps = {
props: { placeholder: t('common.selectText') } options: options
} }
if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
} }
const formSchemaItem = { const formSchemaItem = {
// 默认为 input // 默认为 input
itemRender: itemRender, component: component,
componentProps: comonentProps,
...schemaItem.form, ...schemaItem.form,
span: schemaItem.form?.span || 12,
field: schemaItem.field, field: schemaItem.field,
title: schemaItem.form?.title || schemaItem.title label: schemaItem.form?.label || schemaItem.title
} }
// 删除不必要的字段 // 删除不必要的字段

View File

@ -1,6 +1,19 @@
import { computed, reactive } from 'vue' import { computed, reactive } from 'vue'
import { VxeGridProps } from 'vxe-table' import { VxeGridProps } from 'vxe-table'
import { useAppStore } from '@/store/modules/app' import { useAppStore } from '@/store/modules/app'
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>
}
const appStore = useAppStore() const appStore = useAppStore()
@ -14,36 +27,40 @@ const currentSize = computed(() => {
} }
}) })
export const useVxeGrid = (allSchemas, getPageApi) => { export const useVxeGrid = <T = any>(config?: UseVxeGridConfig<T>) => {
const gridOptions = reactive<VxeGridProps>({ const gridOptions = reactive<VxeGridProps>({
loading: false, loading: true,
size: currentSize.value, size: currentSize.value,
height: 800, height: 800,
rowConfig: { rowConfig: {
keyField: 'id', isCurrent: true, // 当鼠标点击行时,是否要高亮当前行
isCurrent: true, isHover: true // 当鼠标移到行时,是否要高亮当前行
isHover: true },
showOverflow: 'tooltip', // 当内容溢出时显示为省略号
tooltipConfig: {
showAll: true // 开启全表工具提示
}, },
toolbarConfig: { toolbarConfig: {
custom: true, custom: true,
slots: { buttons: 'toolbar_buttons' } slots: { buttons: 'toolbar_buttons' }
}, },
printConfig: { printConfig: {
columns: allSchemas.printSchema columns: config?.allSchemas.printSchema
}, },
formConfig: { formConfig: {
titleWidth: 100, titleWidth: 100,
titleAlign: 'right', titleAlign: 'right',
items: allSchemas.searchSchema items: config?.allSchemas.searchSchema
}, },
columns: allSchemas.tableSchema, columns: config?.allSchemas.tableSchema,
pagerConfig: { pagerConfig: {
border: false, border: false, // 带边框
background: false, background: true, // 带背景颜色
perfect: true, perfect: true, // 配套的样式
pageSize: 10, pageSize: 10, // 每页大小
pagerCount: 7, pagerCount: 7, // 显示页码按钮的数量
pageSizes: [5, 10, 15, 20, 50, 100, 200, 500], autoHidden: true, // 当只有一页时自动隐藏
pageSizes: [5, 10, 15, 20, 50, 100, 200, 500], // 每页大小选项列表
layouts: [ layouts: [
'PrevJump', 'PrevJump',
'PrevPage', 'PrevPage',
@ -61,17 +78,25 @@ export const useVxeGrid = (allSchemas, getPageApi) => {
props: { result: 'list', total: 'total' }, props: { result: 'list', total: 'total' },
ajax: { ajax: {
query: ({ page, form }) => { query: ({ page, form }) => {
gridOptions.loading = true
const queryParams = Object.assign({}, form) const queryParams = Object.assign({}, form)
queryParams.pageSize = page.pageSize queryParams.pageSize = page.pageSize
queryParams.pageNo = page.currentPage queryParams.pageNo = page.currentPage
gridOptions.loading = false gridOptions.loading = false
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
resolve(await getPageApi(queryParams)) resolve(await config?.getListApi(queryParams))
}) })
} }
} }
} }
}) })
return gridOptions const delList = (ids: string | number | string[] | number[]) => {
message.delConfirm().then(() => {
config?.delListApi && config?.delListApi(ids)
message.success(t('common.delSuccess'))
})
}
return {
gridOptions,
delList
}
} }

View File

@ -44,12 +44,11 @@
<XModal id="postModel" v-model="dialogVisible" :title="dialogTitle"> <XModal id="postModel" v-model="dialogVisible" :title="dialogTitle">
<template #default> <template #default>
<!-- 对话框(添加 / 修改) --> <!-- 对话框(添加 / 修改) -->
<vxe-form <Form
ref="xForm"
v-if="['create', 'update'].includes(actionType)" v-if="['create', 'update'].includes(actionType)"
:data="formData" :schema="allSchemas.formSchema"
:items="formItems"
:rules="rules" :rules="rules"
ref="formRef"
/> />
<Descriptions <Descriptions
v-if="actionType === 'detail'" v-if="actionType === 'detail'"
@ -77,7 +76,7 @@
</XModal> </XModal>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { ref, unref } from 'vue'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { DICT_TYPE } from '@/utils/dict' import { DICT_TYPE } from '@/utils/dict'
import * as PostApi from '@/api/system/post' import * as PostApi from '@/api/system/post'
@ -86,20 +85,22 @@ import { rules, allSchemas } from './post.data'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage' import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid' import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeFormEvents, VxeFormInstance, VxeFormItemProps, VxeGridInstance } from 'vxe-table' import { VxeFormEvents, VxeGridInstance } from 'vxe-table'
import { FormExpose } from '@/components/Form'
const { t } = useI18n() // const { t } = useI18n() //
const message = useMessage() // const message = useMessage() //
const xGrid = ref<VxeGridInstance>() const xGrid = ref<VxeGridInstance>()
const xForm = ref<VxeFormInstance>() const formRef = ref<FormExpose>() // Ref
const dialogVisible = ref(false) // const dialogVisible = ref(false) //
const dialogTitle = ref('edit') // const dialogTitle = ref('edit') //
const actionType = ref('') // const actionType = ref('') //
const actionLoading = ref(false) // Loading const actionLoading = ref(false) // Loading
const gridOptions = useVxeGrid(allSchemas, PostApi.getPostPageApi) const { gridOptions } = useVxeGrid<PostVO>({
const formData = ref<PostVO>() allSchemas: allSchemas,
const formItems = ref<VxeFormItemProps[]>(allSchemas.formSchema) getListApi: PostApi.getPostPageApi
})
// //
const setDialogTile = (type: string) => { const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type) dialogTitle.value = t('action.' + type)
@ -116,7 +117,8 @@ const handleDetail = (row: PostVO) => {
// //
const handleCreate = () => { const handleCreate = () => {
setDialogTile('create') setDialogTile('create')
formData.value = undefined //
unref(formRef)?.getElFormRef()?.resetFields()
} }
// //
@ -124,10 +126,10 @@ const handleUpdate = async (rowId: number) => {
setDialogTile('update') setDialogTile('update')
// //
const res = await PostApi.getPostApi(rowId) const res = await PostApi.getPostApi(rowId)
formData.value = res unref(formRef)?.setValues(res)
} }
// //
const handleDelete = (rowId: number) => { const handleDelete = async (rowId: number) => {
message message
.delConfirm() .delConfirm()
.then(async () => { .then(async () => {
@ -140,22 +142,28 @@ const handleDelete = (rowId: number) => {
} }
// //
const submitForm: VxeFormEvents.Submit = async () => { const submitForm: VxeFormEvents.Submit = async () => {
actionLoading.value = true const elForm = unref(formRef)?.getElFormRef()
// if (!elForm) return
try { elForm.validate(async (valid) => {
const data = formData.value as PostVO if (valid) {
if (actionType.value === 'create') { actionLoading.value = true
await PostApi.createPostApi(data) //
message.success(t('common.createSuccess')) try {
} else { const data = unref(formRef)?.formModel as PostVO
await PostApi.updatePostApi(data) if (actionType.value === 'create') {
message.success(t('common.updateSuccess')) await PostApi.createPostApi(data)
message.success(t('common.createSuccess'))
} else {
await PostApi.updatePostApi(data)
message.success(t('common.updateSuccess'))
}
//
dialogVisible.value = false
} finally {
actionLoading.value = false
xGrid.value?.commitProxy('query')
}
} }
// })
dialogVisible.value = false
} finally {
actionLoading.value = false
xGrid.value?.commitProxy('query')
}
} }
</script> </script>

View File

@ -56,9 +56,17 @@ const crudSchemas = reactive<VxeCrudSchema[]>([
show: true show: true
} }
}, },
{
title: '备注',
field: 'remark',
table: {
show: false
}
},
{ {
title: t('common.createTime'), title: t('common.createTime'),
field: 'createTime', field: 'createTime',
formatter: 'formatDate',
form: { form: {
show: false show: false
} }
@ -66,10 +74,8 @@ const crudSchemas = reactive<VxeCrudSchema[]>([
{ {
title: t('table.action'), title: t('table.action'),
field: 'action', field: 'action',
width: '240px',
table: { table: {
width: '240px', width: '240px',
showOverflow: true,
slots: { slots: {
default: 'action_default' default: 'action_default'
} }