108 分钟以前 5367b3b4d92588c4e76728e6bd4ad6aae0cbb967
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { Demo03StudentApi } from '#/api/infra/demo/demo03/normal';
 
import { nextTick, watch } from 'vue';
 
import { IconifyIcon } from '@vben/icons';
 
import { Button, Input } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getDemo03CourseListByStudentId } from '#/api/infra/demo/demo03/normal';
import { $t } from '#/locales';
 
import { useDemo03CourseGridEditColumns } from '../data';
 
const props = defineProps<{
  studentId?: number; // 学生编号(主表的关联字段)
}>();
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useDemo03CourseGridEditColumns(),
    border: true,
    showOverflow: true,
    autoResize: true,
    keepSource: true,
    rowConfig: {
      keyField: 'id',
    },
    pagerConfig: {
      enabled: false,
    },
    toolbarConfig: {
      enabled: false,
    },
  } as VxeTableGridOptions<Demo03StudentApi.Demo03Course>,
});
 
/** 添加学生课程 */
async function handleAdd() {
  await gridApi.grid.insertAt({} as Demo03StudentApi.Demo03Course, -1);
}
 
/** 删除学生课程 */
async function handleDelete(row: Demo03StudentApi.Demo03Course) {
  await gridApi.grid.remove(row);
}
 
/** 提供获取表格数据的方法供父组件调用 */
defineExpose({
  getData: (): 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),
      ),
      ...insertRecords.map((row: any) => ({ ...row, id: undefined })),
    ];
  },
});
 
/** 监听主表的关联字段的变化,加载对应的子表数据 */
watch(
  () => props.studentId,
  async (val) => {
    if (!val) {
      return;
    }
    await nextTick();
    await gridApi.grid.loadData(
      await getDemo03CourseListByStudentId(props.studentId!),
    );
  },
  { immediate: true },
);
</script>
 
<template>
  <Grid class="mx-4">
    <template #name="{ row }">
      <Input v-model:value="row.name" />
    </template>
    <template #score="{ row }">
      <Input v-model:value="row.score" />
    </template>
    <template #actions="{ row }">
      <TableAction
        :actions="[
          {
            label: $t('common.delete'),
            danger: true,
            type: 'link',
            icon: ACTION_ICON.DELETE,
            auth: ['infra:demo03-student:delete'],
            popConfirm: {
              title: $t('ui.actionMessage.deleteConfirm', [row.id]),
              confirm: handleDelete.bind(null, row),
            },
          },
        ]"
      />
    </template>
  </Grid>
  <div class="-mt-4 flex justify-center">
    <Button
      type="primary"
      ghost
      @click="handleAdd"
      v-access:code="['infra:demo03-student:create']"
    >
      <IconifyIcon icon="lucide:plus" />
      {{ $t('ui.actionTitle.create', ['学生课程']) }}
    </Button>
  </div>
</template>