@tmagic/form 1.4.8 → 1.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/style.css +4 -0
- package/dist/tmagic-form.js +13 -12
- package/dist/tmagic-form.umd.cjs +13 -12
- package/package.json +5 -5
- package/src/Form.vue +201 -0
- package/src/FormBox.vue +120 -0
- package/src/FormDialog.vue +173 -0
- package/src/FormDrawer.vue +159 -0
- package/src/containers/Col.vue +52 -0
- package/src/containers/Container.vue +408 -0
- package/src/containers/Fieldset.vue +124 -0
- package/src/containers/GroupList.vue +139 -0
- package/src/containers/GroupListItem.vue +135 -0
- package/src/containers/Panel.vue +99 -0
- package/src/containers/Row.vue +54 -0
- package/src/containers/Step.vue +82 -0
- package/src/containers/Table.vue +648 -0
- package/src/containers/Tabs.vue +226 -0
- package/src/fields/Cascader.vue +131 -0
- package/src/fields/Checkbox.vue +58 -0
- package/src/fields/CheckboxGroup.vue +44 -0
- package/src/fields/ColorPicker.vue +30 -0
- package/src/fields/Date.vue +38 -0
- package/src/fields/DateTime.vue +47 -0
- package/src/fields/Daterange.vue +99 -0
- package/src/fields/Display.vue +21 -0
- package/src/fields/DynamicField.vue +90 -0
- package/src/fields/Hidden.vue +16 -0
- package/src/fields/Link.vue +86 -0
- package/src/fields/Number.vue +49 -0
- package/src/fields/NumberRange.vue +50 -0
- package/src/fields/RadioGroup.vue +28 -0
- package/src/fields/Select.vue +449 -0
- package/src/fields/Switch.vue +59 -0
- package/src/fields/Text.vue +170 -0
- package/src/fields/Textarea.vue +46 -0
- package/src/fields/Time.vue +34 -0
- package/src/fields/Timerange.vue +76 -0
- package/src/index.ts +139 -0
- package/src/schema.ts +758 -0
- package/src/shims-vue.d.ts +6 -0
- package/src/theme/container.scss +5 -0
- package/src/theme/date-time.scss +7 -0
- package/src/theme/fieldset.scss +28 -0
- package/src/theme/form-box.scss +13 -0
- package/src/theme/form-dialog.scss +13 -0
- package/src/theme/form-drawer.scss +11 -0
- package/src/theme/form.scss +43 -0
- package/src/theme/group-list.scss +23 -0
- package/src/theme/index.scss +15 -0
- package/src/theme/link.scss +3 -0
- package/src/theme/number-range.scss +8 -0
- package/src/theme/panel.scss +24 -0
- package/src/theme/select.scss +3 -0
- package/src/theme/table.scss +70 -0
- package/src/theme/tabs.scss +27 -0
- package/src/theme/text.scss +6 -0
- package/src/utils/config.ts +27 -0
- package/src/utils/containerProps.ts +50 -0
- package/src/utils/form.ts +273 -0
- package/src/utils/useAddField.ts +40 -0
- package/types/schema.d.ts +7 -5
- package/types/utils/form.d.ts +2 -2
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="m-fields-table-wrap">
|
|
3
|
+
<teleport to="body" :disabled="!isFullscreen">
|
|
4
|
+
<div ref="mTable" class="m-fields-table" :class="{ 'm-fields-table-item-extra': config.itemExtra }">
|
|
5
|
+
<span v-if="config.extra" style="color: rgba(0, 0, 0, 0.45)" v-html="config.extra"></span>
|
|
6
|
+
<TMagicTooltip content="拖拽可排序" placement="left-start" :disabled="config.dropSort !== true">
|
|
7
|
+
<TMagicTable
|
|
8
|
+
v-if="model[modelName]"
|
|
9
|
+
ref="tMagicTable"
|
|
10
|
+
style="width: 100%"
|
|
11
|
+
:row-key="config.rowKey || 'id'"
|
|
12
|
+
:data="data"
|
|
13
|
+
:lastData="lastData"
|
|
14
|
+
:border="config.border"
|
|
15
|
+
:max-height="config.maxHeight"
|
|
16
|
+
:default-expand-all="true"
|
|
17
|
+
:key="updateKey"
|
|
18
|
+
@select="selectHandle"
|
|
19
|
+
@sort-change="sortChange"
|
|
20
|
+
>
|
|
21
|
+
<TMagicTableColumn v-if="config.itemExtra" :fixed="'left'" width="30" type="expand">
|
|
22
|
+
<template v-slot="scope">
|
|
23
|
+
<span v-html="itemExtra(config.itemExtra, scope.$index)" class="m-form-tip"></span>
|
|
24
|
+
</template>
|
|
25
|
+
</TMagicTableColumn>
|
|
26
|
+
|
|
27
|
+
<TMagicTableColumn
|
|
28
|
+
label="操作"
|
|
29
|
+
:width="config.operateColWidth || 55"
|
|
30
|
+
align="center"
|
|
31
|
+
:fixed="config.fixed === false ? undefined : 'left'"
|
|
32
|
+
>
|
|
33
|
+
<template v-slot="scope">
|
|
34
|
+
<slot name="operateCol" :scope="scope"></slot>
|
|
35
|
+
<TMagicIcon
|
|
36
|
+
v-show="showDelete(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
37
|
+
class="m-table-delete-icon"
|
|
38
|
+
@click="removeHandler(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
39
|
+
><Delete
|
|
40
|
+
/></TMagicIcon>
|
|
41
|
+
</template>
|
|
42
|
+
</TMagicTableColumn>
|
|
43
|
+
|
|
44
|
+
<TMagicTableColumn v-if="sort && model[modelName] && model[modelName].length > 1" label="排序" width="60">
|
|
45
|
+
<template v-slot="scope">
|
|
46
|
+
<TMagicTooltip
|
|
47
|
+
v-if="scope.$index + 1 + pagecontext * pagesize - 1 !== 0"
|
|
48
|
+
content="点击上移,双击置顶"
|
|
49
|
+
placement="top"
|
|
50
|
+
>
|
|
51
|
+
<TMagicButton
|
|
52
|
+
plain
|
|
53
|
+
size="small"
|
|
54
|
+
type="primary"
|
|
55
|
+
:icon="ArrowUp"
|
|
56
|
+
:disabled="disabled"
|
|
57
|
+
link
|
|
58
|
+
@click="upHandler(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
59
|
+
@dblclick="topHandler(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
60
|
+
></TMagicButton>
|
|
61
|
+
</TMagicTooltip>
|
|
62
|
+
<TMagicTooltip
|
|
63
|
+
v-if="scope.$index + 1 + pagecontext * pagesize - 1 !== model[modelName].length - 1"
|
|
64
|
+
content="点击下移,双击置底"
|
|
65
|
+
placement="top"
|
|
66
|
+
>
|
|
67
|
+
<TMagicButton
|
|
68
|
+
plain
|
|
69
|
+
size="small"
|
|
70
|
+
type="primary"
|
|
71
|
+
:icon="ArrowDown"
|
|
72
|
+
:disabled="disabled"
|
|
73
|
+
link
|
|
74
|
+
@click="downHandler(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
75
|
+
@dblclick="bottomHandler(scope.$index + 1 + pagecontext * pagesize - 1)"
|
|
76
|
+
></TMagicButton>
|
|
77
|
+
</TMagicTooltip>
|
|
78
|
+
</template>
|
|
79
|
+
</TMagicTableColumn>
|
|
80
|
+
|
|
81
|
+
<TMagicTableColumn
|
|
82
|
+
v-if="selection"
|
|
83
|
+
align="center"
|
|
84
|
+
header-align="center"
|
|
85
|
+
type="selection"
|
|
86
|
+
width="45"
|
|
87
|
+
></TMagicTableColumn>
|
|
88
|
+
|
|
89
|
+
<TMagicTableColumn width="60" label="序号" v-if="showIndex && config.showIndex">
|
|
90
|
+
<template v-slot="scope">{{ scope.$index + 1 + pagecontext * pagesize }}</template>
|
|
91
|
+
</TMagicTableColumn>
|
|
92
|
+
|
|
93
|
+
<template v-for="(column, index) in config.items">
|
|
94
|
+
<TMagicTableColumn
|
|
95
|
+
v-if="column.type !== 'hidden' && display(column.display)"
|
|
96
|
+
:prop="column.name"
|
|
97
|
+
:width="column.width"
|
|
98
|
+
:label="column.label"
|
|
99
|
+
:sortable="column.sortable"
|
|
100
|
+
:sort-orders="['ascending', 'descending']"
|
|
101
|
+
:key="column[mForm?.keyProp || '__key'] ?? index"
|
|
102
|
+
:class-name="config.dropSort === true ? 'el-table__column--dropable' : ''"
|
|
103
|
+
>
|
|
104
|
+
<template #default="scope">
|
|
105
|
+
<Container
|
|
106
|
+
v-if="scope.$index > -1"
|
|
107
|
+
labelWidth="0"
|
|
108
|
+
:disabled="disabled"
|
|
109
|
+
:prop="getProp(scope.$index)"
|
|
110
|
+
:rules="column.rules"
|
|
111
|
+
:config="makeConfig(column, scope.row)"
|
|
112
|
+
:model="scope.row"
|
|
113
|
+
:lastValues="lastData[scope.$index]"
|
|
114
|
+
:is-compare="isCompare"
|
|
115
|
+
:size="size"
|
|
116
|
+
@change="$emit('change', model[modelName])"
|
|
117
|
+
@addDiffCount="onAddDiffCount()"
|
|
118
|
+
></Container>
|
|
119
|
+
</template>
|
|
120
|
+
</TMagicTableColumn>
|
|
121
|
+
</template>
|
|
122
|
+
</TMagicTable>
|
|
123
|
+
</TMagicTooltip>
|
|
124
|
+
<slot></slot>
|
|
125
|
+
|
|
126
|
+
<div style="display: flex; justify-content: space-between; margin: 10px 0">
|
|
127
|
+
<TMagicButton v-if="addable" size="small" type="primary" :disabled="disabled" plain @click="newHandler()"
|
|
128
|
+
>新增一行</TMagicButton
|
|
129
|
+
>
|
|
130
|
+
|
|
131
|
+
<div style="display: flex">
|
|
132
|
+
<TMagicButton
|
|
133
|
+
:icon="Grid"
|
|
134
|
+
size="small"
|
|
135
|
+
type="primary"
|
|
136
|
+
@click="toggleMode"
|
|
137
|
+
v-if="enableToggleMode && config.enableToggleMode !== false && !isFullscreen"
|
|
138
|
+
>展开配置</TMagicButton
|
|
139
|
+
>
|
|
140
|
+
<TMagicButton
|
|
141
|
+
:icon="FullScreen"
|
|
142
|
+
size="small"
|
|
143
|
+
type="primary"
|
|
144
|
+
@click="toggleFullscreen"
|
|
145
|
+
v-if="config.enableFullscreen !== false"
|
|
146
|
+
>
|
|
147
|
+
{{ isFullscreen ? '退出全屏' : '全屏编辑' }}
|
|
148
|
+
</TMagicButton>
|
|
149
|
+
<TMagicUpload
|
|
150
|
+
v-if="importable"
|
|
151
|
+
style="display: inline-block"
|
|
152
|
+
ref="excelBtn"
|
|
153
|
+
action="/noop"
|
|
154
|
+
:disabled="disabled"
|
|
155
|
+
:on-change="excelHandler"
|
|
156
|
+
:auto-upload="false"
|
|
157
|
+
>
|
|
158
|
+
<TMagicButton size="small" type="success" :disabled="disabled" plain>导入EXCEL</TMagicButton>
|
|
159
|
+
</TMagicUpload>
|
|
160
|
+
<TMagicButton
|
|
161
|
+
v-if="importable"
|
|
162
|
+
size="small"
|
|
163
|
+
type="warning"
|
|
164
|
+
:disabled="disabled"
|
|
165
|
+
plain
|
|
166
|
+
@click="clearHandler()"
|
|
167
|
+
>清空</TMagicButton
|
|
168
|
+
>
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
|
|
172
|
+
<div class="bottom" style="text-align: right" v-if="config.pagination">
|
|
173
|
+
<TMagicPagination
|
|
174
|
+
layout="total, sizes, prev, pager, next, jumper"
|
|
175
|
+
:hide-on-single-page="model[modelName].length < pagesize"
|
|
176
|
+
:current-page="pagecontext + 1"
|
|
177
|
+
:page-sizes="[pagesize, 60, 120, 300]"
|
|
178
|
+
:page-size="pagesize"
|
|
179
|
+
:total="model[modelName].length"
|
|
180
|
+
@size-change="handleSizeChange"
|
|
181
|
+
@current-change="handleCurrentChange"
|
|
182
|
+
>
|
|
183
|
+
</TMagicPagination>
|
|
184
|
+
</div>
|
|
185
|
+
</div>
|
|
186
|
+
</teleport>
|
|
187
|
+
</div>
|
|
188
|
+
</template>
|
|
189
|
+
|
|
190
|
+
<script setup lang="ts">
|
|
191
|
+
import { computed, inject, onMounted, ref, toRefs, watchEffect } from 'vue';
|
|
192
|
+
import { ArrowDown, ArrowUp, Delete, FullScreen, Grid } from '@element-plus/icons-vue';
|
|
193
|
+
import { cloneDeep } from 'lodash-es';
|
|
194
|
+
import Sortable, { SortableEvent } from 'sortablejs';
|
|
195
|
+
|
|
196
|
+
import {
|
|
197
|
+
TMagicButton,
|
|
198
|
+
TMagicIcon,
|
|
199
|
+
tMagicMessage,
|
|
200
|
+
TMagicPagination,
|
|
201
|
+
TMagicTable,
|
|
202
|
+
TMagicTableColumn,
|
|
203
|
+
TMagicTooltip,
|
|
204
|
+
TMagicUpload,
|
|
205
|
+
useZIndex,
|
|
206
|
+
} from '@tmagic/design';
|
|
207
|
+
import { asyncLoadJs, sleep } from '@tmagic/utils';
|
|
208
|
+
|
|
209
|
+
import { ColumnConfig, FormState, SortProp, TableConfig } from '../schema';
|
|
210
|
+
import { display as displayFunc, initValue } from '../utils/form';
|
|
211
|
+
|
|
212
|
+
import Container from './Container.vue';
|
|
213
|
+
|
|
214
|
+
defineOptions({
|
|
215
|
+
name: 'MFormTable',
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const props = withDefaults(
|
|
219
|
+
defineProps<{
|
|
220
|
+
model: any;
|
|
221
|
+
lastValues?: any;
|
|
222
|
+
isCompare?: boolean;
|
|
223
|
+
config: TableConfig;
|
|
224
|
+
name: string;
|
|
225
|
+
prop?: string;
|
|
226
|
+
labelWidth?: string;
|
|
227
|
+
sort?: boolean;
|
|
228
|
+
disabled?: boolean;
|
|
229
|
+
sortKey?: string;
|
|
230
|
+
text?: string;
|
|
231
|
+
size?: string;
|
|
232
|
+
enableToggleMode?: boolean;
|
|
233
|
+
showIndex?: boolean;
|
|
234
|
+
}>(),
|
|
235
|
+
{
|
|
236
|
+
prop: '',
|
|
237
|
+
sortKey: '',
|
|
238
|
+
enableToggleMode: true,
|
|
239
|
+
showIndex: true,
|
|
240
|
+
lastValues: () => ({}),
|
|
241
|
+
isCompare: false,
|
|
242
|
+
},
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const emit = defineEmits(['change', 'select', 'addDiffCount']);
|
|
246
|
+
|
|
247
|
+
let timer: any | null = null;
|
|
248
|
+
const mForm = inject<FormState | undefined>('mForm');
|
|
249
|
+
|
|
250
|
+
const { nextZIndex } = useZIndex();
|
|
251
|
+
|
|
252
|
+
const tMagicTable = ref<InstanceType<typeof TMagicTable>>();
|
|
253
|
+
const excelBtn = ref<InstanceType<typeof TMagicUpload>>();
|
|
254
|
+
const mTable = ref<HTMLDivElement>();
|
|
255
|
+
|
|
256
|
+
const pagesize = ref(10);
|
|
257
|
+
const pagecontext = ref(0);
|
|
258
|
+
const updateKey = ref(1);
|
|
259
|
+
const isFullscreen = ref(false);
|
|
260
|
+
|
|
261
|
+
const modelName = computed(() => props.name || props.config.name || '');
|
|
262
|
+
|
|
263
|
+
const data = computed(() =>
|
|
264
|
+
props.config.pagination
|
|
265
|
+
? (props.model[modelName.value] || []).filter(
|
|
266
|
+
(item: any, index: number) =>
|
|
267
|
+
index >= pagecontext.value * pagesize.value && index + 1 <= (pagecontext.value + 1) * pagesize.value,
|
|
268
|
+
)
|
|
269
|
+
: props.model[modelName.value],
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const lastData = computed(() =>
|
|
273
|
+
props.config.pagination
|
|
274
|
+
? (props.lastValues[modelName.value] || []).filter(
|
|
275
|
+
(item: any, index: number) =>
|
|
276
|
+
index >= pagecontext.value * pagesize.value && index + 1 <= (pagecontext.value + 1) * pagesize.value,
|
|
277
|
+
)
|
|
278
|
+
: props.lastValues[modelName.value] || [],
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
const sortChange = ({ prop, order }: SortProp) => {
|
|
282
|
+
if (order === 'ascending') {
|
|
283
|
+
props.model[modelName.value] = props.model[modelName.value].sort((a: any, b: any) => a[prop] - b[prop]);
|
|
284
|
+
} else if (order === 'descending') {
|
|
285
|
+
props.model[modelName.value] = props.model[modelName.value].sort((a: any, b: any) => b[prop] - a[prop]);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const foreUpdate = () => {
|
|
290
|
+
updateKey.value += 1;
|
|
291
|
+
setTimeout(() => rowDrop());
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const swapArray = (index1: number, index2: number) => {
|
|
295
|
+
props.model[modelName.value].splice(index1, 0, props.model[modelName.value].splice(index2, 1)[0]);
|
|
296
|
+
|
|
297
|
+
if (props.sortKey) {
|
|
298
|
+
for (let i = props.model[modelName.value].length - 1, v = 0; i >= 0; i--, v++) {
|
|
299
|
+
props.model[modelName.value][v][props.sortKey] = i;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
mForm?.$emit('field-change', props.prop, props.model[modelName.value]);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const rowDrop = () => {
|
|
306
|
+
const tableEl = tMagicTable.value?.instance.$el;
|
|
307
|
+
const tBodyEl = tableEl?.querySelector('.el-table__body > tbody');
|
|
308
|
+
if (tBodyEl) {
|
|
309
|
+
// eslint-disable-next-line prefer-destructuring
|
|
310
|
+
const sortable = Sortable.create(tBodyEl, {
|
|
311
|
+
onEnd: ({ newIndex, oldIndex }: SortableEvent) => {
|
|
312
|
+
if (typeof newIndex === 'undefined') return;
|
|
313
|
+
if (typeof oldIndex === 'undefined') return;
|
|
314
|
+
swapArray(newIndex, oldIndex);
|
|
315
|
+
emit('change', props.model[modelName.value]);
|
|
316
|
+
foreUpdate();
|
|
317
|
+
sortable.destroy();
|
|
318
|
+
mForm?.$emit('field-change', props.prop, props.model[modelName.value]);
|
|
319
|
+
sleep(1000).then(() => {
|
|
320
|
+
const elTrs = tableEl.querySelectorAll('.el-table__body > tbody > tr');
|
|
321
|
+
if (elTrs?.[newIndex]) {
|
|
322
|
+
elTrs[newIndex].style.backgroundColor = 'rgba(243, 89, 59, 0.2)';
|
|
323
|
+
sleep(1000).then(() => {
|
|
324
|
+
elTrs[newIndex].style.backgroundColor = '';
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const newHandler = async (row?: any) => {
|
|
334
|
+
if (props.config.max && props.model[modelName.value].length >= props.config.max) {
|
|
335
|
+
tMagicMessage.error(`最多新增配置不能超过${props.config.max}条`);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (typeof props.config.beforeAddRow === 'function') {
|
|
340
|
+
const beforeCheckRes = props.config.beforeAddRow(mForm, {
|
|
341
|
+
model: props.model[modelName.value],
|
|
342
|
+
formValue: mForm?.values,
|
|
343
|
+
prop: props.prop,
|
|
344
|
+
});
|
|
345
|
+
if (!beforeCheckRes) return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const columns = props.config.items;
|
|
349
|
+
const enumValues = props.config.enum || [];
|
|
350
|
+
let enumV = [];
|
|
351
|
+
const { length } = props.model[modelName.value];
|
|
352
|
+
const key = props.config.key || 'id';
|
|
353
|
+
let inputs: any = {};
|
|
354
|
+
|
|
355
|
+
if (enumValues.length) {
|
|
356
|
+
if (length >= enumValues.length) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
enumV = enumValues.filter((item) => {
|
|
360
|
+
let i = 0;
|
|
361
|
+
for (; i < length; i++) {
|
|
362
|
+
if (item[key] === props.model[modelName.value][i][key]) {
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return i === length;
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
if (enumV.length > 0) {
|
|
370
|
+
// eslint-disable-next-line prefer-destructuring
|
|
371
|
+
inputs = enumV[0];
|
|
372
|
+
}
|
|
373
|
+
} else if (Array.isArray(row)) {
|
|
374
|
+
columns.forEach((column, index) => {
|
|
375
|
+
column.name && (inputs[column.name] = row[index]);
|
|
376
|
+
});
|
|
377
|
+
} else {
|
|
378
|
+
if (typeof props.config.defaultAdd === 'function') {
|
|
379
|
+
inputs = await props.config.defaultAdd(mForm, {
|
|
380
|
+
model: props.model[modelName.value],
|
|
381
|
+
formValue: mForm?.values,
|
|
382
|
+
});
|
|
383
|
+
} else if (props.config.defaultAdd) {
|
|
384
|
+
inputs = props.config.defaultAdd;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
inputs = await initValue(mForm, {
|
|
388
|
+
config: columns,
|
|
389
|
+
initValues: inputs,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (props.sortKey && length) {
|
|
394
|
+
inputs[props.sortKey] = props.model[modelName.value][length - 1][props.sortKey] - 1;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
props.model[modelName.value].push(inputs);
|
|
398
|
+
emit('change', props.model[modelName.value]);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
onMounted(() => {
|
|
402
|
+
if (props.config.defautSort) {
|
|
403
|
+
sortChange(props.config.defautSort);
|
|
404
|
+
} else if (props.config.defaultSort) {
|
|
405
|
+
sortChange(props.config.defaultSort);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (props.sort && props.sortKey) {
|
|
409
|
+
props.model[modelName.value].sort((a: any, b: any) => b[props.sortKey] - a[props.sortKey]);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
watchEffect(() => {
|
|
414
|
+
if (props.config.dropSort) {
|
|
415
|
+
rowDrop();
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
const addable = computed(() => {
|
|
420
|
+
if (!props.model[modelName.value].length) {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
if (typeof props.config.addable === 'function') {
|
|
424
|
+
return props.config.addable(mForm, {
|
|
425
|
+
model: props.model[modelName.value],
|
|
426
|
+
formValue: mForm?.values,
|
|
427
|
+
prop: props.prop,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
return typeof props.config.addable === 'undefined' ? true : props.config.addable;
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const selection = computed(() => {
|
|
434
|
+
if (typeof props.config.selection === 'function') {
|
|
435
|
+
return props.config.selection(mForm, { model: props.model[modelName.value] });
|
|
436
|
+
}
|
|
437
|
+
return props.config.selection;
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
const importable = computed(() => {
|
|
441
|
+
if (typeof props.config.importable === 'function') {
|
|
442
|
+
return props.config.importable(mForm, {
|
|
443
|
+
formValue: mForm?.values,
|
|
444
|
+
model: props.model[modelName.value],
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return typeof props.config.importable === 'undefined' ? false : props.config.importable;
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const display = (fuc: any) => displayFunc(mForm, fuc, props);
|
|
451
|
+
|
|
452
|
+
const itemExtra = (fuc: any, index: number) => {
|
|
453
|
+
if (typeof fuc === 'function') {
|
|
454
|
+
return fuc(mForm, {
|
|
455
|
+
values: mForm?.initValues,
|
|
456
|
+
model: props.model,
|
|
457
|
+
formValue: mForm ? mForm.values : props.model,
|
|
458
|
+
prop: props.prop,
|
|
459
|
+
index,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return fuc;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
const removeHandler = (index: number) => {
|
|
467
|
+
if (props.disabled) return;
|
|
468
|
+
props.model[modelName.value].splice(index, 1);
|
|
469
|
+
emit('change', props.model[modelName.value]);
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const selectHandle = (selection: any, row: any) => {
|
|
473
|
+
if (typeof props.config.selection === 'string' && props.config.selection === 'single') {
|
|
474
|
+
tMagicTable.value?.clearSelection();
|
|
475
|
+
tMagicTable.value?.toggleRowSelection(row, true);
|
|
476
|
+
}
|
|
477
|
+
emit('select', selection, row);
|
|
478
|
+
if (typeof props.config.onSelect === 'function') {
|
|
479
|
+
props.config.onSelect(mForm, { selection, row, config: props.config });
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const toggleRowSelection = (row: any, selected: boolean) => {
|
|
484
|
+
tMagicTable.value?.toggleRowSelection.call(tMagicTable.value, row, selected);
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const makeConfig = (config: ColumnConfig, row: any) => {
|
|
488
|
+
const newConfig = cloneDeep(config);
|
|
489
|
+
if (typeof config.itemsFunction === 'function') {
|
|
490
|
+
newConfig.items = config.itemsFunction(row);
|
|
491
|
+
}
|
|
492
|
+
delete newConfig.display;
|
|
493
|
+
return newConfig;
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const upHandler = (index: number) => {
|
|
497
|
+
if (timer) {
|
|
498
|
+
clearTimeout(timer);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
timer = setTimeout(() => {
|
|
502
|
+
swapArray(index, index - 1);
|
|
503
|
+
}, 300);
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const topHandler = (index: number) => {
|
|
507
|
+
if (timer) {
|
|
508
|
+
clearTimeout(timer);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// 首先判断当前元素需要上移几个位置,置底移动到数组的第一位
|
|
512
|
+
const moveNum = index;
|
|
513
|
+
|
|
514
|
+
// 循环出需要一个一个上移的次数
|
|
515
|
+
for (let i = 0; i < moveNum; i++) {
|
|
516
|
+
swapArray(index, index - 1);
|
|
517
|
+
index -= 1;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
const downHandler = (index: number) => {
|
|
522
|
+
if (timer) {
|
|
523
|
+
clearTimeout(timer);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
timer = setTimeout(() => {
|
|
527
|
+
swapArray(index, index + 1);
|
|
528
|
+
}, 300);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const bottomHandler = (index: number) => {
|
|
532
|
+
if (timer) {
|
|
533
|
+
clearTimeout(timer);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// 首先判断当前元素需要上移几个位置,置底移动到数组的第一位
|
|
537
|
+
const moveNum = props.model[modelName.value].length - 1 - index;
|
|
538
|
+
|
|
539
|
+
// 循环出需要一个一个上移的次数
|
|
540
|
+
for (let i = 0; i < moveNum; i++) {
|
|
541
|
+
swapArray(index, index + 1);
|
|
542
|
+
index += 1;
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// 希望支持单行可控制是否显示删除按钮,不会影响现有逻辑
|
|
547
|
+
const showDelete = (index: number) => {
|
|
548
|
+
const deleteFunc = props.config.delete;
|
|
549
|
+
if (deleteFunc && typeof deleteFunc === 'function') {
|
|
550
|
+
return deleteFunc(props.model[modelName.value], index, mForm?.values);
|
|
551
|
+
}
|
|
552
|
+
return true;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const clearHandler = () => {
|
|
556
|
+
const len = props.model[modelName.value].length;
|
|
557
|
+
props.model[modelName.value].splice(0, len);
|
|
558
|
+
mForm?.$emit('field-change', props.prop, props.model[modelName.value]);
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const excelHandler = async (file: any) => {
|
|
562
|
+
if (!file?.raw) {
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (!(globalThis as any).XLSX) {
|
|
567
|
+
await asyncLoadJs('https://cdn.bootcdn.net/ajax/libs/xlsx/0.17.0/xlsx.full.min.js');
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const reader = new FileReader();
|
|
571
|
+
reader.onload = () => {
|
|
572
|
+
const data = reader.result;
|
|
573
|
+
const pdata = (globalThis as any).XLSX.read(data, { type: 'array' });
|
|
574
|
+
pdata.SheetNames.forEach((sheetName: string) => {
|
|
575
|
+
const arr = (globalThis as any).XLSX.utils.sheet_to_json(pdata.Sheets[sheetName], { header: 1 });
|
|
576
|
+
if (arr?.[0]) {
|
|
577
|
+
arr.forEach((row: any) => {
|
|
578
|
+
newHandler(row);
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
setTimeout(() => {
|
|
582
|
+
excelBtn.value?.clearFiles();
|
|
583
|
+
}, 300);
|
|
584
|
+
});
|
|
585
|
+
};
|
|
586
|
+
reader.readAsArrayBuffer(file.raw);
|
|
587
|
+
|
|
588
|
+
return false;
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
const handleSizeChange = (val: number) => {
|
|
592
|
+
pagesize.value = val;
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
const handleCurrentChange = (val: number) => {
|
|
596
|
+
pagecontext.value = val - 1;
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
const toggleMode = () => {
|
|
600
|
+
const calcLabelWidth = (label: string) => {
|
|
601
|
+
if (!label) return '0px';
|
|
602
|
+
const zhLength = label.match(/[^\x00-\xff]/g)?.length || 0;
|
|
603
|
+
const chLength = label.length - zhLength;
|
|
604
|
+
return `${Math.max(chLength * 8 + zhLength * 20, 80)}px`;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// 切换为groupList的形式
|
|
608
|
+
props.config.type = 'groupList';
|
|
609
|
+
props.config.enableToggleMode = true;
|
|
610
|
+
props.config.tableItems = props.config.items;
|
|
611
|
+
props.config.items =
|
|
612
|
+
props.config.groupItems ||
|
|
613
|
+
props.config.items.map((item: any) => {
|
|
614
|
+
const text = item.text || item.label;
|
|
615
|
+
const labelWidth = calcLabelWidth(text);
|
|
616
|
+
return {
|
|
617
|
+
...item,
|
|
618
|
+
text,
|
|
619
|
+
labelWidth,
|
|
620
|
+
span: item.span || 12,
|
|
621
|
+
};
|
|
622
|
+
});
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
const toggleFullscreen = () => {
|
|
626
|
+
if (!mTable.value) return;
|
|
627
|
+
|
|
628
|
+
if (isFullscreen.value) {
|
|
629
|
+
mTable.value.classList.remove('fixed');
|
|
630
|
+
isFullscreen.value = false;
|
|
631
|
+
} else {
|
|
632
|
+
mTable.value.classList.add('fixed');
|
|
633
|
+
mTable.value.style.zIndex = `${nextZIndex()}`;
|
|
634
|
+
isFullscreen.value = true;
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
const getProp = (index: number) => {
|
|
639
|
+
const { prop } = toRefs(props);
|
|
640
|
+
return `${prop.value}${prop.value ? '.' : ''}${index + 1 + pagecontext.value * pagesize.value - 1}`;
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
const onAddDiffCount = () => emit('addDiffCount');
|
|
644
|
+
|
|
645
|
+
defineExpose({
|
|
646
|
+
toggleRowSelection,
|
|
647
|
+
});
|
|
648
|
+
</script>
|