@tmagic/table 1.7.7 → 1.7.8-beta.1

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.
@@ -1,652 +0,0 @@
1
- import { defineComponent, createElementBlock, openBlock, Fragment, withDirectives, renderList, createBlock, unref, withCtx, createVNode, createElementVNode, vShow, createTextVNode, resolveDynamicComponent, mergeProps, toHandlers, createCommentVNode, normalizeProps, toDisplayString, computed, useTemplateRef, ref, resolveDirective, h } from 'vue';
2
- import { cloneDeep } from 'lodash-es';
3
- import { TMagicTooltip, TMagicButton, tMagicMessage, TMagicPopover, TMagicTag, TMagicTable } from '@tmagic/design';
4
- import { MForm, datetimeFormatter } from '@tmagic/form';
5
- import { setValueByKeyPath } from '@tmagic/utils';
6
-
7
- const _hoisted_1$2 = ["innerHTML"];
8
- const _sfc_main$5 = /* @__PURE__ */ defineComponent({
9
- ...{
10
- name: "MTableActionsColumn"
11
- },
12
- __name: "ActionsColumn",
13
- props: {
14
- columns: { default: () => [] },
15
- config: { default: () => ({}) },
16
- rowkeyName: { default: "c_id" },
17
- editState: { default: () => [] },
18
- row: {},
19
- index: {}
20
- },
21
- emits: ["after-action", "after-action-cancel"],
22
- setup(__props, { emit: __emit }) {
23
- const props = __props;
24
- const emit = __emit;
25
- const display = (fuc, row) => {
26
- if (typeof fuc === "function") {
27
- return fuc(row);
28
- }
29
- if (typeof fuc === "boolean") {
30
- return fuc;
31
- }
32
- return true;
33
- };
34
- const disabled = (fuc, row) => {
35
- if (typeof fuc === "function") {
36
- return fuc(row);
37
- }
38
- if (typeof fuc === "boolean") {
39
- return fuc;
40
- }
41
- return false;
42
- };
43
- const formatter = (fuc, row) => {
44
- if (typeof fuc === "function") {
45
- return fuc(row);
46
- }
47
- return fuc;
48
- };
49
- const actionHandler = async (action, row, index) => {
50
- await action.before?.(row, index);
51
- if (action.type === "edit") {
52
- props.editState[index] = cloneDeep(row);
53
- } else {
54
- await action.handler?.(row, index);
55
- }
56
- action.after?.(row, index);
57
- };
58
- const save = async (index, config) => {
59
- const action = config.actions?.find((item) => item.type === "edit")?.action;
60
- if (!action) return;
61
- const res = await action({
62
- data: props.editState[index],
63
- index
64
- });
65
- if (res) {
66
- if (res.ret === 0) {
67
- tMagicMessage.success("保存成功");
68
- props.editState[index] = void 0;
69
- emit("after-action", { index });
70
- } else {
71
- tMagicMessage.error(res.msg || "保存失败");
72
- }
73
- } else {
74
- props.editState[index] = void 0;
75
- emit("after-action", { index });
76
- }
77
- };
78
- const cancel = async (index, config) => {
79
- props.editState[index] = void 0;
80
- const cancel2 = config.actions?.find((item) => item.type === "edit")?.cancel;
81
- if (cancel2) {
82
- await cancel2({ index });
83
- }
84
- emit("after-action-cancel", { index });
85
- };
86
- return (_ctx, _cache) => {
87
- return openBlock(), createElementBlock(
88
- Fragment,
89
- null,
90
- [
91
- (openBlock(true), createElementBlock(
92
- Fragment,
93
- null,
94
- renderList(__props.config.actions, (action, actionIndex) => {
95
- return openBlock(), createBlock(unref(TMagicTooltip), {
96
- placement: action.tooltipPlacement || "top",
97
- key: actionIndex,
98
- disabled: !Boolean(action.tooltip),
99
- content: action.tooltip
100
- }, {
101
- default: withCtx(() => [
102
- withDirectives(createVNode(unref(TMagicButton), {
103
- class: "action-btn",
104
- link: "",
105
- size: "small",
106
- type: action.buttonType || "primary",
107
- icon: action.icon,
108
- disabled: disabled(action.disabled, __props.row),
109
- onClick: ($event) => actionHandler(action, __props.row, __props.index)
110
- }, {
111
- default: withCtx(() => [
112
- createElementVNode("span", {
113
- innerHTML: formatter(action.text, __props.row)
114
- }, null, 8, _hoisted_1$2)
115
- ]),
116
- _: 2
117
- /* DYNAMIC */
118
- }, 1032, ["type", "icon", "disabled", "onClick"]), [
119
- [vShow, display(action.display, __props.row) && !__props.editState[__props.index]]
120
- ])
121
- ]),
122
- _: 2
123
- /* DYNAMIC */
124
- }, 1032, ["placement", "disabled", "content"]);
125
- }),
126
- 128
127
- /* KEYED_FRAGMENT */
128
- )),
129
- withDirectives(createVNode(
130
- unref(TMagicButton),
131
- {
132
- class: "action-btn",
133
- link: "",
134
- type: "primary",
135
- size: "small",
136
- onClick: _cache[0] || (_cache[0] = ($event) => save(__props.index, __props.config))
137
- },
138
- {
139
- default: withCtx(() => [..._cache[2] || (_cache[2] = [
140
- createTextVNode(
141
- "保存",
142
- -1
143
- /* CACHED */
144
- )
145
- ])]),
146
- _: 1
147
- /* STABLE */
148
- },
149
- 512
150
- /* NEED_PATCH */
151
- ), [
152
- [vShow, __props.editState[__props.index]]
153
- ]),
154
- withDirectives(createVNode(
155
- unref(TMagicButton),
156
- {
157
- class: "action-btn",
158
- link: "",
159
- type: "danger",
160
- size: "small",
161
- onClick: _cache[1] || (_cache[1] = ($event) => cancel(__props.index, __props.config))
162
- },
163
- {
164
- default: withCtx(() => [..._cache[3] || (_cache[3] = [
165
- createTextVNode(
166
- "取消",
167
- -1
168
- /* CACHED */
169
- )
170
- ])]),
171
- _: 1
172
- /* STABLE */
173
- },
174
- 512
175
- /* NEED_PATCH */
176
- ), [
177
- [vShow, __props.editState[__props.index]]
178
- ])
179
- ],
180
- 64
181
- /* STABLE_FRAGMENT */
182
- );
183
- };
184
- }
185
- });
186
-
187
- const _sfc_main$4 = /* @__PURE__ */ defineComponent({
188
- ...{
189
- name: "MTableColumn"
190
- },
191
- __name: "ComponentColumn",
192
- props: {
193
- config: { default: () => ({}) },
194
- row: {},
195
- index: {}
196
- },
197
- setup(__props) {
198
- const props = __props;
199
- const componentProps = (row, index) => {
200
- if (typeof props.config.props === "function") {
201
- return props.config.props(row, index) || {};
202
- }
203
- return props.config.props || {};
204
- };
205
- const componentListeners = (row, index) => {
206
- if (typeof props.config.listeners === "function") {
207
- return props.config.listeners(row, index) || {};
208
- }
209
- return props.config.listeners || {};
210
- };
211
- return (_ctx, _cache) => {
212
- return openBlock(), createBlock(
213
- resolveDynamicComponent(__props.config.component),
214
- mergeProps(componentProps(__props.row, __props.index), toHandlers(componentListeners(__props.row, __props.index))),
215
- null,
216
- 16
217
- /* FULL_PROPS */
218
- );
219
- };
220
- }
221
- });
222
-
223
- const _hoisted_1$1 = ["innerHTML"];
224
- const _sfc_main$3 = /* @__PURE__ */ defineComponent({
225
- ...{
226
- name: "MTableExpandColumn"
227
- },
228
- __name: "ExpandColumn",
229
- props: {
230
- config: { default: () => ({}) },
231
- row: {}
232
- },
233
- setup(__props) {
234
- const props = __props;
235
- const componentProps = (row) => {
236
- if (typeof props.config.props === "function") {
237
- return props.config.props(row) || {};
238
- }
239
- return props.config.props || {};
240
- };
241
- return (_ctx, _cache) => {
242
- return openBlock(), createElementBlock(
243
- Fragment,
244
- null,
245
- [
246
- __props.config.table ? (openBlock(), createBlock(_sfc_main, {
247
- key: 0,
248
- "show-header": false,
249
- columns: __props.config.table,
250
- data: __props.config.prop && __props.row[__props.config.prop] || []
251
- }, null, 8, ["columns", "data"])) : createCommentVNode("v-if", true),
252
- __props.config.form ? (openBlock(), createBlock(unref(MForm), {
253
- key: 1,
254
- config: __props.config.form,
255
- "init-values": __props.config.values || __props.config.prop && __props.row[__props.config.prop] || {}
256
- }, null, 8, ["config", "init-values"])) : createCommentVNode("v-if", true),
257
- __props.config.expandContent ? (openBlock(), createElementBlock("div", {
258
- key: 2,
259
- innerHTML: __props.config.expandContent(__props.row, __props.config.prop)
260
- }, null, 8, _hoisted_1$1)) : createCommentVNode("v-if", true),
261
- __props.config.component ? (openBlock(), createBlock(
262
- resolveDynamicComponent(__props.config.component),
263
- normalizeProps(mergeProps({ key: 3 }, componentProps(__props.row))),
264
- null,
265
- 16
266
- /* FULL_PROPS */
267
- )) : createCommentVNode("v-if", true)
268
- ],
269
- 64
270
- /* STABLE_FRAGMENT */
271
- );
272
- };
273
- }
274
- });
275
-
276
- const formatter = (item, row, data) => {
277
- if (!item.prop) return "";
278
- if (item.formatter) {
279
- if (item.formatter === "datetime") {
280
- item.formatter = (value) => datetimeFormatter(value);
281
- }
282
- try {
283
- return item.formatter(row[item.prop], row, data);
284
- } catch (e) {
285
- console.error("Formatter error:", e);
286
- return row[item.prop];
287
- }
288
- } else {
289
- return row[item.prop];
290
- }
291
- };
292
- const createColumns = (columns) => columns;
293
-
294
- const _sfc_main$2 = /* @__PURE__ */ defineComponent({
295
- ...{
296
- name: "MTablePopoverColumn"
297
- },
298
- __name: "PopoverColumn",
299
- props: {
300
- config: { default: () => ({}) },
301
- row: {},
302
- index: {}
303
- },
304
- setup(__props) {
305
- return (_ctx, _cache) => {
306
- return __props.config.popover ? (openBlock(), createBlock(unref(TMagicPopover), {
307
- key: 0,
308
- placement: __props.config.popover.placement,
309
- width: __props.config.popover.width,
310
- trigger: __props.config.popover.trigger,
311
- "destroy-on-close": __props.config.popover.destroyOnClose ?? true
312
- }, {
313
- reference: withCtx(() => [
314
- createVNode(unref(TMagicButton), {
315
- link: "",
316
- type: "primary"
317
- }, {
318
- default: withCtx(() => [
319
- createTextVNode(
320
- toDisplayString(__props.config.text || unref(formatter)(__props.config, __props.row, { index: __props.index })),
321
- 1
322
- /* TEXT */
323
- )
324
- ]),
325
- _: 1
326
- /* STABLE */
327
- })
328
- ]),
329
- default: withCtx(() => [
330
- __props.config.popover.tableEmbed ? (openBlock(), createBlock(_sfc_main, {
331
- key: 0,
332
- "show-header": __props.config.showHeader,
333
- columns: __props.config.table,
334
- data: __props.config.prop && __props.row[__props.config.prop] || []
335
- }, null, 8, ["show-header", "columns", "data"])) : createCommentVNode("v-if", true)
336
- ]),
337
- _: 1
338
- /* STABLE */
339
- }, 8, ["placement", "width", "trigger", "destroy-on-close"])) : createCommentVNode("v-if", true);
340
- };
341
- }
342
- });
343
-
344
- const _hoisted_1 = { key: 0 };
345
- const _hoisted_2 = ["innerHTML"];
346
- const _hoisted_3 = ["href"];
347
- const _hoisted_4 = ["src"];
348
- const _hoisted_5 = ["href"];
349
- const _hoisted_6 = ["innerHTML"];
350
- const _sfc_main$1 = /* @__PURE__ */ defineComponent({
351
- ...{
352
- name: "MTableColumn"
353
- },
354
- __name: "TextColumn",
355
- props: {
356
- config: { default: () => ({}) },
357
- editState: { default: () => ({}) },
358
- row: {},
359
- index: {}
360
- },
361
- setup(__props) {
362
- const props = __props;
363
- const formChangeHandler = (v, eventData) => {
364
- if (eventData.changeRecords?.length) {
365
- for (const record of eventData.changeRecords) {
366
- if (record.propPath) {
367
- setValueByKeyPath(record.propPath, record.value, props.editState[props.index]);
368
- }
369
- }
370
- }
371
- };
372
- return (_ctx, _cache) => {
373
- return __props.config.type === "index" ? (openBlock(), createElementBlock(
374
- "div",
375
- _hoisted_1,
376
- toDisplayString(__props.config.pageIndex && __props.config.pageSize ? __props.config.pageIndex * __props.config.pageSize + __props.index + 1 : __props.index + 1),
377
- 1
378
- /* TEXT */
379
- )) : (__props.config.type || __props.config.editInlineFormConfig) && __props.editState[__props.index] ? (openBlock(), createBlock(unref(MForm), {
380
- key: 1,
381
- "label-width": "0",
382
- config: __props.config.editInlineFormConfig ?? [__props.config],
383
- "init-values": __props.editState[__props.index],
384
- onChange: formChangeHandler
385
- }, null, 8, ["config", "init-values"])) : __props.config.action === "actionLink" && __props.config.prop ? (openBlock(), createBlock(unref(TMagicButton), {
386
- key: 2,
387
- link: "",
388
- type: "primary",
389
- onClick: _cache[0] || (_cache[0] = ($event) => __props.config.handler?.(__props.row))
390
- }, {
391
- default: withCtx(() => [
392
- createElementVNode("span", {
393
- innerHTML: unref(formatter)(__props.config, __props.row, { index: __props.index })
394
- }, null, 8, _hoisted_2)
395
- ]),
396
- _: 1
397
- /* STABLE */
398
- })) : __props.config.action === "img" && __props.config.prop ? (openBlock(), createElementBlock("a", {
399
- key: 3,
400
- target: "_blank",
401
- href: __props.row[__props.config.prop]
402
- }, [
403
- createElementVNode("img", {
404
- src: __props.row[__props.config.prop],
405
- height: "50"
406
- }, null, 8, _hoisted_4)
407
- ], 8, _hoisted_3)) : __props.config.action === "link" && __props.config.prop ? (openBlock(), createElementBlock("a", {
408
- key: 4,
409
- target: "_blank",
410
- href: __props.row[__props.config.prop],
411
- class: "keep-all"
412
- }, toDisplayString(__props.row[__props.config.prop]), 9, _hoisted_5)) : __props.config.action === "tip" ? (openBlock(), createBlock(unref(TMagicTooltip), {
413
- key: 5,
414
- placement: "left"
415
- }, {
416
- content: withCtx(() => [
417
- createElementVNode(
418
- "div",
419
- null,
420
- toDisplayString(unref(formatter)(__props.config, __props.row, { index: __props.index })),
421
- 1
422
- /* TEXT */
423
- )
424
- ]),
425
- default: withCtx(() => [
426
- createVNode(unref(TMagicButton), {
427
- link: "",
428
- type: "primary"
429
- }, {
430
- default: withCtx(() => [
431
- createTextVNode(
432
- toDisplayString(__props.config.buttonText || "扩展配置"),
433
- 1
434
- /* TEXT */
435
- )
436
- ]),
437
- _: 1
438
- /* STABLE */
439
- })
440
- ]),
441
- _: 1
442
- /* STABLE */
443
- })) : __props.config.action === "tag" && __props.config.prop ? (openBlock(), createBlock(unref(TMagicTag), {
444
- key: 6,
445
- type: typeof __props.config.type === "function" ? __props.config.type(__props.row[__props.config.prop], __props.row) : __props.config.type,
446
- "close-transition": ""
447
- }, {
448
- default: withCtx(() => [
449
- createTextVNode(
450
- toDisplayString(unref(formatter)(__props.config, __props.row, { index: __props.index })),
451
- 1
452
- /* TEXT */
453
- )
454
- ]),
455
- _: 1
456
- /* STABLE */
457
- }, 8, ["type"])) : (openBlock(), createElementBlock("div", {
458
- key: 7,
459
- innerHTML: unref(formatter)(__props.config, __props.row, { index: __props.index })
460
- }, null, 8, _hoisted_6));
461
- };
462
- }
463
- });
464
-
465
- const _sfc_main = /* @__PURE__ */ defineComponent({
466
- ...{
467
- name: "MTable"
468
- },
469
- __name: "Table",
470
- props: {
471
- data: {},
472
- columns: { default: () => [] },
473
- spanMethod: {},
474
- loading: { type: Boolean, default: false },
475
- bodyHeight: {},
476
- showHeader: { type: Boolean, default: true },
477
- emptyText: {},
478
- defaultExpandAll: { type: Boolean, default: false },
479
- rowkeyName: {},
480
- border: { type: Boolean, default: false }
481
- },
482
- emits: [
483
- "sort-change",
484
- "after-action",
485
- "after-action-cancel",
486
- "select",
487
- "select-all",
488
- "selection-change",
489
- "expand-change",
490
- "cell-click"
491
- ],
492
- setup(__props, { expose: __expose, emit: __emit }) {
493
- const props = __props;
494
- const emit = __emit;
495
- const cellRender = (config, { row = {}, $index }) => {
496
- if (config.type === "expand") {
497
- return h(_sfc_main$3, {
498
- config,
499
- row
500
- });
501
- }
502
- if (config.type === "component") {
503
- return h(_sfc_main$4, {
504
- config,
505
- row,
506
- index: $index
507
- });
508
- }
509
- if (config.actions) {
510
- return h(_sfc_main$5, {
511
- config,
512
- row,
513
- index: $index,
514
- rowkeyName: props.rowkeyName,
515
- editState: editState.value,
516
- columns: props.columns,
517
- onAfterAction: (payload) => emit("after-action", payload),
518
- onAfterActionCancel: (payload) => emit("after-action-cancel", payload)
519
- });
520
- }
521
- if (config.type === "popover") {
522
- return h(_sfc_main$2, {
523
- config,
524
- row,
525
- index: $index
526
- });
527
- }
528
- return h(_sfc_main$1, {
529
- config,
530
- row,
531
- index: $index,
532
- editState: editState.value
533
- });
534
- };
535
- const tableColumns = computed(
536
- () => props.columns.map((item) => {
537
- let type = "default";
538
- if (item.type === "expand") {
539
- type = "expand";
540
- } else if (item.selection) {
541
- type = "selection";
542
- }
543
- return {
544
- props: {
545
- label: item.label,
546
- fixed: item.fixed,
547
- width: item.width ?? (item.selection ? 40 : void 0),
548
- prop: item.prop,
549
- type,
550
- selectable: item.selectable
551
- },
552
- cell: type === "selection" ? void 0 : ({ row, $index }) => cellRender(item, { row, $index })
553
- };
554
- })
555
- );
556
- const tMagicTableRef = useTemplateRef("tMagicTable");
557
- const editState = ref([]);
558
- const selectionColumn = computed(() => {
559
- const column = props.columns.filter((item) => item.selection);
560
- return column.length ? column[0] : null;
561
- });
562
- const tableData = computed(() => {
563
- if (selectionColumn.value) {
564
- return props.data || [];
565
- }
566
- return cloneDeep(props.data) || [];
567
- });
568
- const hasBorder = computed(() => typeof props.border !== "undefined" ? props.border : true);
569
- const sortChange = (data) => {
570
- emit("sort-change", data);
571
- };
572
- const selectHandler = (selection, row) => {
573
- const column = selectionColumn.value;
574
- if (!column) {
575
- return;
576
- }
577
- if (column.selection === "single") ;
578
- emit("select", selection, row);
579
- };
580
- const selectAllHandler = (selection) => {
581
- emit("select-all", selection);
582
- };
583
- const selectionChangeHandler = (selection) => {
584
- emit("selection-change", selection);
585
- };
586
- const cellClickHandler = (...args) => {
587
- emit("cell-click", ...args);
588
- };
589
- const expandChange = (...args) => {
590
- emit("expand-change", ...args);
591
- };
592
- const toggleRowSelection = (row, selected) => {
593
- tMagicTableRef.value?.toggleRowSelection(row, selected);
594
- };
595
- const toggleRowExpansion = (row, expanded) => {
596
- tMagicTableRef.value?.toggleRowExpansion(row, expanded);
597
- };
598
- const clearSelection = () => {
599
- tMagicTableRef.value?.clearSelection();
600
- };
601
- const objectSpanMethod = (data) => {
602
- if (typeof props.spanMethod === "function") {
603
- return props.spanMethod(data);
604
- }
605
- return () => ({
606
- rowspan: 0,
607
- colspan: 0
608
- });
609
- };
610
- __expose({
611
- toggleRowSelection,
612
- toggleRowExpansion,
613
- clearSelection
614
- });
615
- return (_ctx, _cache) => {
616
- const _directive_loading = resolveDirective("loading");
617
- return withDirectives((openBlock(), createBlock(unref(TMagicTable), {
618
- class: "m-table",
619
- ref: "tMagicTable",
620
- "show-overflow-tooltip": true,
621
- "tooltip-effect": "dark",
622
- "tooltip-options": { popperOptions: { strategy: "absolute" } },
623
- data: tableData.value,
624
- "show-header": __props.showHeader,
625
- "max-height": __props.bodyHeight,
626
- "default-expand-all": __props.defaultExpandAll,
627
- border: hasBorder.value,
628
- "row-key": __props.rowkeyName || "id",
629
- "tree-props": { children: "children" },
630
- "empty-text": __props.emptyText || "暂无数据",
631
- "span-method": objectSpanMethod,
632
- columns: tableColumns.value,
633
- onSortChange: sortChange,
634
- onSelect: selectHandler,
635
- onSelectAll: selectAllHandler,
636
- onSelectionChange: selectionChangeHandler,
637
- onCellClick: cellClickHandler,
638
- onExpandChange: expandChange
639
- }, null, 8, ["data", "show-header", "max-height", "default-expand-all", "border", "row-key", "empty-text", "columns"])), [
640
- [_directive_loading, __props.loading]
641
- ]);
642
- };
643
- }
644
- });
645
-
646
- const index = {
647
- install(app) {
648
- app.component("m-table", _sfc_main);
649
- }
650
- };
651
-
652
- export { _sfc_main as MagicTable, createColumns, index as default, formatter };