liu
3 天以前 b337be5073334334962e873c1a980968ad2cf068
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ResearchApplicationApi } from '#/api/research/application';
 
import { nextTick, watch } from 'vue';
 
import { Button, Input, InputNumber, Select } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { $t } from '#/locales';
 
import { MEMBER_TYPE_OPTIONS, toSubmitMembers } from '../data';
 
const props = withDefaults(
  defineProps<{
    members?: ResearchApplicationApi.ApplicationMember[];
    disabled?: boolean;
  }>(),
  {
    members: () => [],
    disabled: false,
  },
);
 
const emit = defineEmits<{
  'update:members': [members: ResearchApplicationApi.ApplicationMember[]];
}>();
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: [
      {
        field: 'sortNo',
        title: '署名顺序',
        width: 110,
        slots: { default: 'sortNo' },
      },
      {
        field: 'memberType',
        title: '成员类型',
        width: 130,
        slots: { default: 'memberType' },
      },
      {
        field: 'name',
        title: '成员姓名',
        minWidth: 120,
        slots: { default: 'name' },
      },
      {
        field: 'workUnit',
        title: '所属部门',
        minWidth: 220,
        slots: { default: 'workUnit' },
      },
      {
        field: 'jobTitle',
        title: '职称',
        minWidth: 130,
        slots: { default: 'jobTitle' },
      },
      {
        title: '操作',
        width: 80,
        fixed: 'right',
        slots: { default: 'actions' },
      },
    ],
    data: [],
    minHeight: 200,
    autoResize: true,
    border: true,
    rowConfig: { keyField: 'id', isHover: true },
    pagerConfig: { enabled: false },
    toolbarConfig: { enabled: false },
  } as VxeTableGridOptions<ResearchApplicationApi.ApplicationMember>,
});
 
/** 读取当前全部成员行(提交形态:不含 id / applicationId) */
function collectMembers(): ResearchApplicationApi.ApplicationMember[] {
  const rows =
    (gridApi.grid.getData() as ResearchApplicationApi.ApplicationMember[]) ??
    [];
  const removed =
    (gridApi.grid.getRemoveRecords() as ResearchApplicationApi.ApplicationMember[]) ??
    [];
  const inserted =
    (gridApi.grid.getInsertRecords() as ResearchApplicationApi.ApplicationMember[]) ??
    [];
  const isRemoved = (row: ResearchApplicationApi.ApplicationMember) =>
    removed.some(
      (item) => item === row || (row.id != null && item.id === row.id),
    );
  return toSubmitMembers([
    ...rows.filter((row) => !isRemoved(row)),
    ...inserted,
  ]);
}
 
/** 同步成员数据给父组件 */
function recalc() {
  emit('update:members', collectMembers());
}
 
/** 添加成员行 */
async function handleAdd() {
  await gridApi.grid.insertAt(
    {
      memberType: '教师',
      sortNo: collectMembers().length + 1,
    } as unknown as ResearchApplicationApi.ApplicationMember,
    -1,
  );
  recalc();
}
 
/** 删除成员行 */
async function handleDelete(row: ResearchApplicationApi.ApplicationMember) {
  await gridApi.grid.remove(row);
  recalc();
}
 
/** 校验成员:姓名必填;填写电话时须为 11 位手机号 */
function validate() {
  const rows = collectMembers();
  for (let i = 0; i < rows.length; i++) {
    const row = rows[i];
    if (!row?.name || String(row.name).trim() === '') {
      throw new Error(`第 ${i + 1} 行成员:请填写成员姓名`);
    }
    const phone = row.contactPhone ? String(row.contactPhone).trim() : '';
    if (phone !== '' && !/^1[3-9]\d{9}$/.test(phone)) {
      throw new Error(
        `第 ${i + 1} 行成员 ${row.name}:联系电话格式不正确,请输入 11 位手机号`,
      );
    }
  }
}
 
defineExpose({ validate });
 
/**
 * 回显成员(单向:父 → 表格)。
 * watcher 内禁止回传 emit,避免 prop ↔ emit 死循环。
 */
watch(
  () => props.members,
  async (members) => {
    if (!members) {
      return;
    }
    await nextTick();
    await gridApi.grid.loadData([...members]);
  },
  { immediate: true },
);
</script>
 
<template>
  <Grid class="w-full">
    <template #sortNo="{ row }">
      <InputNumber
        v-model:value="row.sortNo"
        :disabled="disabled"
        :min="1"
        :precision="0"
        class="!w-full"
        @change="() => recalc()"
      />
    </template>
    <template #memberType="{ row }">
      <Select
        v-model:value="row.memberType"
        :disabled="disabled"
        :options="MEMBER_TYPE_OPTIONS"
        placeholder="请选择成员类型"
        allow-clear
        @change="() => recalc()"
      />
    </template>
    <template #name="{ row }">
      <Input
        v-model:value="row.name"
        :disabled="disabled"
        placeholder="请输入成员姓名"
        @change="() => recalc()"
      />
    </template>
    <template #workUnit="{ row }">
      <Input
        v-model:value="row.workUnit"
        :disabled="disabled"
        placeholder="请输入所属部门"
        @change="() => recalc()"
      />
    </template>
    <template #jobTitle="{ row }">
      <Input
        v-model:value="row.jobTitle"
        :disabled="disabled"
        placeholder="请输入职称"
        @change="() => recalc()"
      />
    </template>
    <template #actions="{ row }">
      <TableAction
        :actions="[
          {
            label: $t('common.delete'),
            danger: true,
            type: 'link',
            icon: ACTION_ICON.DELETE,
            disabled,
            onClick: handleDelete.bind(null, row),
          },
        ]"
      />
    </template>
  </Grid>
  <div v-if="!disabled" class="-mt-4 flex justify-center">
    <Button type="primary" ghost @click="handleAdd"> 添加成员 </Button>
  </div>
</template>