refactor: 重新生成主子表标准模式代码预览
parent
d3d250f16f
commit
7ab66aa363
|
@ -10,6 +10,7 @@ export namespace Demo03StudentApi {
|
||||||
name?: string; // 名字
|
name?: string; // 名字
|
||||||
score?: number; // 分数
|
score?: number; // 分数
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 学生班级信息 */
|
/** 学生班级信息 */
|
||||||
export interface Demo03Grade {
|
export interface Demo03Grade {
|
||||||
id: number; // 编号
|
id: number; // 编号
|
||||||
|
@ -17,6 +18,7 @@ export namespace Demo03StudentApi {
|
||||||
name?: string; // 名字
|
name?: string; // 名字
|
||||||
teacher?: string; // 班主任
|
teacher?: string; // 班主任
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 学生信息 */
|
/** 学生信息 */
|
||||||
export interface Demo03Student {
|
export interface Demo03Student {
|
||||||
id: number; // 编号
|
id: number; // 编号
|
||||||
|
|
|
@ -56,7 +56,7 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||||
fieldName: 'description',
|
fieldName: 'description',
|
||||||
label: '简介',
|
label: '简介',
|
||||||
rules: 'required',
|
rules: 'required',
|
||||||
component: 'Textarea',
|
component: 'RichTextarea',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -176,7 +176,8 @@ export function useGridColumns(
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== 子表(学生课程) ====================
|
// ==================== 子表(学生课程) ====================
|
||||||
/** 新增/修改的列表的字段 */
|
|
||||||
|
/** 新增/修改列表的字段 */
|
||||||
export function useDemo03CourseGridEditColumns(
|
export function useDemo03CourseGridEditColumns(
|
||||||
onActionClick?: OnActionClickFn<Demo03StudentApi.Demo03Course>,
|
onActionClick?: OnActionClickFn<Demo03StudentApi.Demo03Course>,
|
||||||
): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
|
): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
|
||||||
|
@ -218,7 +219,9 @@ export function useDemo03CourseGridEditColumns(
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== 子表(学生班级) ====================
|
// ==================== 子表(学生班级) ====================
|
||||||
|
|
||||||
/** 新增/修改的表单 */
|
/** 新增/修改的表单 */
|
||||||
export function useDemo03GradeFormSchema(): VbenFormSchema[] {
|
export function useDemo03GradeFormSchema(): VbenFormSchema[] {
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -25,12 +25,6 @@ function onRefresh() {
|
||||||
gridApi.query();
|
gridApi.query();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 导出表格 */
|
|
||||||
async function onExport() {
|
|
||||||
const data = await exportDemo03Student(await gridApi.formApi.getValues());
|
|
||||||
downloadByData(data, '学生.xls');
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 创建学生 */
|
/** 创建学生 */
|
||||||
function onCreate() {
|
function onCreate() {
|
||||||
formModalApi.setData({}).open();
|
formModalApi.setData({}).open();
|
||||||
|
@ -60,17 +54,23 @@ async function onDelete(row: Demo03StudentApi.Demo03Student) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 导出表格 */
|
||||||
|
async function onExport() {
|
||||||
|
const data = await exportDemo03Student(await gridApi.formApi.getValues());
|
||||||
|
downloadByData(data, '学生.xls');
|
||||||
|
}
|
||||||
|
|
||||||
/** 表格操作按钮的回调函数 */
|
/** 表格操作按钮的回调函数 */
|
||||||
function onActionClick({ code, row }: OnActionClickParams<Demo03StudentApi.Demo03Student>) {
|
function onActionClick({ code, row }: OnActionClickParams<Demo03StudentApi.Demo03Student>) {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 'delete': {
|
|
||||||
onDelete(row);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'edit': {
|
case 'edit': {
|
||||||
onEdit(row);
|
onEdit(row);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'delete': {
|
||||||
|
onDelete(row);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ import { h, nextTick, watch } from 'vue';
|
||||||
import { useDemo03CourseGridEditColumns } from '../data';
|
import { useDemo03CourseGridEditColumns } from '../data';
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
studentId?: any; // 学生编号(主表的关联字段)
|
studentId?: number; // 学生编号(主表的关联字段)
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
/** 表格操作按钮的回调函数 */
|
/** 表格操作按钮的回调函数 */
|
||||||
|
@ -26,7 +26,7 @@ function onActionClick({ code, row }: OnActionClickParams<Demo03StudentApi.Demo0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [Demo03CourseGrid, demo03CourseGridApi] = useVbenVxeGrid({
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
gridOptions: {
|
gridOptions: {
|
||||||
columns: useDemo03CourseGridEditColumns(onActionClick),
|
columns: useDemo03CourseGridEditColumns(onActionClick),
|
||||||
border: true,
|
border: true,
|
||||||
|
@ -45,34 +45,25 @@ const [Demo03CourseGrid, demo03CourseGridApi] = useVbenVxeGrid({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
/** 删除学生课程 */
|
/** 添加学生课程 */
|
||||||
const onDelete = async (row: Demo03StudentApi.Demo03Course) => {
|
const onAdd = async () => {
|
||||||
await demo03CourseGridApi.grid.remove(row);
|
await gridApi.grid.insertAt({} as Demo03StudentApi.Demo03Course, -1);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 添加学生课程 */
|
/** 删除学生课程 */
|
||||||
const handleAdd = async () => {
|
const onDelete = async (row: Demo03StudentApi.Demo03Course) => {
|
||||||
await demo03CourseGridApi.grid.insertAt({} as Demo03StudentApi.Demo03Course, -1);
|
await gridApi.grid.remove(row);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 提供获取表格数据的方法供父组件调用 */
|
/** 提供获取表格数据的方法供父组件调用 */
|
||||||
defineExpose({
|
defineExpose({
|
||||||
getData: (): Demo03StudentApi.Demo03Course[] => {
|
getData: (): Demo03StudentApi.Demo03Course[] => {
|
||||||
// 获取当前数据,但排除已删除的记录
|
const data = gridApi.grid.getData() as Demo03StudentApi.Demo03Course[];
|
||||||
const allData = demo03CourseGridApi.grid.getData();
|
const removeRecords = gridApi.grid.getRemoveRecords() as Demo03StudentApi.Demo03Course[];
|
||||||
const removedData = demo03CourseGridApi.grid.getRemoveRecords();
|
const insertRecords = gridApi.grid.getInsertRecords() as Demo03StudentApi.Demo03Course[];
|
||||||
const removedIds = new Set(removedData.map((row) => row.id));
|
return data
|
||||||
|
.filter((row) => !removeRecords.some((removed) => removed.id === row.id))
|
||||||
// 过滤掉已删除的记录
|
.concat(insertRecords.map((row: any) => ({ ...row, id: undefined })));
|
||||||
const currentData = allData.filter((row) => !removedIds.has(row.id));
|
|
||||||
|
|
||||||
// 获取新插入的记录并移除id
|
|
||||||
const insertedData = demo03CourseGridApi.grid.getInsertRecords().map((row) => {
|
|
||||||
delete row.id;
|
|
||||||
return row;
|
|
||||||
});
|
|
||||||
|
|
||||||
return [...currentData, ...insertedData];
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -83,25 +74,24 @@ watch(
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await nextTick();
|
await nextTick();
|
||||||
await demo03CourseGridApi.grid.loadData(await getDemo03CourseListByStudentId(props.studentId!));
|
await gridApi.grid.loadData(await getDemo03CourseListByStudentId(props.studentId!));
|
||||||
},
|
},
|
||||||
{ immediate: true },
|
{ immediate: true },
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Demo03CourseGrid class="mx-4">
|
<Grid class="mx-4">
|
||||||
<template #name="{ row }">
|
<template #name="{ row }">
|
||||||
<Input v-model:value="row.name" />
|
<Input v-model:value="row.name" />
|
||||||
</template>
|
</template>
|
||||||
<template #score="{ row }">
|
<template #score="{ row }">
|
||||||
<Input v-model:value="row.score" />
|
<Input v-model:value="row.score" />
|
||||||
</template>
|
</template>
|
||||||
</Demo03CourseGrid>
|
</Grid>
|
||||||
<div class="flex justify-center">
|
<div class="-mt-4 flex justify-center">
|
||||||
<Button :icon="h(Plus)" type="primary" ghost @click="handleAdd" v-access:code="['infra:demo03-student:create']">
|
<Button :icon="h(Plus)" type="primary" ghost @click="onAdd" v-access:code="['infra:demo03-student:create']">
|
||||||
{{ $t('ui.actionTitle.create', ['学生课程']) }}
|
{{ $t('ui.actionTitle.create', ['学生课程']) }}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
|
@ -1,15 +1,15 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { useVbenForm } from '#/adapter/form';
|
import { useVbenForm } from '#/adapter/form';
|
||||||
import { getDemo03GradeByStudentId } from '#/api/infra/demo/demo03/normal';
|
import { getDemo03GradeByStudentId } from '#/api/infra/demo/demo03/normal';
|
||||||
import { watch } from 'vue';
|
import { nextTick, watch } from 'vue';
|
||||||
|
|
||||||
import { useDemo03GradeFormSchema } from '../data';
|
import { useDemo03GradeFormSchema } from '../data';
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
studentId?: any; // 学生编号(主表的关联字段)
|
studentId?: number; // 学生编号(主表的关联字段)
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
const [Demo03GradeForm, demo03GradeFormApi] = useVbenForm({
|
const [Form, formApi] = useVbenForm({
|
||||||
layout: 'horizontal',
|
layout: 'horizontal',
|
||||||
schema: useDemo03GradeFormSchema(),
|
schema: useDemo03GradeFormSchema(),
|
||||||
showDefaultActions: false,
|
showDefaultActions: false,
|
||||||
|
@ -18,10 +18,10 @@ const [Demo03GradeForm, demo03GradeFormApi] = useVbenForm({
|
||||||
/** 暴露出表单校验方法和表单值获取方法 */
|
/** 暴露出表单校验方法和表单值获取方法 */
|
||||||
defineExpose({
|
defineExpose({
|
||||||
validate: async () => {
|
validate: async () => {
|
||||||
const { valid } = await demo03GradeFormApi.validate();
|
const { valid } = await formApi.validate();
|
||||||
return valid;
|
return valid;
|
||||||
},
|
},
|
||||||
getValues: demo03GradeFormApi.getValues,
|
getValues: formApi.getValues,
|
||||||
});
|
});
|
||||||
|
|
||||||
/** 监听主表的关联字段的变化,加载对应的子表数据 */
|
/** 监听主表的关联字段的变化,加载对应的子表数据 */
|
||||||
|
@ -31,13 +31,13 @@ watch(
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
await nextTick();
|
||||||
await demo03GradeFormApi.setValues(await getDemo03GradeByStudentId(props.studentId!));
|
await formApi.setValues(await getDemo03GradeByStudentId(props.studentId!));
|
||||||
},
|
},
|
||||||
{ immediate: true },
|
{ immediate: true },
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Demo03GradeForm class="mx-4" />
|
<Form class="mx-4" />
|
||||||
</template>
|
</template>
|
|
@ -10,8 +10,8 @@ import { $t } from '#/locales';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
import { useFormSchema } from '../data';
|
import { useFormSchema } from '../data';
|
||||||
import Demo03CourseForm from './Demo03CourseForm.vue';
|
import Demo03CourseForm from './demo03-course-form.vue';
|
||||||
import Demo03GradeForm from './Demo03GradeForm.vue';
|
import Demo03GradeForm from './demo03-grade-form.vue';
|
||||||
|
|
||||||
const emit = defineEmits(['success']);
|
const emit = defineEmits(['success']);
|
||||||
const formData = ref<Demo03StudentApi.Demo03Student>();
|
const formData = ref<Demo03StudentApi.Demo03Student>();
|
||||||
|
@ -72,9 +72,7 @@ const [Modal, modalApi] = useVbenModal({
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.id) {
|
if (data.id) {
|
||||||
// 编辑
|
|
||||||
modalApi.lock();
|
modalApi.lock();
|
||||||
try {
|
try {
|
||||||
data = await getDemo03Student(data.id);
|
data = await getDemo03Student(data.id);
|
||||||
|
|
5222
pnpm-lock.yaml
5222
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue