1
|
|
-<script lang="ts" setup>
|
2
|
|
- import { Button, Tabs, Badge, ButtonProps, Tag } from 'ant-design-vue';
|
3
|
|
- import { get, isFunction, set, uniqBy } from 'lodash-es';
|
4
|
|
- import { ExtractPropTypes, computed, unref, ref, nextTick, onMounted, watch } from 'vue';
|
5
|
|
- import { DynamicProps } from '/#/utils';
|
6
|
|
- import { BasicModal, useModal } from '/@/components/Modal';
|
7
|
|
- import { BasicTable, BasicTableProps, TableRowSelection, useTable } from '/@/components/Table';
|
8
|
|
- import { FETCH_SETTING } from '/@/components/Table/src/const';
|
9
|
|
- import { useDesign } from '/@/hooks/web/useDesign';
|
10
|
|
-
|
11
|
|
- interface Options extends Recordable {
|
12
|
|
- primaryKey?: string;
|
13
|
|
- disabled?: boolean;
|
14
|
|
- }
|
15
|
|
-
|
16
|
|
- enum Active {
|
17
|
|
- PENDING = 'pending',
|
18
|
|
- SELECTED = 'selected',
|
19
|
|
- }
|
20
|
|
-
|
21
|
|
- interface ActionType {
|
22
|
|
- setSelectedOptions: (options: Recordable[]) => void;
|
23
|
|
- }
|
24
|
|
-
|
25
|
|
- const emit = defineEmits(['change', 'update:value']);
|
26
|
|
-
|
27
|
|
- const props = withDefaults(
|
28
|
|
- defineProps<{
|
29
|
|
- value?: string[];
|
30
|
|
- labelField?: string;
|
31
|
|
- valueField?: string;
|
32
|
|
- primaryKey?: string;
|
33
|
|
- params?: Recordable;
|
34
|
|
- buttonName?: string;
|
35
|
|
- pendingTableProps?: BasicTableProps;
|
36
|
|
- selectedTableProps?: BasicTableProps;
|
37
|
|
- maxTagLength?: number;
|
38
|
|
- modalProps?: ExtractPropTypes<InstanceType<typeof BasicModal>['$props']>;
|
39
|
|
- buttonProps?: ExtractPropTypes<InstanceType<typeof Button>['$props']>;
|
40
|
|
- initSelectedOptions?: (actionType: ActionType) => Promise<Recordable[]>;
|
41
|
|
- transformValue?: (selectedRowKeys: string[], selectedRows: Options[]) => any[];
|
42
|
|
- onValueChange?: (selectedRowkeys: string[]) => any[];
|
43
|
|
- onRemoveAfter?: (actionType: ActionType) => Promise<any>;
|
44
|
|
- onSelectedAfter?: (actionType: ActionType) => Promise<any>;
|
45
|
|
- disabled?: any;
|
46
|
|
- }>(),
|
47
|
|
- {
|
48
|
|
- buttonName: '选择设备',
|
49
|
|
- primaryKey: 'id',
|
50
|
|
- maxTagLength: 2,
|
51
|
|
- labelField: 'label',
|
52
|
|
- valueField: 'value',
|
53
|
|
- disabled: false,
|
54
|
|
- }
|
55
|
|
- );
|
56
|
|
-
|
57
|
|
- const { prefixCls } = useDesign('transfer-table-modal');
|
58
|
|
-
|
59
|
|
- const activeKey = ref<Active>(Active.PENDING);
|
60
|
|
-
|
61
|
|
- const selectedRows = ref<Options[]>([]);
|
62
|
|
-
|
63
|
|
- const selectedRowKeys = ref<string[]>(props.value || []);
|
64
|
|
-
|
65
|
|
- const pendingOptions = ref<Options[]>([]);
|
66
|
|
-
|
67
|
|
- const selectedConfirmQueue = ref<Options[]>([]);
|
68
|
|
-
|
69
|
|
- const pendingConfirmQueue = ref<Options[]>([]);
|
70
|
|
-
|
71
|
|
- const selectedTotal = ref(0);
|
72
|
|
-
|
73
|
|
- const pendingTotal = ref(0);
|
74
|
|
-
|
75
|
|
- const getFetchSetting = computed(() => {
|
76
|
|
- const { pendingTableProps } = props;
|
77
|
|
- return pendingTableProps?.fetchSetting || FETCH_SETTING;
|
78
|
|
- });
|
79
|
|
-
|
80
|
|
- const getPendingRowSelection = computed<TableRowSelection>(() => {
|
81
|
|
- const rowKeys = unref(selectedRowKeys);
|
82
|
|
- return {
|
83
|
|
- type: 'checkbox',
|
84
|
|
- getCheckboxProps: (record: Recordable) => {
|
85
|
|
- const { primaryKey } = props;
|
86
|
|
- return {
|
87
|
|
- ...record,
|
88
|
|
- disabled: rowKeys.includes(record[primaryKey]),
|
89
|
|
- };
|
90
|
|
- },
|
91
|
|
- onSelect: (_record: Recordable, _selected: boolean, selectedRows: Object[]) => {
|
92
|
|
- pendingConfirmQueue.value = selectedRows;
|
93
|
|
- },
|
94
|
|
- onSelectAll: (_selected: boolean, selectedRows: Recordable[]) => {
|
95
|
|
- pendingConfirmQueue.value = selectedRows;
|
96
|
|
- },
|
97
|
|
- };
|
98
|
|
- });
|
99
|
|
-
|
100
|
|
- const getPendingTableBindProps = computed<Partial<DynamicProps<BasicTableProps>>>(() => {
|
101
|
|
- const { pendingTableProps, primaryKey } = props;
|
102
|
|
- return {
|
103
|
|
- ...pendingTableProps,
|
104
|
|
- rowKey: primaryKey,
|
105
|
|
- api: handlePendingApiIntercept,
|
106
|
|
- clickToRowSelect: false,
|
107
|
|
- rowSelection: getPendingRowSelection,
|
108
|
|
- };
|
109
|
|
- });
|
110
|
|
-
|
111
|
|
- const getSelectedTableBindProps = computed<Partial<DynamicProps<BasicTableProps>>>(() => {
|
112
|
|
- const { selectedTableProps, primaryKey } = props;
|
113
|
|
- return {
|
114
|
|
- ...selectedTableProps,
|
115
|
|
- dataSource: selectedRows,
|
116
|
|
- clickToRowSelect: false,
|
117
|
|
- rowKey: primaryKey,
|
118
|
|
- api: selectedTableProps!.api ? handleSelectedApiIntercept : undefined,
|
119
|
|
- rowSelection: {
|
120
|
|
- type: 'checkbox',
|
121
|
|
- onSelect: (_record: Recordable, _selected: boolean, selectedRows: Object[]) => {
|
122
|
|
- selectedConfirmQueue.value = selectedRows;
|
123
|
|
- },
|
124
|
|
- onSelectAll: (_selected: boolean, selectedRows: Recordable[]) => {
|
125
|
|
- selectedConfirmQueue.value = selectedRows;
|
126
|
|
- },
|
127
|
|
- },
|
128
|
|
- };
|
129
|
|
- });
|
130
|
|
-
|
131
|
|
- const getModalBindProps = computed(() => {
|
132
|
|
- const { modalProps = {} } = props;
|
133
|
|
- return {
|
134
|
|
- width: '60%',
|
135
|
|
- title: '穿梭表格',
|
136
|
|
- wrapClassName: prefixCls,
|
137
|
|
- ...modalProps,
|
138
|
|
- showOkBtn: false,
|
139
|
|
- };
|
140
|
|
- });
|
141
|
|
-
|
142
|
|
- const getBindButtonProps = computed<ButtonProps>(() => {
|
143
|
|
- const { buttonProps = {} } = props;
|
144
|
|
- return {
|
145
|
|
- type: 'link',
|
146
|
|
- ...buttonProps,
|
147
|
|
- };
|
148
|
|
- });
|
149
|
|
-
|
150
|
|
- const getShowTagOptions = computed(() => {
|
151
|
|
- const { maxTagLength } = props;
|
152
|
|
- return unref(selectedRows).slice(0, maxTagLength);
|
153
|
|
- });
|
154
|
|
-
|
155
|
|
- const getSurplusOptionsLength = computed(() => {
|
156
|
|
- const { maxTagLength } = props;
|
157
|
|
- const surplusValue = unref(selectedRows).length - maxTagLength;
|
158
|
|
- return surplusValue < 0 ? 0 : surplusValue;
|
159
|
|
- });
|
160
|
|
-
|
161
|
|
- const [registerModal, { openModal }] = useModal();
|
162
|
|
-
|
163
|
|
- const [
|
164
|
|
- regsterPendingTable,
|
165
|
|
- {
|
166
|
|
- getSelectRows: getPendingSelectRows,
|
167
|
|
- getSelectRowKeys: getPendingSelectRowKeys,
|
168
|
|
- reload: reloadPending,
|
169
|
|
- clearSelectedRowKeys: clearPendingSelectedRowKeys,
|
170
|
|
- },
|
171
|
|
- ] = useTable(unref(getPendingTableBindProps));
|
172
|
|
-
|
173
|
|
- const [
|
174
|
|
- registerSelectedTable,
|
175
|
|
- { getSelectRowKeys, setProps, clearSelectedRowKeys, reload: reloadSelected },
|
176
|
|
- ] = useTable(unref(getSelectedTableBindProps));
|
177
|
|
-
|
178
|
|
- async function handlePendingApiIntercept(params?: Recordable) {
|
179
|
|
- try {
|
180
|
|
- const { api } = props.pendingTableProps || {};
|
181
|
|
- if (api && isFunction(api)) {
|
182
|
|
- let options = await api(params);
|
183
|
|
- pendingOptions.value = options;
|
184
|
|
- const { totalField, listField } = unref(getFetchSetting);
|
185
|
|
- const total = get(options, totalField!);
|
186
|
|
- if (unref(selectedTotal) + unref(pendingTotal) !== total) {
|
187
|
|
- pendingTotal.value = total;
|
188
|
|
- }
|
189
|
|
- let list: Recordable[] = get(options, listField!);
|
190
|
|
- list = getSelectedRows(list);
|
191
|
|
- options = set(options, listField!, list);
|
192
|
|
- return options;
|
193
|
|
- }
|
194
|
|
- } catch (error) {
|
195
|
|
- console.error(error);
|
196
|
|
- return [];
|
197
|
|
- }
|
198
|
|
- return [];
|
199
|
|
- }
|
200
|
|
-
|
201
|
|
- async function handleSelectedApiIntercept(params?: Recordable) {
|
202
|
|
- try {
|
203
|
|
- const { api } = props.selectedTableProps || {};
|
204
|
|
- if (api && isFunction(api)) {
|
205
|
|
- let options = await api(params);
|
206
|
|
- pendingOptions.value = options;
|
207
|
|
- const { totalField, listField } = unref(getFetchSetting);
|
208
|
|
- selectedTotal.value = get(options, totalField!);
|
209
|
|
- let list: Recordable[] = get(options, listField!);
|
210
|
|
- list = getSelectedRows(list);
|
211
|
|
- options = set(options, listField!, list);
|
212
|
|
- return options;
|
213
|
|
- }
|
214
|
|
- } catch (error) {
|
215
|
|
- console.error(error);
|
216
|
|
- return [];
|
217
|
|
- }
|
218
|
|
- return [];
|
219
|
|
- }
|
220
|
|
-
|
221
|
|
- const handleOpenModal = async () => {
|
222
|
|
- const { disabled } = props;
|
223
|
|
- if (disabled) return;
|
224
|
|
- openModal(true);
|
225
|
|
- await nextTick();
|
226
|
|
- if (props.value && !props.value.length) {
|
227
|
|
- activeKey.value = Active.PENDING;
|
228
|
|
- reloadPending();
|
229
|
|
- }
|
230
|
|
- };
|
231
|
|
-
|
232
|
|
- const handleTriggerEmit = (selectedRowKeys: string[], selectedRows: Options[]) => {
|
233
|
|
- const { transformValue } = props;
|
234
|
|
- let value = selectedRowKeys;
|
235
|
|
- if (transformValue && isFunction(transformValue)) {
|
236
|
|
- value = transformValue(selectedRowKeys, selectedRows);
|
237
|
|
- }
|
238
|
|
- emit('change', unref(selectedRowKeys), unref(selectedRows));
|
239
|
|
- emit('update:value', unref(value));
|
240
|
|
- };
|
241
|
|
-
|
242
|
|
- const handleSelected = async () => {
|
243
|
|
- const { onSelectedAfter } = props;
|
244
|
|
- const currentPageSelectRows = getPendingSelectRows();
|
245
|
|
- const currentPageSelectRowKeys = getPendingSelectRowKeys();
|
246
|
|
- const { primaryKey } = props;
|
247
|
|
- selectedRows.value = uniqBy([...unref(selectedRows), ...currentPageSelectRows], primaryKey);
|
248
|
|
- selectedRowKeys.value = [...new Set([...unref(selectedRowKeys), ...currentPageSelectRowKeys])];
|
249
|
|
- pendingConfirmQueue.value = [];
|
250
|
|
- // selectedTotal.value = unref(selectedRowKeys).length;
|
251
|
|
- pendingTotal.value = unref(pendingTotal) - currentPageSelectRows.length;
|
252
|
|
- selectedTotal.value = unref(selectedTotal) + currentPageSelectRows.length;
|
253
|
|
-
|
254
|
|
- clearPendingSelectedRowKeys();
|
255
|
|
- handleTriggerEmit(unref(selectedRowKeys), unref(selectedRows));
|
256
|
|
-
|
257
|
|
- if (onSelectedAfter && isFunction(onSelectedAfter)) {
|
258
|
|
- await onSelectedAfter(actionType);
|
259
|
|
- }
|
260
|
|
- reloadPending();
|
261
|
|
- };
|
262
|
|
-
|
263
|
|
- const handleRemoveSelected = async () => {
|
264
|
|
- const { onRemoveAfter } = props;
|
265
|
|
- const removeRowKeys = getSelectRowKeys();
|
266
|
|
- selectedRowKeys.value = unref(selectedRowKeys).filter((key) => !removeRowKeys.includes(key));
|
267
|
|
- selectedRows.value = unref(selectedRows).filter((item) => {
|
268
|
|
- const { primaryKey } = props;
|
269
|
|
- return unref(selectedRowKeys).includes(item[primaryKey]);
|
270
|
|
- });
|
271
|
|
- pendingTotal.value = unref(pendingTotal) + removeRowKeys.length;
|
272
|
|
- selectedTotal.value = unref(selectedTotal) - removeRowKeys.length;
|
273
|
|
-
|
274
|
|
- clearSelectedRowKeys();
|
275
|
|
- selectedConfirmQueue.value = [];
|
276
|
|
- setProps({ dataSource: unref(selectedRows) });
|
277
|
|
- handleTriggerEmit(unref(selectedRowKeys), unref(selectedRows));
|
278
|
|
-
|
279
|
|
- if (onRemoveAfter && isFunction(onRemoveAfter)) {
|
280
|
|
- await onRemoveAfter(actionType);
|
281
|
|
- }
|
282
|
|
- };
|
283
|
|
-
|
284
|
|
- const actionType = {
|
285
|
|
- setSelectedOptions,
|
286
|
|
- setSelectedTotal,
|
287
|
|
- reloadPending,
|
288
|
|
- reloadSelected,
|
289
|
|
- };
|
290
|
|
-
|
291
|
|
- const getSelectedRows = (options: Recordable[]) => {
|
292
|
|
- const { labelField, valueField } = props;
|
293
|
|
- return options.map((item) => ({ ...item, label: item[labelField], value: item[valueField] }));
|
294
|
|
- };
|
295
|
|
-
|
296
|
|
- const getSelectedKeys = (options: Recordable[]) => {
|
297
|
|
- const { primaryKey } = props;
|
298
|
|
- return options.map((item) => item[primaryKey]);
|
299
|
|
- };
|
300
|
|
-
|
301
|
|
- function setSelectedOptions(options: Recordable[]) {
|
302
|
|
- selectedRows.value = getSelectedRows(options);
|
303
|
|
- selectedRowKeys.value = getSelectedKeys(options);
|
304
|
|
- }
|
305
|
|
-
|
306
|
|
- function setSelectedTotal(number: number) {
|
307
|
|
- selectedTotal.value = number;
|
308
|
|
- }
|
309
|
|
-
|
310
|
|
- const handleCheckoutPanel = async (keys: Active) => {
|
311
|
|
- await nextTick();
|
312
|
|
- if (keys === Active.PENDING) {
|
313
|
|
- reloadPending();
|
314
|
|
- } else {
|
315
|
|
- reloadSelected();
|
316
|
|
- setProps({
|
317
|
|
- dataSource: unref(selectedRows),
|
318
|
|
- });
|
319
|
|
- }
|
320
|
|
- };
|
321
|
|
-
|
322
|
|
- watch(
|
323
|
|
- () => props.value,
|
324
|
|
- () => {
|
325
|
|
- if (props.value && !props.value.length) {
|
326
|
|
- selectedRowKeys.value = [];
|
327
|
|
- selectedRows.value = [];
|
328
|
|
- // pendingTotal.value = 0;
|
329
|
|
- selectedTotal.value = 0;
|
330
|
|
- }
|
331
|
|
- }
|
332
|
|
- );
|
333
|
|
-
|
334
|
|
- onMounted(async () => {
|
335
|
|
- const { initSelectedOptions } = props;
|
336
|
|
- if (initSelectedOptions && isFunction(initSelectedOptions)) {
|
337
|
|
- const options = await initSelectedOptions(actionType);
|
338
|
|
- setSelectedOptions(options);
|
339
|
|
- }
|
340
|
|
- });
|
341
|
|
-</script>
|
342
|
|
-
|
343
|
|
-<template>
|
344
|
|
- <section>
|
345
|
|
- <BasicModal @register="registerModal" v-bind="getModalBindProps">
|
346
|
|
- <section class="bg-gray-100">
|
347
|
|
- <Tabs v-model:active-key="activeKey" type="card" @change="handleCheckoutPanel">
|
348
|
|
- <Tabs.TabPane :key="Active.PENDING">
|
349
|
|
- <template #tab>
|
350
|
|
- <div class="flex items-center justify-center">
|
351
|
|
- <span>待选设备</span>
|
352
|
|
- <Badge show-zero :count="pendingTotal" />
|
353
|
|
- </div>
|
354
|
|
- </template>
|
355
|
|
- <BasicTable @register="regsterPendingTable">
|
356
|
|
- <template #toolbar>
|
357
|
|
- <section class="flex w-full justify-end items-center">
|
358
|
|
- <!-- <Button type="primary">全选</Button> -->
|
359
|
|
- <div class="text-blue-400">
|
360
|
|
- <span class="mr-2">选择设备:</span>
|
361
|
|
- <span>{{ pendingConfirmQueue.length }}</span>
|
362
|
|
- </div>
|
363
|
|
- </section>
|
364
|
|
- </template>
|
365
|
|
- </BasicTable>
|
366
|
|
- <section class="flex justify-end px-4 pb-4">
|
367
|
|
- <Button
|
368
|
|
- type="primary"
|
369
|
|
- @click="handleSelected"
|
370
|
|
- :disabled="!pendingConfirmQueue.length"
|
371
|
|
- >
|
372
|
|
- <span>确定已选</span>
|
373
|
|
- </Button>
|
374
|
|
- </section>
|
375
|
|
- </Tabs.TabPane>
|
376
|
|
- <Tabs.TabPane :key="Active.SELECTED">
|
377
|
|
- <template #tab>
|
378
|
|
- <div class="flex items-center justify-center">
|
379
|
|
- <span>已选设备</span>
|
380
|
|
- <Badge show-zero :count="selectedTotal" />
|
381
|
|
- </div>
|
382
|
|
- </template>
|
383
|
|
- <BasicTable @register="registerSelectedTable">
|
384
|
|
- <template #toolbar>
|
385
|
|
- <section class="flex w-full justify-end items-center">
|
386
|
|
- <!-- <Button type="primary">全选</Button> -->
|
387
|
|
- <div class="text-blue-400">
|
388
|
|
- <span class="mr-2">选择设备:</span>
|
389
|
|
- <span>{{ selectedConfirmQueue.length }}</span>
|
390
|
|
- </div>
|
391
|
|
- </section>
|
392
|
|
- </template>
|
393
|
|
- </BasicTable>
|
394
|
|
- <section class="flex justify-end px-4 pb-4">
|
395
|
|
- <Button
|
396
|
|
- type="primary"
|
397
|
|
- :disabled="!selectedConfirmQueue.length"
|
398
|
|
- @click="handleRemoveSelected"
|
399
|
|
- >
|
400
|
|
- <span>移除已选</span>
|
401
|
|
- </Button>
|
402
|
|
- </section>
|
403
|
|
- </Tabs.TabPane>
|
404
|
|
- </Tabs>
|
405
|
|
- </section>
|
406
|
|
- </BasicModal>
|
407
|
|
- <Button @click="handleOpenModal" v-bind="getBindButtonProps">
|
408
|
|
- <span v-if="!selectedRowKeys.length">选择设备</span>
|
409
|
|
- <div v-if="selectedRowKeys.length">
|
410
|
|
- <Tag
|
411
|
|
- class="!px-2 !py-1 !bg-gray-50 !border-gray-100"
|
412
|
|
- v-for="item in getShowTagOptions"
|
413
|
|
- :key="item.value"
|
414
|
|
- >
|
415
|
|
- <span>
|
416
|
|
- {{ item.alias || item.name }}
|
417
|
|
- </span>
|
418
|
|
- </Tag>
|
419
|
|
- <Tag class="!px-2 !py-1 !bg-gray-50 !border-gray-100" v-if="getSurplusOptionsLength">
|
420
|
|
- <span> +{{ getSurplusOptionsLength }}... </span>
|
421
|
|
- </Tag>
|
422
|
|
- </div>
|
423
|
|
- </Button>
|
424
|
|
- </section>
|
425
|
|
-</template>
|
426
|
|
-
|
427
|
|
-<style lang="less">
|
428
|
|
- @prefix-cls: ~'@{namespace}-transfer-table-modal';
|
429
|
|
-
|
430
|
|
- .@{prefix-cls} {
|
431
|
|
- .vben-basic-table {
|
432
|
|
- padding-top: 0;
|
433
|
|
- }
|
434
|
|
-
|
435
|
|
- .vben-basic-form > .ant-row {
|
436
|
|
- width: 100%;
|
437
|
|
- }
|
438
|
|
-
|
439
|
|
- .ant-tabs-top-bar {
|
440
|
|
- background-color: #fff;
|
441
|
|
- }
|
442
|
|
- }
|
443
|
|
-</style> |