review:主子表的示例代码
parent
7622e9a666
commit
4ccda9a5a1
|
@ -10,6 +10,7 @@ export namespace Demo03StudentApi {
|
|||
name?: string; // 名字
|
||||
score?: number; // 分数
|
||||
}
|
||||
// TODO @puhui999:要不要每个 interface 之间,有个空行
|
||||
/** 学生班级信息 */
|
||||
export interface Demo03Grade {
|
||||
id: number; // 编号
|
||||
|
@ -60,6 +61,7 @@ export function exportDemo03Student(params: any) {
|
|||
}
|
||||
|
||||
// ==================== 子表(学生课程) ====================
|
||||
// TODO @puhui999:==================== 后面,加个空行,会更清晰一点
|
||||
/** 获得学生课程列表 */
|
||||
export function getDemo03CourseListByStudentId(studentId: number) {
|
||||
return requestClient.get<Demo03StudentApi.Demo03Course[]>(
|
||||
|
|
|
@ -67,6 +67,7 @@ function onActionClick({ code, row }: OnActionClickParams<Demo01ContactApi.Demo0
|
|||
onDelete(row);
|
||||
break;
|
||||
}
|
||||
// TODO @puhui999:edit 放在 delete 前面哈。好理解一点,修改 => 删除
|
||||
case 'edit': {
|
||||
onEdit(row);
|
||||
break;
|
||||
|
|
|
@ -57,7 +57,7 @@ const [Modal, modalApi] = useVbenModal({
|
|||
}
|
||||
|
||||
if (data.id) {
|
||||
// 编辑
|
||||
// 编辑 TODO @puhui999:1)这里注释,“编辑”去掉;2)data.id 上面的空行去掉;
|
||||
modalApi.lock();
|
||||
try {
|
||||
data = await getDemo01Contact(data.id);
|
||||
|
|
|
@ -180,7 +180,7 @@ export function useGridColumns(
|
|||
}
|
||||
|
||||
// ==================== 子表(学生课程) ====================
|
||||
/** 新增/修改列表的字段 */
|
||||
/** 新增/修改列表的字段 */ // TODO @puhui999: ==== 下面空一行;
|
||||
export function useDemo03CourseGridEditColumns(
|
||||
onActionClick?: OnActionClickFn<Demo03StudentApi.Demo03Course>,
|
||||
): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
|
||||
|
@ -222,7 +222,7 @@ export function useDemo03CourseGridEditColumns(
|
|||
},
|
||||
];
|
||||
}
|
||||
/** 列表的字段 */
|
||||
/** 列表的字段 */ // TODO @puhui999:这里空一行;
|
||||
export function useDemo03CourseGridColumns(): VxeTableGridOptions<Demo03StudentApi.Demo03Course>['columns'] {
|
||||
return [
|
||||
{
|
||||
|
@ -253,7 +253,7 @@ export function useDemo03CourseGridColumns(): VxeTableGridOptions<Demo03StudentA
|
|||
},
|
||||
];
|
||||
}
|
||||
// ==================== 子表(学生班级) ====================
|
||||
// ==================== 子表(学生班级) ==================== // TODO @puhui999: ==== 前后都空一行;
|
||||
/** 新增/修改的表单 */
|
||||
export function useDemo03GradeFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
|
|
|
@ -119,6 +119,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
|||
<Grid table-title="学生列表">
|
||||
<template #expand_content="{ row }">
|
||||
<!-- 子表的表单 -->
|
||||
<!-- TODO @puhui999:【样式优化】1)Tabs 和箭头对齐;2)子 Table 也和箭头对齐 -->
|
||||
<Tabs v-model:active-key="subTabsName">
|
||||
<Tabs.TabPane key="demo03Course" tab="学生课程" force-render>
|
||||
<Demo03CourseList :student-id="row?.id" />
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<script lang="ts" setup>
|
||||
// TODO @puhui999:命名使用小写 + 中划线
|
||||
import type { OnActionClickParams } from '#/adapter/vxe-table';
|
||||
import type { Demo03StudentApi } from '#/api/infra/demo/demo03/inner';
|
||||
|
||||
|
@ -13,7 +14,7 @@ import { h, nextTick, watch } from 'vue';
|
|||
import { useDemo03CourseGridEditColumns } from '../data';
|
||||
|
||||
const props = defineProps<{
|
||||
studentId?: any; // 学生编号(主表的关联字段)
|
||||
studentId?: any; // 学生编号(主表的关联字段) TODO @puhui999:类型定义,应该是 number?
|
||||
}>();
|
||||
|
||||
/** 表格操作按钮的回调函数 */
|
||||
|
@ -26,6 +27,7 @@ function onActionClick({ code, row }: OnActionClickParams<Demo03StudentApi.Demo0
|
|||
}
|
||||
}
|
||||
|
||||
// TODO @puhui999:对于当前来说,就 Grid,gridApi,更简洁一点?
|
||||
const [Demo03CourseGrid, demo03CourseGridApi] = useVbenVxeGrid({
|
||||
gridOptions: {
|
||||
columns: useDemo03CourseGridEditColumns(onActionClick),
|
||||
|
@ -51,6 +53,7 @@ const onDelete = async (row: Demo03StudentApi.Demo03Course) => {
|
|||
};
|
||||
|
||||
/** 添加学生课程 */
|
||||
// TODO @puhui999:要不改成 onAdd?风格一致?;然后 add 放 delete 前面;
|
||||
const handleAdd = async () => {
|
||||
await demo03CourseGridApi.grid.insertAt({} as Demo03StudentApi.Demo03Course, -1);
|
||||
};
|
||||
|
@ -58,21 +61,13 @@ const handleAdd = async () => {
|
|||
/** 提供获取表格数据的方法供父组件调用 */
|
||||
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];
|
||||
// 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[];
|
||||
return data
|
||||
.filter(row => !removeRecords.some(removed => removed.id === row.id))
|
||||
.concat(insertRecords.map(row => ({ ...row, id: undefined })));
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -84,7 +79,7 @@ watch(
|
|||
return;
|
||||
}
|
||||
|
||||
await nextTick();
|
||||
await nextTick(); // TODO @puhui999:上面的空行去掉
|
||||
await demo03CourseGridApi.grid.loadData(await getDemo03CourseListByStudentId(props.studentId!));
|
||||
},
|
||||
);
|
||||
|
@ -99,7 +94,8 @@ watch(
|
|||
<Input v-model:value="row.score" />
|
||||
</template>
|
||||
</Demo03CourseGrid>
|
||||
<div class="flex justify-center">
|
||||
<!-- 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']">
|
||||
{{ $t('ui.actionTitle.create', ['学生课程']) }}
|
||||
</Button>
|
||||
|
|
|
@ -9,7 +9,7 @@ import { nextTick, watch } from 'vue';
|
|||
import { useDemo03CourseGridColumns } from '../data';
|
||||
|
||||
const props = defineProps<{
|
||||
studentId?: any; // 学生编号(主表的关联字段)
|
||||
studentId?: any; // 学生编号(主表的关联字段) TODO @puhui999:类型定义,应该是 number?
|
||||
}>();
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
|
@ -42,7 +42,7 @@ watch(
|
|||
return;
|
||||
}
|
||||
|
||||
await nextTick();
|
||||
await nextTick(); // TODO @puhui999:上面空行去掉?
|
||||
await onRefresh();
|
||||
},
|
||||
{ immediate: true },
|
||||
|
@ -50,6 +50,7 @@ watch(
|
|||
</script>
|
||||
|
||||
<template>
|
||||
<!-- TODO @puhui999:这个边距可以调整下,还是“箭头”那相关的哈 -->
|
||||
<div class="mx-4">
|
||||
<Grid table-title="学生课程列表" />
|
||||
</div>
|
||||
|
|
|
@ -6,7 +6,7 @@ import { nextTick, watch } from 'vue';
|
|||
import { useDemo03GradeFormSchema } from '../data';
|
||||
|
||||
const props = defineProps<{
|
||||
studentId?: any; // 学生编号(主表的关联字段)
|
||||
studentId?: any; // 学生编号(主表的关联字段) TODO @puhui999:类型定义,应该是 number?
|
||||
}>();
|
||||
|
||||
const [Demo03GradeForm, demo03GradeFormApi] = useVbenForm({
|
||||
|
@ -32,7 +32,7 @@ watch(
|
|||
return;
|
||||
}
|
||||
|
||||
await nextTick();
|
||||
await nextTick(); // TODO @puhui999:上面的空行去掉
|
||||
await demo03GradeFormApi.setValues(await getDemo03GradeByStudentId(props.studentId!));
|
||||
},
|
||||
);
|
||||
|
|
|
@ -9,7 +9,7 @@ import { nextTick, watch } from 'vue';
|
|||
import { useDemo03GradeGridColumns } from '../data';
|
||||
|
||||
const props = defineProps<{
|
||||
studentId?: any; // 学生编号(主表的关联字段)
|
||||
studentId?: any; // 学生编号(主表的关联字段) // TODO @puhui999:上面的空行去掉
|
||||
}>();
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
|
@ -42,7 +42,7 @@ watch(
|
|||
return;
|
||||
}
|
||||
|
||||
await nextTick();
|
||||
await nextTick(); // TODO @puhui999:上面的空行去掉
|
||||
await onRefresh();
|
||||
},
|
||||
{ immediate: true },
|
||||
|
|
Loading…
Reference in New Issue