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