refactor: 重新生成主子表内嵌模式代码预览

pull/77/head
puhui999 2025-04-21 16:02:50 +08:00
parent 7ab66aa363
commit 0cb157a29c
8 changed files with 60 additions and 71 deletions

View File

@ -62,6 +62,7 @@ export function exportDemo03Student(params: any) {
}
// ==================== 子表(学生课程) ====================
/** 获得学生课程列表 */
export function getDemo03CourseListByStudentId(studentId: number) {
return requestClient.get<Demo03StudentApi.Demo03Course[]>(
@ -70,6 +71,7 @@ export function getDemo03CourseListByStudentId(studentId: number) {
}
// ==================== 子表(学生班级) ====================
/** 获得学生班级 */
export function getDemo03GradeByStudentId(studentId: number) {
return requestClient.get<Demo03StudentApi.Demo03Grade>(

View File

@ -56,10 +56,7 @@ export function useFormSchema(): VbenFormSchema[] {
fieldName: 'description',
label: '简介',
rules: 'required',
component: 'Textarea',
componentProps: {
placeholder: '请输入简介',
},
component: 'RichTextarea',
},
];
}
@ -180,7 +177,8 @@ export function useGridColumns(
}
// ==================== 子表(学生课程) ====================
/** 新增/修改列表的字段 */ // TODO @puhui999 ==== 下面空一行;
/** 新增/修改列表的字段 */
export function useDemo03CourseGridEditColumns(
onActionClick?: OnActionClickFn<Demo03StudentApi.Demo03Course>,
): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
@ -222,7 +220,8 @@ export function useDemo03CourseGridEditColumns(
},
];
}
/** 列表的字段 */ // TODO @puhui999这里空一行
/** 列表的字段 */
export function useDemo03CourseGridColumns(): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
return [
{
@ -253,7 +252,9 @@ export function useDemo03CourseGridColumns(): VxeTableGridOptions<Demo03StudentA
},
];
}
// ==================== 子表(学生班级) ==================== // TODO @puhui999 ==== 前后都空一行;
// ==================== 子表(学生班级) ====================
/** 新增/修改的表单 */
export function useDemo03GradeFormSchema(): VbenFormSchema[] {
return [
@ -285,6 +286,7 @@ export function useDemo03GradeFormSchema(): VbenFormSchema[] {
},
];
}
/** 列表的字段 */
export function useDemo03GradeGridColumns(): VxeTableGridOptions<Demo03StudentApi.Demo03Grade>['columns'] {
return [

View File

@ -2,8 +2,8 @@
import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { Demo03StudentApi } from '#/api/infra/demo/demo03/inner';
import Demo03CourseList from './modules/Demo03CourseList.vue';
import Demo03GradeList from './modules/Demo03GradeList.vue';
import Demo03CourseList from './modules/demo03-course-list.vue';
import Demo03GradeList from './modules/demo03-grade-list.vue';
import Form from './modules/form.vue';
import { Page, useVbenModal } from '@vben/common-ui';
import { Download, Plus } from '@vben/icons';
@ -30,12 +30,6 @@ function onRefresh() {
gridApi.reload();
}
/** 导出表格 */
async function onExport() {
const data = await exportDemo03Student(await gridApi.formApi.getValues());
downloadByData(data, '学生.xls');
}
/** 创建学生 */
function onCreate() {
formModalApi.setData({}).open();
@ -65,17 +59,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;
}
}
}
@ -119,8 +119,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
<Grid table-title="">
<template #expand_content="{ row }">
<!-- 子表的表单 -->
<!-- TODO @puhui999样式优化1Tabs 和箭头对齐2 Table 也和箭头对齐 -->
<Tabs v-model:active-key="subTabsName">
<Tabs v-model:active-key="subTabsName" class="mx-8">
<Tabs.TabPane key="demo03Course" tab="学生课程" force-render>
<Demo03CourseList :student-id="row?.id" />
</Tabs.TabPane>

View File

@ -1,5 +1,4 @@
<script lang="ts" setup>
// TODO @puhui999使 + 线
import type { OnActionClickParams } from '#/adapter/vxe-table';
import type { Demo03StudentApi } from '#/api/infra/demo/demo03/inner';
@ -14,7 +13,7 @@ import { h, nextTick, watch } from 'vue';
import { useDemo03CourseGridEditColumns } from '../data';
const props = defineProps<{
studentId?: any; // TODO @puhui999 number
studentId?: number; //
}>();
/** 表格操作按钮的回调函数 */
@ -27,8 +26,7 @@ function onActionClick({ code, row }: OnActionClickParams<Demo03StudentApi.Demo0
}
}
// TODO @puhui999 GridgridApi
const [Demo03CourseGrid, demo03CourseGridApi] = useVbenVxeGrid({
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions: {
columns: useDemo03CourseGridEditColumns(onActionClick),
border: true,
@ -47,27 +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);
};
/** 添加学生课程 */
// TODO @puhui999 onAdd add delete
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[] => {
// TODO @puhui999
const data = demo03CourseGridApi.grid.getData() as Demo03StudentApi.Demo03Course[];
const removeRecords = demo03CourseGridApi.grid.getRemoveRecords() as Demo03StudentApi.Demo03Course[];
const insertRecords = demo03CourseGridApi.grid.getInsertRecords() as Demo03StudentApi.Demo03Course[];
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 => ({ ...row, id: undefined })));
.filter((row) => !removeRecords.some((removed) => removed.id === row.id))
.concat(insertRecords.map((row: any) => ({ ...row, id: undefined })));
},
});
@ -78,25 +74,24 @@ watch(
if (!val) {
return;
}
await nextTick(); // TODO @puhui999
await demo03CourseGridApi.grid.loadData(await getDemo03CourseListByStudentId(props.studentId!));
await nextTick();
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>
<!-- TODO @puhui999-mt-4 把距离控制下哈 -->
<div class="flex justify-center -mt-4">
<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>

View File

@ -9,7 +9,7 @@ import { nextTick, watch } from 'vue';
import { useDemo03CourseGridColumns } from '../data';
const props = defineProps<{
studentId?: any; // TODO @puhui999 number
studentId?: number; //
}>();
const [Grid, gridApi] = useVbenVxeGrid({
@ -41,8 +41,7 @@ watch(
if (!val) {
return;
}
await nextTick(); // TODO @puhui999
await nextTick();
await onRefresh();
},
{ immediate: true },
@ -50,8 +49,5 @@ watch(
</script>
<template>
<!-- TODO @puhui999这个边距可以调整下还是箭头那相关的哈 -->
<div class="mx-4">
<Grid table-title="" />
</div>
<Grid table-title="" />
</template>

View File

@ -6,10 +6,10 @@ import { nextTick, watch } from 'vue';
import { useDemo03GradeFormSchema } from '../data';
const props = defineProps<{
studentId?: any; // TODO @puhui999 number
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 nextTick(); // TODO @puhui999
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>

View File

@ -9,7 +9,7 @@ import { nextTick, watch } from 'vue';
import { useDemo03GradeGridColumns } from '../data';
const props = defineProps<{
studentId?: any; // // TODO @puhui999
studentId?: number; //
}>();
const [Grid, gridApi] = useVbenVxeGrid({
@ -41,8 +41,7 @@ watch(
if (!val) {
return;
}
await nextTick(); // TODO @puhui999
await nextTick();
await onRefresh();
},
{ immediate: true },
@ -50,7 +49,5 @@ watch(
</script>
<template>
<div class="mx-4">
<Grid table-title="" />
</div>
<Grid table-title="" />
</template>

View File

@ -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);