<script lang="ts" setup>
|
import type { StudentApi } from '#/api/infra/demo';
|
|
import { computed, ref, h, onMounted, watch, nextTick } from 'vue';
|
|
import { DICT_TYPE } from '@vben/constants';
|
import { getDictOptions } from '@vben/hooks';
|
|
import { message, Tabs, Form, Input, Textarea, Button, Select, RadioGroup, Radio, CheckboxGroup, Checkbox, DatePicker } from 'ant-design-vue';
|
import { $t } from '#/locales';
|
|
import type { VxeTableInstance } from '#/adapter/vxe-table';
|
import { Plus } from "@vben/icons";
|
import { VxeColumn, VxeTable } from '#/adapter/vxe-table';
|
import { ImageUpload, FileUpload } from "#/components/upload";
|
import { getStudentContactListByStudentId } from '#/api/infra/demo';
|
|
const props = defineProps<{
|
studentId?: number // 学生编号(主表的关联字段)
|
}>()
|
|
const list = ref<StudentApi.StudentContact[]>([]) // 列表的数据
|
const tableRef = ref<VxeTableInstance>();
|
|
/** 添加学生联系人 */
|
async function handleAdd() {
|
await tableRef.value?.insertAt({} as StudentApi.StudentContact, -1);
|
}
|
|
/** 删除学生联系人 */
|
async function handleDelete(row: StudentApi.StudentContact) {
|
await tableRef.value?.remove(row);
|
}
|
|
/** 提供获取表格数据的方法供父组件调用 */
|
defineExpose({
|
getData: (): StudentApi.StudentContact[] => {
|
const data = list.value as StudentApi.StudentContact[];
|
const removeRecords = tableRef.value?.getRemoveRecords() as StudentApi.StudentContact[];
|
const insertRecords = tableRef.value?.getInsertRecords() as StudentApi.StudentContact[];
|
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;
|
}
|
list.value = await getStudentContactListByStudentId(props.studentId!);
|
},
|
{ immediate: true },
|
);
|
</script>
|
|
<template>
|
<VxeTable ref="tableRef" :data="list" show-overflow class="mx-4">
|
<VxeColumn field="name" title="名字" align="center">
|
<template #default="{ row }">
|
<Input v-model:value="row.name" />
|
</template>
|
</VxeColumn>
|
<VxeColumn field="description" title="简介" align="center">
|
<template #default="{ row }">
|
<Textarea v-model:value="row.description" />
|
</template>
|
</VxeColumn>
|
<VxeColumn field="birthday" title="出生日期" align="center">
|
<template #default="{ row }">
|
<DatePicker
|
v-model:value="row.birthday"
|
:showTime="true"
|
format="YYYY-MM-DD HH:mm:ss"
|
valueFormat='x'
|
/>
|
</template>
|
</VxeColumn>
|
<VxeColumn field="sex" title="性别" align="center">
|
<template #default="{ row }">
|
<Select v-model:value="row.sex" placeholder="请选择性别">
|
<Select.Option
|
v-for="dict in getDictOptions(DICT_TYPE.$dictType.toUpperCase(), 'number')"
|
:key="dict.value"
|
:value="dict.value"
|
>
|
{{ dict.label }}
|
</Select.Option>
|
</Select>
|
</template>
|
</VxeColumn>
|
<VxeColumn field="enabled" title="是否有效" align="center">
|
<template #default="{ row }">
|
<RadioGroup v-model:value="row.enabled">
|
<Radio
|
v-for="dict in getDictOptions(DICT_TYPE.$dictType.toUpperCase(), 'boolean')"
|
:key="dict.value"
|
:value="dict.value"
|
>
|
{{ dict.label }}
|
</Radio>
|
</RadioGroup>
|
</template>
|
</VxeColumn>
|
<VxeColumn field="avatar" title="头像" align="center">
|
<template #default="{ row }">
|
<ImageUpload v-model:value="row.avatar" />
|
</template>
|
</VxeColumn>
|
<VxeColumn field="video" title="附件" align="center">
|
<template #default="{ row }">
|
<FileUpload v-model:value="row.video" />
|
</template>
|
</VxeColumn>
|
<VxeColumn field="memo" title="备注" align="center">
|
<template #default="{ row }">
|
<Textarea v-model:value="row.memo" />
|
</template>
|
</VxeColumn>
|
<VxeColumn field="operation" title="操作" align="center">
|
<template #default="{ row }">
|
<Button
|
size="small"
|
type="link"
|
danger
|
@click="handleDelete(row)"
|
v-access:code="['infra:student:delete']"
|
>
|
{{ $t('ui.actionTitle.delete') }}
|
</Button>
|
</template>
|
</VxeColumn>
|
</VxeTable>
|
<div class="flex justify-center mt-4">
|
<Button :icon="h(Plus)" type="primary" ghost @click="handleAdd" v-access:code="['infra:student:create']">
|
{{ $t('ui.actionTitle.create', ['学生联系人']) }}
|
</Button>
|
</div>
|
</template>
|