feat: demo with code gen
This commit is contained in:
parent
41fda26248
commit
64d4878628
60
apps/web-antd/src/views/demo/demo/api/index.ts
Executable file
60
apps/web-antd/src/views/demo/demo/api/index.ts
Executable file
@ -0,0 +1,60 @@
|
||||
import type { DemoForm, DemoQuery, DemoVO } from './model';
|
||||
|
||||
import type { ID, IDS, PageResult } from '#/api/common';
|
||||
|
||||
import { commonExport } from '#/api/helper';
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 查询测试单表列表
|
||||
* @param params
|
||||
* @returns 测试单表列表
|
||||
*/
|
||||
export function demoList(params?: DemoQuery) {
|
||||
return requestClient.get<PageResult<DemoVO>>('/demo/demo/list', { params });
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出测试单表列表
|
||||
* @param params
|
||||
* @returns 测试单表列表
|
||||
*/
|
||||
export function demoExport(params?: DemoQuery) {
|
||||
return commonExport('/demo/demo/export', params ?? {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询测试单表详情
|
||||
* @param id id
|
||||
* @returns 测试单表详情
|
||||
*/
|
||||
export function demoInfo(id: ID) {
|
||||
return requestClient.get<DemoVO>(`/demo/demo/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增测试单表
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function demoAdd(data: DemoForm) {
|
||||
return requestClient.postWithMsg<void>('/demo/demo', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新测试单表
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function demoUpdate(data: DemoForm) {
|
||||
return requestClient.putWithMsg<void>('/demo/demo', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除测试单表
|
||||
* @param id id
|
||||
* @returns void
|
||||
*/
|
||||
export function demoRemove(id: ID | IDS) {
|
||||
return requestClient.deleteWithMsg<void>(`/demo/demo/${id}`);
|
||||
}
|
82
apps/web-antd/src/views/demo/demo/api/model.d.ts
vendored
Executable file
82
apps/web-antd/src/views/demo/demo/api/model.d.ts
vendored
Executable file
@ -0,0 +1,82 @@
|
||||
import type { BaseEntity, PageQuery } from '#/api/common';
|
||||
|
||||
export interface DemoVO {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id: number | string;
|
||||
|
||||
/**
|
||||
* 排序号
|
||||
*/
|
||||
orderNum: number;
|
||||
|
||||
/**
|
||||
* key键
|
||||
*/
|
||||
testKey: string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
value: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version: number;
|
||||
}
|
||||
|
||||
export interface DemoForm extends BaseEntity {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id?: number | string;
|
||||
|
||||
/**
|
||||
* 排序号
|
||||
*/
|
||||
orderNum?: number;
|
||||
|
||||
/**
|
||||
* key键
|
||||
*/
|
||||
testKey?: string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
value?: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version?: number;
|
||||
}
|
||||
|
||||
export interface DemoQuery extends PageQuery {
|
||||
/**
|
||||
* 排序号
|
||||
*/
|
||||
orderNum?: number;
|
||||
|
||||
/**
|
||||
* key键
|
||||
*/
|
||||
testKey?: string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
value?: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version?: number;
|
||||
|
||||
/**
|
||||
* 日期范围参数
|
||||
*/
|
||||
params?: any;
|
||||
}
|
91
apps/web-antd/src/views/demo/demo/data.ts
Executable file
91
apps/web-antd/src/views/demo/demo/data.ts
Executable file
@ -0,0 +1,91 @@
|
||||
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
|
||||
|
||||
export const querySchema: FormSchemaGetter = () => [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'orderNum',
|
||||
label: '排序号',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'testKey',
|
||||
label: 'key键',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'value',
|
||||
label: '值',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'version',
|
||||
label: '版本',
|
||||
},
|
||||
];
|
||||
|
||||
export const columns: VxeGridProps['columns'] = [
|
||||
{ type: 'checkbox', width: 60 },
|
||||
{
|
||||
title: '主键',
|
||||
field: 'id',
|
||||
},
|
||||
{
|
||||
title: '排序号',
|
||||
field: 'orderNum',
|
||||
},
|
||||
{
|
||||
title: 'key键',
|
||||
field: 'testKey',
|
||||
},
|
||||
{
|
||||
title: '值',
|
||||
field: 'value',
|
||||
},
|
||||
{
|
||||
title: '版本',
|
||||
field: 'version',
|
||||
},
|
||||
{
|
||||
field: 'action',
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' },
|
||||
title: '操作',
|
||||
width: 180,
|
||||
},
|
||||
];
|
||||
|
||||
export const modalSchema: FormSchemaGetter = () => [
|
||||
{
|
||||
label: '主键',
|
||||
fieldName: 'id',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
show: () => false,
|
||||
triggerFields: [''],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: '排序号',
|
||||
fieldName: 'orderNum',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: 'key键',
|
||||
fieldName: 'testKey',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '值',
|
||||
fieldName: 'value',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '版本',
|
||||
fieldName: 'version',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
];
|
87
apps/web-antd/src/views/demo/demo/demo-modal.vue
Executable file
87
apps/web-antd/src/views/demo/demo/demo-modal.vue
Executable file
@ -0,0 +1,87 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep } from '@vben/utils';
|
||||
|
||||
import { useVbenForm } from '#/adapter';
|
||||
|
||||
import { demoAdd, demoInfo, demoUpdate } from './api';
|
||||
import { modalSchema } from './data';
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
const isUpdate = ref(false);
|
||||
const title = computed(() => {
|
||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||
});
|
||||
|
||||
const [BasicForm, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
// 默认占满两列
|
||||
formItemClass: 'col-span-2',
|
||||
// 默认label宽度 px
|
||||
labelWidth: 80,
|
||||
// 通用配置项 会影响到所有表单项
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
},
|
||||
schema: modalSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
});
|
||||
|
||||
const [BasicModal, modalApi] = useVbenModal({
|
||||
fullscreenButton: false,
|
||||
onCancel: handleCancel,
|
||||
onConfirm: handleConfirm,
|
||||
onOpenChange: async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
return null;
|
||||
}
|
||||
modalApi.modalLoading(true);
|
||||
|
||||
const { id } = modalApi.getData() as { id?: number | string };
|
||||
isUpdate.value = !!id;
|
||||
|
||||
if (isUpdate.value && id) {
|
||||
const record = await demoInfo(id);
|
||||
await formApi.setValues(record);
|
||||
}
|
||||
|
||||
modalApi.modalLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
async function handleConfirm() {
|
||||
try {
|
||||
modalApi.modalLoading(true);
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
|
||||
const data = cloneDeep(await formApi.getValues());
|
||||
await (isUpdate.value ? demoUpdate(data) : demoAdd(data));
|
||||
emit('reload');
|
||||
await handleCancel();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
modalApi.modalLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
modalApi.close();
|
||||
await formApi.resetForm();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicModal :close-on-click-modal="false" :title="title" class="w-[550px]">
|
||||
<BasicForm />
|
||||
</BasicModal>
|
||||
</template>
|
@ -1,9 +1,183 @@
|
||||
<script setup lang="ts">
|
||||
import CommonSkeleton from '#/views/common';
|
||||
import type { Recordable } from '@vben/types';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
|
||||
|
||||
import { Modal, Popconfirm, Space } from 'ant-design-vue';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
|
||||
import { downloadExcel } from '#/utils/file/download';
|
||||
|
||||
import { demoExport, demoList, demoRemove } from './api';
|
||||
import { columns, querySchema } from './data';
|
||||
import demoModal from './demo-modal.vue';
|
||||
|
||||
const formOptions: VbenFormProps = {
|
||||
commonConfig: {
|
||||
labelWidth: 80,
|
||||
},
|
||||
schema: querySchema(),
|
||||
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps = {
|
||||
checkboxConfig: {
|
||||
// 高亮
|
||||
highlight: true,
|
||||
// 翻页时保留选中状态
|
||||
reserve: true,
|
||||
// 点击行选中
|
||||
// trigger: 'row',
|
||||
},
|
||||
columns,
|
||||
height: 'auto',
|
||||
keepSource: true,
|
||||
pagerConfig: {},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }, formValues = {}) => {
|
||||
// 区间选择器处理
|
||||
if (formValues?.createTime) {
|
||||
formValues.params = {
|
||||
beginTime: dayjs(formValues.createTime[0]).format(
|
||||
'YYYY-MM-DD 00:00:00',
|
||||
),
|
||||
endTime: dayjs(formValues.createTime[1]).format(
|
||||
'YYYY-MM-DD 23:59:59',
|
||||
),
|
||||
};
|
||||
Reflect.deleteProperty(formValues, 'createTime');
|
||||
} else {
|
||||
Reflect.deleteProperty(formValues, 'params');
|
||||
}
|
||||
|
||||
return await demoList({
|
||||
pageNum: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
...formValues,
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
isHover: true,
|
||||
keyField: 'id',
|
||||
},
|
||||
round: true,
|
||||
align: 'center',
|
||||
showOverflow: true,
|
||||
};
|
||||
|
||||
const checked = ref(false);
|
||||
const [BasicTable, tableApi] = useVbenVxeGrid({
|
||||
formOptions,
|
||||
gridOptions,
|
||||
gridEvents: {
|
||||
checkboxChange: (e: any) => {
|
||||
checked.value = e.records.length > 0;
|
||||
},
|
||||
checkboxAll: (e: any) => {
|
||||
checked.value = e.records.length > 0;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const [DemoModal, modalApi] = useVbenModal({
|
||||
connectedComponent: demoModal,
|
||||
});
|
||||
|
||||
function handleAdd() {
|
||||
modalApi.setData({});
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
async function handleEdit(record: Recordable<any>) {
|
||||
modalApi.setData({ id: record.id });
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
async function handleDelete(row: Recordable<any>) {
|
||||
await demoRemove(row.id);
|
||||
await tableApi.query();
|
||||
}
|
||||
|
||||
function handleMultiDelete() {
|
||||
const rows = tableApi.grid.getCheckboxRecords();
|
||||
const ids = rows.map((row: any) => row.id);
|
||||
Modal.confirm({
|
||||
title: '提示',
|
||||
okType: 'danger',
|
||||
content: `确认删除选中的${ids.length}条记录吗?`,
|
||||
onOk: async () => {
|
||||
await demoRemove(ids);
|
||||
await tableApi.query();
|
||||
checked.value = false;
|
||||
},
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<CommonSkeleton />
|
||||
</div>
|
||||
<Page :auto-content-height="true">
|
||||
<BasicTable>
|
||||
<template #toolbar-actions>
|
||||
<span class="pl-[7px] text-[16px]">测试单表列表</span>
|
||||
</template>
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<a-button
|
||||
v-access:code="['demo:demo:export']"
|
||||
@click="downloadExcel(demoExport, '测试单表数据', {})"
|
||||
>
|
||||
{{ $t('pages.common.export') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
:disabled="!checked"
|
||||
danger
|
||||
type="primary"
|
||||
v-access:code="['demo:demo:remove']"
|
||||
@click="handleMultiDelete"
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
type="primary"
|
||||
v-access:code="['demo:demo:add']"
|
||||
@click="handleAdd"
|
||||
>
|
||||
{{ $t('pages.common.add') }}
|
||||
</a-button>
|
||||
</Space>
|
||||
</template>
|
||||
<template #action="{ row }">
|
||||
<a-button
|
||||
size="small"
|
||||
type="link"
|
||||
v-access:code="['demo:demo:edit']"
|
||||
@click="handleEdit(row)"
|
||||
>
|
||||
{{ $t('pages.common.edit') }}
|
||||
</a-button>
|
||||
<Popconfirm
|
||||
placement="left"
|
||||
title="确认删除?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<a-button
|
||||
danger
|
||||
size="small"
|
||||
type="link"
|
||||
v-access:code="['demo:demo:remove']"
|
||||
@click.stop=""
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</a-button>
|
||||
</Popconfirm>
|
||||
</template>
|
||||
</BasicTable>
|
||||
<DemoModal @reload="tableApi.query()" />
|
||||
</Page>
|
||||
</template>
|
||||
|
50
apps/web-antd/src/views/demo/tree/api/index.ts
Executable file
50
apps/web-antd/src/views/demo/tree/api/index.ts
Executable file
@ -0,0 +1,50 @@
|
||||
import type { TreeForm, TreeQuery, TreeVO } from './model';
|
||||
|
||||
import type { ID, IDS } from '#/api/common';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 查询测试树列表
|
||||
* @param params
|
||||
* @returns 测试树列表
|
||||
*/
|
||||
export function treeList(params?: TreeQuery) {
|
||||
return requestClient.get<TreeVO[]>('/demo/tree/list', { params });
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询测试树详情
|
||||
* @param id id
|
||||
* @returns 测试树详情
|
||||
*/
|
||||
export function treeInfo(id: ID) {
|
||||
return requestClient.get<TreeVO>(`/demo/tree/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增测试树
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function treeAdd(data: TreeForm) {
|
||||
return requestClient.postWithMsg<void>('/demo/tree', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新测试树
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function treeUpdate(data: TreeForm) {
|
||||
return requestClient.putWithMsg<void>('/demo/tree', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除测试树
|
||||
* @param id id
|
||||
* @returns void
|
||||
*/
|
||||
export function treeRemove(id: ID | IDS) {
|
||||
return requestClient.deleteWithMsg<void>(`/demo/tree/${id}`);
|
||||
}
|
102
apps/web-antd/src/views/demo/tree/api/model.d.ts
vendored
Executable file
102
apps/web-antd/src/views/demo/tree/api/model.d.ts
vendored
Executable file
@ -0,0 +1,102 @@
|
||||
import type { BaseEntity } from '#/api/common';
|
||||
|
||||
export interface TreeVO {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id: number | string;
|
||||
|
||||
/**
|
||||
* 父id
|
||||
*/
|
||||
parentId: number | string;
|
||||
|
||||
/**
|
||||
* 部门id
|
||||
*/
|
||||
deptId: number | string;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId: number | string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
treeName: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version: number;
|
||||
|
||||
/**
|
||||
* 子对象
|
||||
*/
|
||||
children: TreeVO[];
|
||||
}
|
||||
|
||||
export interface TreeForm extends BaseEntity {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
id?: number | string;
|
||||
|
||||
/**
|
||||
* 父id
|
||||
*/
|
||||
parentId?: number | string;
|
||||
|
||||
/**
|
||||
* 部门id
|
||||
*/
|
||||
deptId?: number | string;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId?: number | string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
treeName?: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version?: number;
|
||||
}
|
||||
|
||||
export interface TreeQuery {
|
||||
/**
|
||||
* 父id
|
||||
*/
|
||||
parentId?: number | string;
|
||||
|
||||
/**
|
||||
* 部门id
|
||||
*/
|
||||
deptId?: number | string;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
userId?: number | string;
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
treeName?: string;
|
||||
|
||||
/**
|
||||
* 版本
|
||||
*/
|
||||
version?: number;
|
||||
|
||||
/**
|
||||
* 日期范围参数
|
||||
*/
|
||||
params?: any;
|
||||
}
|
106
apps/web-antd/src/views/demo/tree/data.ts
Executable file
106
apps/web-antd/src/views/demo/tree/data.ts
Executable file
@ -0,0 +1,106 @@
|
||||
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
|
||||
|
||||
export const querySchema: FormSchemaGetter = () => [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'parentId',
|
||||
label: '父id',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'deptId',
|
||||
label: '部门id',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'userId',
|
||||
label: '用户id',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'treeName',
|
||||
label: '值',
|
||||
},
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'version',
|
||||
label: '版本',
|
||||
},
|
||||
];
|
||||
|
||||
export const columns: VxeGridProps['columns'] = [
|
||||
{
|
||||
title: '主键',
|
||||
field: 'id',
|
||||
treeNode: true,
|
||||
},
|
||||
{
|
||||
title: '父id',
|
||||
field: 'parentId',
|
||||
},
|
||||
{
|
||||
title: '部门id',
|
||||
field: 'deptId',
|
||||
},
|
||||
{
|
||||
title: '用户id',
|
||||
field: 'userId',
|
||||
},
|
||||
{
|
||||
title: '值',
|
||||
field: 'treeName',
|
||||
},
|
||||
{
|
||||
title: '版本',
|
||||
field: 'version',
|
||||
},
|
||||
{
|
||||
field: 'action',
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' },
|
||||
title: '操作',
|
||||
width: 180,
|
||||
},
|
||||
];
|
||||
|
||||
export const modalSchema: FormSchemaGetter = () => [
|
||||
{
|
||||
label: '主键',
|
||||
fieldName: 'id',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
show: () => false,
|
||||
triggerFields: [''],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: '父id',
|
||||
fieldName: 'parentId',
|
||||
component: 'TreeSelect',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '部门id',
|
||||
fieldName: 'deptId',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '用户id',
|
||||
fieldName: 'userId',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '值',
|
||||
fieldName: 'treeName',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '版本',
|
||||
fieldName: 'version',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
];
|
@ -1,9 +1,147 @@
|
||||
<script setup lang="ts">
|
||||
import CommonSkeleton from '#/views/common';
|
||||
import type { Recordable } from '@vben/types';
|
||||
|
||||
import { nextTick } from 'vue';
|
||||
|
||||
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
|
||||
import { listToTree } from '@vben/utils';
|
||||
|
||||
import { Popconfirm, Space } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
|
||||
|
||||
import { treeList, treeRemove } from './api';
|
||||
import { columns, querySchema } from './data';
|
||||
import treeModal from './tree-modal.vue';
|
||||
|
||||
const formOptions: VbenFormProps = {
|
||||
commonConfig: {
|
||||
labelWidth: 80,
|
||||
},
|
||||
schema: querySchema(),
|
||||
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps = {
|
||||
columns,
|
||||
height: 'auto',
|
||||
keepSource: true,
|
||||
pagerConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async (_, formValues = {}) => {
|
||||
const resp = await treeList({
|
||||
...formValues,
|
||||
});
|
||||
const treeData = listToTree(resp, {
|
||||
id: 'id',
|
||||
pid: 'parentId',
|
||||
children: 'children',
|
||||
});
|
||||
return { rows: treeData };
|
||||
},
|
||||
// 默认请求接口后展开全部 不需要可以删除这段
|
||||
querySuccess: () => {
|
||||
nextTick(() => {
|
||||
expandAll();
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
isHover: true,
|
||||
keyField: 'id',
|
||||
},
|
||||
round: true,
|
||||
align: 'center',
|
||||
showOverflow: true,
|
||||
treeConfig: {
|
||||
parentField: 'parentId',
|
||||
rowField: 'id',
|
||||
transform: false,
|
||||
},
|
||||
};
|
||||
|
||||
const [BasicTable, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
|
||||
const [TreeModal, modalApi] = useVbenModal({
|
||||
connectedComponent: treeModal,
|
||||
});
|
||||
|
||||
function handleAdd() {
|
||||
modalApi.setData({ update: false });
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
async function handleEdit(row: Recordable<any>) {
|
||||
modalApi.setData({ id: row.id, update: true });
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
async function handleDelete(row: Recordable<any>) {
|
||||
await treeRemove(row.id);
|
||||
await tableApi.query();
|
||||
}
|
||||
|
||||
function expandAll() {
|
||||
tableApi.grid?.setAllTreeExpand(true);
|
||||
}
|
||||
|
||||
function collapseAll() {
|
||||
tableApi.grid?.setAllTreeExpand(false);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<CommonSkeleton />
|
||||
</div>
|
||||
<Page :auto-content-height="true">
|
||||
<BasicTable>
|
||||
<template #toolbar-actions>
|
||||
<span class="pl-[7px] text-[16px]">测试树列表</span>
|
||||
</template>
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<a-button @click="collapseAll">
|
||||
{{ $t('pages.common.collapse') }}
|
||||
</a-button>
|
||||
<a-button @click="expandAll">
|
||||
{{ $t('pages.common.expand') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
type="primary"
|
||||
v-access:code="['demo:tree:add']"
|
||||
@click="handleAdd"
|
||||
>
|
||||
{{ $t('pages.common.add') }}
|
||||
</a-button>
|
||||
</Space>
|
||||
</template>
|
||||
<template #action="{ row }">
|
||||
<a-button
|
||||
size="small"
|
||||
type="link"
|
||||
v-access:code="['demo:tree:edit']"
|
||||
@click="handleEdit(row)"
|
||||
>
|
||||
{{ $t('pages.common.edit') }}
|
||||
</a-button>
|
||||
<Popconfirm
|
||||
placement="left"
|
||||
title="确认删除?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<a-button
|
||||
danger
|
||||
size="small"
|
||||
type="link"
|
||||
v-access:code="['demo:tree:remove']"
|
||||
@click.stop=""
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</a-button>
|
||||
</Popconfirm>
|
||||
</template>
|
||||
</BasicTable>
|
||||
<TreeModal @reload="tableApi.query()" />
|
||||
</Page>
|
||||
</template>
|
||||
|
104
apps/web-antd/src/views/demo/tree/tree-modal.vue
Executable file
104
apps/web-antd/src/views/demo/tree/tree-modal.vue
Executable file
@ -0,0 +1,104 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep, listToTree } from '@vben/utils';
|
||||
|
||||
import { useVbenForm } from '#/adapter';
|
||||
|
||||
import { treeAdd, treeInfo, treeList, treeUpdate } from './api';
|
||||
import { modalSchema } from './data';
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
const isUpdate = ref(false);
|
||||
const title = computed(() => {
|
||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||
});
|
||||
|
||||
const [BasicForm, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
// 默认占满两列
|
||||
formItemClass: 'col-span-2',
|
||||
// 默认label宽度 px
|
||||
labelWidth: 80,
|
||||
// 通用配置项 会影响到所有表单项
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
},
|
||||
schema: modalSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
});
|
||||
|
||||
async function setupTreeSelect() {
|
||||
const listData = await treeList();
|
||||
const treeData = listToTree(listData, { id: 'id', pid: 'parentId' });
|
||||
formApi.updateSchema([
|
||||
{
|
||||
fieldName: 'parentId',
|
||||
componentProps: {
|
||||
treeData,
|
||||
treeLine: { showLeafIcon: false },
|
||||
fieldNames: { label: 'treeName', value: 'id' },
|
||||
treeDefaultExpandAll: true,
|
||||
},
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
const [BasicModal, modalApi] = useVbenModal({
|
||||
fullscreenButton: false,
|
||||
onCancel: handleCancel,
|
||||
onConfirm: handleConfirm,
|
||||
onOpenChange: async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
return null;
|
||||
}
|
||||
modalApi.modalLoading(true);
|
||||
|
||||
const { id } = modalApi.getData() as { id?: number | string };
|
||||
isUpdate.value = !!id;
|
||||
|
||||
if (isUpdate.value && id) {
|
||||
const record = await treeInfo(id);
|
||||
await formApi.setValues(record);
|
||||
}
|
||||
await setupTreeSelect();
|
||||
|
||||
modalApi.modalLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
async function handleConfirm() {
|
||||
try {
|
||||
modalApi.modalLoading(true);
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
|
||||
const data = cloneDeep(await formApi.getValues());
|
||||
await (isUpdate.value ? treeUpdate(data) : treeAdd(data));
|
||||
emit('reload');
|
||||
await handleCancel();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
modalApi.modalLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
modalApi.close();
|
||||
await formApi.resetForm();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicModal :close-on-click-modal="false" :title="title" class="w-[550px]">
|
||||
<BasicForm />
|
||||
</BasicModal>
|
||||
</template>
|
Loading…
Reference in New Issue
Block a user