z-crud-table 0.0.25 → 0.0.26
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/z-crud-table.js +144 -123
- package/dist/z-crud-table.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/z-crud-table.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import { defineComponent as G, ref as
|
|
2
|
-
import { ElMessage as k } from "element-plus";
|
|
3
|
-
import
|
|
4
|
-
const
|
|
1
|
+
import { defineComponent as G, ref as P, resolveComponent as g, createBlock as f, openBlock as n, mergeProps as U, withCtx as u, createElementBlock as h, Fragment as $, renderList as E, createCommentVNode as c, createTextVNode as D, toDisplayString as re, computed as j, reactive as H, onMounted as ne, resolveDirective as ie, normalizeClass as se, renderSlot as w, withDirectives as M, createVNode as S, createElementVNode as I, createSlots as ue } from "vue";
|
|
2
|
+
import { ElMessage as k, ElMessageBox as de } from "element-plus";
|
|
3
|
+
import pe from "axios";
|
|
4
|
+
const fe = /* @__PURE__ */ G({
|
|
5
5
|
__name: "DynamicForm",
|
|
6
6
|
props: {
|
|
7
7
|
modelValue: {},
|
|
8
8
|
formConfig: {},
|
|
9
9
|
rules: {}
|
|
10
10
|
},
|
|
11
|
-
setup(y, { expose:
|
|
12
|
-
const
|
|
13
|
-
return
|
|
11
|
+
setup(y, { expose: b }) {
|
|
12
|
+
const O = P(null);
|
|
13
|
+
return b({
|
|
14
14
|
validate: () => {
|
|
15
15
|
var s;
|
|
16
|
-
return (s =
|
|
16
|
+
return (s = O.value) == null ? void 0 : s.validate();
|
|
17
17
|
}
|
|
18
18
|
}), (s, e) => {
|
|
19
|
-
const z = g("el-input"),
|
|
20
|
-
return n(), f(A,
|
|
19
|
+
const z = g("el-input"), x = g("el-option"), v = g("el-select"), q = g("el-radio"), m = g("el-radio-group"), V = g("el-form-item"), A = g("el-form");
|
|
20
|
+
return n(), f(A, U({
|
|
21
21
|
model: s.modelValue,
|
|
22
22
|
rules: s.rules,
|
|
23
23
|
ref_key: "formRef",
|
|
24
|
-
ref:
|
|
24
|
+
ref: O
|
|
25
25
|
}, s.$attrs), {
|
|
26
26
|
default: u(() => [
|
|
27
|
-
(n(!0),
|
|
27
|
+
(n(!0), h($, null, E(s.formConfig, (r) => (n(), f(V, {
|
|
28
28
|
key: r.prop,
|
|
29
29
|
label: r.label,
|
|
30
30
|
prop: r.prop
|
|
31
31
|
}, {
|
|
32
32
|
default: u(() => [
|
|
33
|
-
r.type === "input" ? (n(), f(z,
|
|
33
|
+
r.type === "input" ? (n(), f(z, U({
|
|
34
34
|
key: 0,
|
|
35
35
|
modelValue: s.modelValue[r.prop],
|
|
36
36
|
"onUpdate:modelValue": (d) => s.modelValue[r.prop] = d
|
|
37
37
|
}, { ref_for: !0 }, r.componentProps), null, 16, ["modelValue", "onUpdate:modelValue"])) : c("", !0),
|
|
38
|
-
r.type === "textarea" ? (n(), f(z,
|
|
38
|
+
r.type === "textarea" ? (n(), f(z, U({
|
|
39
39
|
key: 1,
|
|
40
40
|
type: "textarea",
|
|
41
41
|
modelValue: s.modelValue[r.prop],
|
|
42
42
|
"onUpdate:modelValue": (d) => s.modelValue[r.prop] = d
|
|
43
43
|
}, { ref_for: !0 }, r.componentProps), null, 16, ["modelValue", "onUpdate:modelValue"])) : c("", !0),
|
|
44
|
-
r.type === "select" ? (n(), f(
|
|
44
|
+
r.type === "select" ? (n(), f(v, U({
|
|
45
45
|
key: 2,
|
|
46
46
|
modelValue: s.modelValue[r.prop],
|
|
47
47
|
"onUpdate:modelValue": (d) => s.modelValue[r.prop] = d
|
|
48
48
|
}, { ref_for: !0 }, r.componentProps), {
|
|
49
49
|
default: u(() => [
|
|
50
|
-
(n(!0),
|
|
50
|
+
(n(!0), h($, null, E(r.options, (d) => (n(), f(x, {
|
|
51
51
|
key: d.value,
|
|
52
52
|
label: d.label,
|
|
53
53
|
value: d.value
|
|
@@ -55,25 +55,25 @@ const pe = /* @__PURE__ */ G({
|
|
|
55
55
|
]),
|
|
56
56
|
_: 2
|
|
57
57
|
}, 1040, ["modelValue", "onUpdate:modelValue"])) : c("", !0),
|
|
58
|
-
r.type === "radio-group" ? (n(), f(m,
|
|
58
|
+
r.type === "radio-group" ? (n(), f(m, U({
|
|
59
59
|
key: 3,
|
|
60
60
|
modelValue: s.modelValue[r.prop],
|
|
61
61
|
"onUpdate:modelValue": (d) => s.modelValue[r.prop] = d
|
|
62
62
|
}, { ref_for: !0 }, r.componentProps), {
|
|
63
63
|
default: u(() => [
|
|
64
|
-
(n(!0),
|
|
64
|
+
(n(!0), h($, null, E(r.options, (d) => (n(), f(q, {
|
|
65
65
|
key: d.value,
|
|
66
66
|
label: d.value
|
|
67
67
|
}, {
|
|
68
68
|
default: u(() => [
|
|
69
|
-
|
|
69
|
+
D(re(d.label), 1)
|
|
70
70
|
]),
|
|
71
71
|
_: 2
|
|
72
72
|
}, 1032, ["label"]))), 128))
|
|
73
73
|
]),
|
|
74
74
|
_: 2
|
|
75
75
|
}, 1040, ["modelValue", "onUpdate:modelValue"])) : c("", !0),
|
|
76
|
-
r.type === "input-disabled" ? (n(), f(z,
|
|
76
|
+
r.type === "input-disabled" ? (n(), f(z, U({
|
|
77
77
|
key: 4,
|
|
78
78
|
"model-value": s.modelValue[r.prop],
|
|
79
79
|
disabled: ""
|
|
@@ -86,39 +86,60 @@ const pe = /* @__PURE__ */ G({
|
|
|
86
86
|
}, 16, ["model", "rules"]);
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
-
}),
|
|
89
|
+
}), _ = pe.create({
|
|
90
90
|
// VITE_APP_BASE_API 是在 .env 文件中定义的基础 URL
|
|
91
|
-
// 您可以根据您的项目需求进行修改
|
|
92
91
|
baseURL: "",
|
|
93
92
|
timeout: 1e4
|
|
94
93
|
// 请求超时时间
|
|
95
94
|
});
|
|
96
|
-
|
|
95
|
+
_.interceptors.request.use(
|
|
97
96
|
(y) => {
|
|
98
|
-
const
|
|
99
|
-
return
|
|
97
|
+
const b = localStorage.getItem("token");
|
|
98
|
+
return b && (y.headers.Authorization = "Bearer " + b), y;
|
|
100
99
|
},
|
|
101
100
|
(y) => (console.log(y), Promise.reject(y))
|
|
102
101
|
);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
_.interceptors.response.use(
|
|
103
|
+
/**
|
|
104
|
+
* @description 如果您想直接获取诸如 headers 或 status 之类的信息,
|
|
105
|
+
* 请直接返回 response => response
|
|
106
|
+
*/
|
|
107
|
+
(y) => {
|
|
108
|
+
const b = y.data;
|
|
109
|
+
return b.code !== 200 ? (k({
|
|
110
|
+
message: b.msg || "Error",
|
|
111
|
+
// 使用后端返回的 `msg` 作为错误提示
|
|
112
|
+
type: "error",
|
|
113
|
+
duration: 5 * 1e3
|
|
114
|
+
}), (b.code === 401 || b.code === 403) && de.confirm(
|
|
115
|
+
"您的登录状态已失效,请重新登录",
|
|
116
|
+
"确认登出",
|
|
117
|
+
{
|
|
118
|
+
confirmButtonText: "重新登录",
|
|
119
|
+
cancelButtonText: "取消",
|
|
120
|
+
type: "warning"
|
|
121
|
+
}
|
|
122
|
+
).then(() => {
|
|
123
|
+
localStorage.removeItem("token"), location.reload();
|
|
124
|
+
}), Promise.reject(new Error(b.msg || "Error"))) : b;
|
|
125
|
+
},
|
|
126
|
+
(y) => (console.log("HTTP Error: " + y), k({
|
|
127
|
+
message: "网络错误,请检查您的网络连接或联系管理员",
|
|
128
|
+
// 提供更友好的网络错误提示
|
|
108
129
|
type: "error",
|
|
109
130
|
duration: 5 * 1e3
|
|
110
131
|
}), Promise.reject(y))
|
|
111
132
|
);
|
|
112
|
-
const
|
|
133
|
+
const me = {
|
|
113
134
|
key: 0,
|
|
114
135
|
class: "flex flex-wrap items-center justify-between gap-4 mb-6"
|
|
115
|
-
},
|
|
136
|
+
}, ce = { class: "flex items-center gap-x-2" }, ge = { class: "flex items-center gap-x-3 action-buttons flex-shrink-0" }, ye = {
|
|
116
137
|
key: 0,
|
|
117
138
|
class: "flex items-center gap-x-2"
|
|
118
|
-
},
|
|
139
|
+
}, be = {
|
|
119
140
|
key: 1,
|
|
120
141
|
class: "flex justify-end"
|
|
121
|
-
},
|
|
142
|
+
}, we = { class: "dialog-footer" }, _e = /* @__PURE__ */ G({
|
|
122
143
|
__name: "CrudTable",
|
|
123
144
|
props: {
|
|
124
145
|
/**
|
|
@@ -180,45 +201,45 @@ const fe = {
|
|
|
180
201
|
submitAsFormData: { type: Boolean, default: !1 }
|
|
181
202
|
},
|
|
182
203
|
emits: ["open-dialog", "submit", "delete"],
|
|
183
|
-
setup(y, { expose:
|
|
184
|
-
const s =
|
|
204
|
+
setup(y, { expose: b, emit: O }) {
|
|
205
|
+
const s = O, e = y, z = j(() => ["crud-table-wrapper", `theme-${e.theme}`, e.customClass]), x = j(() => e.theme === "large-screen" ? "large-screen-dialog" : ""), v = (t, o) => t ? !0 : (k.error(`${o} prop is required.`), !1), q = async (t, o) => {
|
|
185
206
|
try {
|
|
186
207
|
let a = { ...o };
|
|
187
208
|
e.onBeforeSubmit && (a = await e.onBeforeSubmit(a));
|
|
188
209
|
let p = a;
|
|
189
210
|
if (e.submitAsFormData) {
|
|
190
211
|
const F = new FormData();
|
|
191
|
-
for (const
|
|
192
|
-
if (Object.prototype.hasOwnProperty.call(a,
|
|
193
|
-
const
|
|
194
|
-
F.append(
|
|
212
|
+
for (const C in a)
|
|
213
|
+
if (Object.prototype.hasOwnProperty.call(a, C)) {
|
|
214
|
+
const T = a[C];
|
|
215
|
+
F.append(C, T ?? "");
|
|
195
216
|
}
|
|
196
217
|
p = F;
|
|
197
218
|
}
|
|
198
219
|
if (l.submitting = !0, t === "add") {
|
|
199
|
-
if (!
|
|
200
|
-
await
|
|
220
|
+
if (!v(e.apiUrlCreate, "apiUrlCreate")) throw new Error("apiUrlCreate is not configured.");
|
|
221
|
+
await _.post(e.apiUrlCreate, p), k.success("新增成功");
|
|
201
222
|
} else {
|
|
202
|
-
if (!
|
|
203
|
-
await
|
|
223
|
+
if (!v(e.apiUrlUpdate, "apiUrlUpdate")) throw new Error("apiUrlUpdate is not configured.");
|
|
224
|
+
await _.put(e.apiUrlUpdate, p), k.success("更新成功");
|
|
204
225
|
}
|
|
205
|
-
return e.onAfterSubmit && e.onAfterSubmit(t, a), s("submit", { mode: t, data: a }), l.visible && (l.visible = !1),
|
|
226
|
+
return e.onAfterSubmit && e.onAfterSubmit(t, a), s("submit", { mode: t, data: a }), l.visible && (l.visible = !1), B(), Promise.resolve();
|
|
206
227
|
} catch (a) {
|
|
207
228
|
return console.error("Submit failed:", a), Promise.reject(a);
|
|
208
229
|
} finally {
|
|
209
230
|
l.submitting = !1;
|
|
210
231
|
}
|
|
211
|
-
}, m = H({ pageNum: 1, pageSize: 10, ...e.initialSearchForm }), V =
|
|
232
|
+
}, m = H({ pageNum: 1, pageSize: 10, ...e.initialSearchForm }), V = P([]), A = P(0), r = P(!1), d = P([]), l = H({ visible: !1, loading: !1, submitting: !1, mode: "add", data: {}, formRef: null }), J = j(() => l.mode === "add" ? "新增" : "编辑"), K = j(() => {
|
|
212
233
|
if (l.mode === "add") return e.dialogFormConfig.filter((o) => o.prop !== "id");
|
|
213
234
|
const t = [...e.dialogFormConfig.filter((o) => o.prop !== "id")];
|
|
214
235
|
return t.some((o) => o.prop === "id") || t.unshift({ type: "input-disabled", prop: "id", label: "用户ID" }), t;
|
|
215
|
-
}),
|
|
216
|
-
if (
|
|
236
|
+
}), B = async () => {
|
|
237
|
+
if (v(e.apiUrlQuery, "apiUrlQuery")) {
|
|
217
238
|
r.value = !0;
|
|
218
239
|
try {
|
|
219
240
|
let t = { ...m };
|
|
220
241
|
e.onBeforeQuery && (t = await e.onBeforeQuery(t));
|
|
221
|
-
const o = await
|
|
242
|
+
const o = await _.get(e.apiUrlQuery, { params: t });
|
|
222
243
|
if (o && o.data && Array.isArray(o.data.rows) && typeof o.data.total == "number") {
|
|
223
244
|
let a = o.data.rows;
|
|
224
245
|
e.onAfterQuery && (a = await e.onAfterQuery(a)), V.value = a, A.value = o.data.total;
|
|
@@ -230,26 +251,26 @@ const fe = {
|
|
|
230
251
|
r.value = !1;
|
|
231
252
|
}
|
|
232
253
|
}
|
|
233
|
-
},
|
|
234
|
-
m.pageNum = 1,
|
|
254
|
+
}, N = () => {
|
|
255
|
+
m.pageNum = 1, B();
|
|
235
256
|
}, X = () => {
|
|
236
257
|
const { pageNum: t, pageSize: o, ...a } = e.initialSearchForm;
|
|
237
258
|
Object.keys(m).forEach((p) => {
|
|
238
259
|
p !== "pageNum" && p !== "pageSize" && delete m[p];
|
|
239
|
-
}), Object.assign(m, a),
|
|
260
|
+
}), Object.assign(m, a), N();
|
|
240
261
|
}, Y = (t) => {
|
|
241
262
|
d.value = t;
|
|
242
|
-
},
|
|
263
|
+
}, Q = async (t, o) => {
|
|
243
264
|
let a;
|
|
244
265
|
if (t === "add" ? a = o ? { ...o } : { role: "user" } : a = { ...o }, e.onBeforeOpenDialog) {
|
|
245
266
|
const p = await e.onBeforeOpenDialog(t, a);
|
|
246
267
|
p && (a = p);
|
|
247
268
|
}
|
|
248
269
|
if (l.mode = t, l.visible = !0, t === "edit") {
|
|
249
|
-
if (!
|
|
270
|
+
if (!v(e.apiUrlDetail, "apiUrlDetail")) return;
|
|
250
271
|
l.loading = !0;
|
|
251
272
|
try {
|
|
252
|
-
const p = await
|
|
273
|
+
const p = await _.get(e.apiUrlDetail + "/" + a.id.toString());
|
|
253
274
|
l.data = p.data.data;
|
|
254
275
|
} finally {
|
|
255
276
|
l.loading = !1, e.onAfterOpenDialog && e.onAfterOpenDialog(t, l.data), s("open-dialog", { mode: t, data: l.data });
|
|
@@ -273,83 +294,83 @@ const fe = {
|
|
|
273
294
|
debugger;
|
|
274
295
|
}
|
|
275
296
|
if (l.submitting = !0, l.mode === "add") {
|
|
276
|
-
if (!
|
|
277
|
-
await
|
|
297
|
+
if (!v(e.apiUrlCreate, "apiUrlCreate")) return;
|
|
298
|
+
await _.post(e.apiUrlCreate, o), k.success("新增成功");
|
|
278
299
|
} else {
|
|
279
|
-
if (!
|
|
280
|
-
await
|
|
300
|
+
if (!v(e.apiUrlUpdate, "apiUrlUpdate")) return;
|
|
301
|
+
await _.put(e.apiUrlUpdate, o), k.success("更新成功");
|
|
281
302
|
}
|
|
282
|
-
e.onAfterSubmit && e.onAfterSubmit(l.mode, t), s("submit", { mode: l.mode, data: t }), l.visible = !1,
|
|
303
|
+
e.onAfterSubmit && e.onAfterSubmit(l.mode, t), s("submit", { mode: l.mode, data: t }), l.visible = !1, B();
|
|
283
304
|
} catch (t) {
|
|
284
305
|
console.log("Submit error or validation failed:", t);
|
|
285
306
|
} finally {
|
|
286
307
|
l.submitting = !1;
|
|
287
308
|
}
|
|
288
309
|
}, L = async (t) => {
|
|
289
|
-
if (
|
|
310
|
+
if (v(e.apiUrlDelete, "apiUrlDelete"))
|
|
290
311
|
try {
|
|
291
312
|
if (e.onBeforeDelete && await e.onBeforeDelete(t) === !1)
|
|
292
313
|
return;
|
|
293
314
|
const o = t.join(",");
|
|
294
|
-
await
|
|
315
|
+
await _.delete(e.apiUrlDelete + "/" + o.toString()), k.success("删除成功"), e.onAfterDelete && e.onAfterDelete(t), s("delete", t), V.value.length === t.length && m.pageNum > 1 && m.pageNum--, B();
|
|
295
316
|
} catch (o) {
|
|
296
317
|
console.error("Delete failed", o);
|
|
297
318
|
}
|
|
298
319
|
}, ee = (t) => {
|
|
299
|
-
m.pageSize = t,
|
|
320
|
+
m.pageSize = t, N();
|
|
300
321
|
}, te = (t) => {
|
|
301
|
-
m.pageNum = t,
|
|
322
|
+
m.pageNum = t, B();
|
|
302
323
|
};
|
|
303
|
-
return ne(
|
|
304
|
-
refresh:
|
|
324
|
+
return ne(B), b({
|
|
325
|
+
refresh: B,
|
|
305
326
|
// 刷新表格
|
|
306
|
-
search:
|
|
327
|
+
search: N,
|
|
307
328
|
// 按当前条件搜索
|
|
308
329
|
handleDelete: L,
|
|
309
330
|
// 手动触发删除
|
|
310
|
-
openDialog:
|
|
331
|
+
openDialog: Q,
|
|
311
332
|
// 手动打开弹窗
|
|
312
|
-
submit:
|
|
333
|
+
submit: q
|
|
313
334
|
// 手动提交
|
|
314
335
|
}), (t, o) => {
|
|
315
|
-
const a = g("el-button"), p = g("el-form-item"), F = g("el-form"),
|
|
316
|
-
return n(),
|
|
336
|
+
const a = g("el-button"), p = g("el-form-item"), F = g("el-form"), C = g("el-table-column"), T = g("el-popconfirm"), oe = g("el-table"), ae = g("el-pagination"), le = g("el-dialog"), W = ie("loading");
|
|
337
|
+
return n(), h("div", {
|
|
317
338
|
class: se(z.value)
|
|
318
339
|
}, [
|
|
319
|
-
|
|
320
|
-
e.showSearchSection ? (n(),
|
|
321
|
-
|
|
340
|
+
w(t.$slots, "header"),
|
|
341
|
+
e.showSearchSection ? (n(), h("div", me, [
|
|
342
|
+
S(F, {
|
|
322
343
|
model: m,
|
|
323
344
|
class: "query-form flex flex-nowrap items-center gap-x-4",
|
|
324
345
|
style: { "overflow-x": "auto", "padding-bottom": "8px" }
|
|
325
346
|
}, {
|
|
326
347
|
default: u(() => [
|
|
327
|
-
|
|
328
|
-
|
|
348
|
+
w(t.$slots, "query-conditions", { searchForm: m }),
|
|
349
|
+
S(p, { class: "!mr-0 flex-shrink-0" }, {
|
|
329
350
|
default: u(() => [
|
|
330
|
-
I("div",
|
|
331
|
-
|
|
332
|
-
e.showSearchActionButtons ? (n(),
|
|
333
|
-
|
|
351
|
+
I("div", ce, [
|
|
352
|
+
w(t.$slots, "query-left"),
|
|
353
|
+
e.showSearchActionButtons ? (n(), h($, { key: 0 }, [
|
|
354
|
+
S(a, {
|
|
334
355
|
type: "primary",
|
|
335
|
-
onClick:
|
|
356
|
+
onClick: N,
|
|
336
357
|
loading: r.value
|
|
337
358
|
}, {
|
|
338
359
|
default: u(() => o[6] || (o[6] = [
|
|
339
|
-
|
|
360
|
+
D("搜索")
|
|
340
361
|
])),
|
|
341
362
|
_: 1,
|
|
342
363
|
__: [6]
|
|
343
364
|
}, 8, ["loading"]),
|
|
344
|
-
|
|
365
|
+
S(a, { onClick: X }, {
|
|
345
366
|
default: u(() => o[7] || (o[7] = [
|
|
346
|
-
|
|
367
|
+
D("清空")
|
|
347
368
|
])),
|
|
348
369
|
_: 1,
|
|
349
370
|
__: [7]
|
|
350
371
|
})
|
|
351
372
|
], 64)) : c("", !0),
|
|
352
|
-
|
|
373
|
+
w(t.$slots, "query-right")
|
|
353
374
|
])
|
|
354
375
|
]),
|
|
355
376
|
_: 3
|
|
@@ -357,43 +378,43 @@ const fe = {
|
|
|
357
378
|
]),
|
|
358
379
|
_: 3
|
|
359
380
|
}, 8, ["model"]),
|
|
360
|
-
I("div",
|
|
361
|
-
|
|
362
|
-
|
|
381
|
+
I("div", ge, [
|
|
382
|
+
w(t.$slots, "action-left", { selections: d.value }),
|
|
383
|
+
w(t.$slots, "add-button-content", {}, () => [
|
|
363
384
|
e.showNewBtn ? (n(), f(a, {
|
|
364
385
|
key: 0,
|
|
365
386
|
type: "success",
|
|
366
|
-
onClick: o[0] || (o[0] = (i) =>
|
|
387
|
+
onClick: o[0] || (o[0] = (i) => Q("add"))
|
|
367
388
|
}, {
|
|
368
389
|
default: u(() => o[8] || (o[8] = [
|
|
369
|
-
|
|
390
|
+
D("新增")
|
|
370
391
|
])),
|
|
371
392
|
_: 1,
|
|
372
393
|
__: [8]
|
|
373
394
|
})) : c("", !0)
|
|
374
395
|
]),
|
|
375
|
-
|
|
396
|
+
w(t.$slots, "action-right")
|
|
376
397
|
])
|
|
377
398
|
])) : c("", !0),
|
|
378
|
-
M((n(), f(oe,
|
|
399
|
+
M((n(), f(oe, U({
|
|
379
400
|
data: V.value,
|
|
380
401
|
onSelectionChange: Y
|
|
381
402
|
}, t.$attrs, { style: { width: "100%", "margin-bottom": "1.5rem" } }), {
|
|
382
403
|
default: u(() => [
|
|
383
|
-
e.showSelectionColumn ? (n(), f(
|
|
404
|
+
e.showSelectionColumn ? (n(), f(C, {
|
|
384
405
|
key: 0,
|
|
385
406
|
type: "selection",
|
|
386
407
|
width: "55",
|
|
387
408
|
fixed: ""
|
|
388
409
|
})) : c("", !0),
|
|
389
|
-
e.showIndexColumn ? (n(), f(
|
|
410
|
+
e.showIndexColumn ? (n(), f(C, {
|
|
390
411
|
key: 1,
|
|
391
412
|
type: "index",
|
|
392
413
|
label: "序号",
|
|
393
414
|
width: "70",
|
|
394
415
|
fixed: ""
|
|
395
416
|
})) : c("", !0),
|
|
396
|
-
(n(!0),
|
|
417
|
+
(n(!0), h($, null, E(e.columns, (i) => (n(), f(C, U({
|
|
397
418
|
key: i.prop,
|
|
398
419
|
prop: i.prop,
|
|
399
420
|
label: i.label,
|
|
@@ -402,26 +423,26 @@ const fe = {
|
|
|
402
423
|
}, { ref_for: !0 }, i.attrs), ue({ _: 2 }, [
|
|
403
424
|
i.slot ? {
|
|
404
425
|
name: "default",
|
|
405
|
-
fn: u((
|
|
406
|
-
|
|
407
|
-
row:
|
|
426
|
+
fn: u((R) => [
|
|
427
|
+
w(t.$slots, i.slot, {
|
|
428
|
+
row: R.row
|
|
408
429
|
})
|
|
409
430
|
]),
|
|
410
431
|
key: "0"
|
|
411
432
|
} : void 0
|
|
412
433
|
]), 1040, ["prop", "label", "width", "sortable"]))), 128)),
|
|
413
|
-
e.showActionsColumn ? (n(), f(
|
|
434
|
+
e.showActionsColumn ? (n(), f(C, {
|
|
414
435
|
key: 2,
|
|
415
436
|
label: "操作",
|
|
416
437
|
width: y.actionsColumnWidth
|
|
417
438
|
}, {
|
|
418
439
|
default: u((i) => [
|
|
419
|
-
i.row ? (n(),
|
|
420
|
-
t.$slots.actions ?
|
|
440
|
+
i.row ? (n(), h("div", ye, [
|
|
441
|
+
t.$slots.actions ? w(t.$slots, "actions", {
|
|
421
442
|
key: 0,
|
|
422
443
|
row: i.row
|
|
423
|
-
}) : (n(),
|
|
424
|
-
|
|
444
|
+
}) : (n(), h($, { key: 1 }, [
|
|
445
|
+
w(t.$slots, "action-before-edit", {
|
|
425
446
|
row: i.row
|
|
426
447
|
}),
|
|
427
448
|
e.showEditButton ? (n(), f(a, {
|
|
@@ -429,30 +450,30 @@ const fe = {
|
|
|
429
450
|
size: "small",
|
|
430
451
|
type: "primary",
|
|
431
452
|
link: "",
|
|
432
|
-
onClick: (
|
|
453
|
+
onClick: (R) => Q("edit", i.row)
|
|
433
454
|
}, {
|
|
434
455
|
default: u(() => o[9] || (o[9] = [
|
|
435
|
-
|
|
456
|
+
D("编辑 ")
|
|
436
457
|
])),
|
|
437
458
|
_: 2,
|
|
438
459
|
__: [9]
|
|
439
460
|
}, 1032, ["onClick"])) : c("", !0),
|
|
440
|
-
e.showDeleteButton ? (n(), f(
|
|
461
|
+
e.showDeleteButton ? (n(), f(T, {
|
|
441
462
|
key: 1,
|
|
442
463
|
title: "确定要删除这条数据吗?",
|
|
443
|
-
onConfirm: (
|
|
464
|
+
onConfirm: (R) => L([i.row.id]),
|
|
444
465
|
"confirm-button-text": "确定",
|
|
445
466
|
"cancel-button-text": "取消",
|
|
446
467
|
width: "200"
|
|
447
468
|
}, {
|
|
448
469
|
reference: u(() => [
|
|
449
|
-
|
|
470
|
+
S(a, {
|
|
450
471
|
size: "small",
|
|
451
472
|
type: "danger",
|
|
452
473
|
link: ""
|
|
453
474
|
}, {
|
|
454
475
|
default: u(() => o[10] || (o[10] = [
|
|
455
|
-
|
|
476
|
+
D("删除")
|
|
456
477
|
])),
|
|
457
478
|
_: 1,
|
|
458
479
|
__: [10]
|
|
@@ -460,7 +481,7 @@ const fe = {
|
|
|
460
481
|
]),
|
|
461
482
|
_: 2
|
|
462
483
|
}, 1032, ["onConfirm"])) : c("", !0),
|
|
463
|
-
|
|
484
|
+
w(t.$slots, "action-after-delete", {
|
|
464
485
|
row: i.row
|
|
465
486
|
})
|
|
466
487
|
], 64))
|
|
@@ -473,8 +494,8 @@ const fe = {
|
|
|
473
494
|
}, 16, ["data"])), [
|
|
474
495
|
[W, r.value]
|
|
475
496
|
]),
|
|
476
|
-
e.showPagination && A.value > 0 ? (n(),
|
|
477
|
-
|
|
497
|
+
e.showPagination && A.value > 0 ? (n(), h("div", be, [
|
|
498
|
+
S(ae, {
|
|
478
499
|
"current-page": m.pageNum,
|
|
479
500
|
"onUpdate:currentPage": o[1] || (o[1] = (i) => m.pageNum = i),
|
|
480
501
|
"page-size": m.pageSize,
|
|
@@ -489,32 +510,32 @@ const fe = {
|
|
|
489
510
|
onCurrentChange: te
|
|
490
511
|
}, null, 8, ["current-page", "page-size", "page-sizes", "layout", "total", "background", "small", "hide-on-single-page"])
|
|
491
512
|
])) : c("", !0),
|
|
492
|
-
|
|
513
|
+
S(le, {
|
|
493
514
|
modelValue: l.visible,
|
|
494
515
|
"onUpdate:modelValue": o[5] || (o[5] = (i) => l.visible = i),
|
|
495
516
|
title: J.value,
|
|
496
517
|
width: e.dialogWidth,
|
|
497
518
|
"destroy-on-close": !0,
|
|
498
|
-
"custom-class":
|
|
519
|
+
"custom-class": x.value
|
|
499
520
|
}, {
|
|
500
521
|
footer: u(() => [
|
|
501
|
-
I("div",
|
|
502
|
-
|
|
522
|
+
I("div", we, [
|
|
523
|
+
S(a, {
|
|
503
524
|
onClick: o[4] || (o[4] = (i) => l.visible = !1)
|
|
504
525
|
}, {
|
|
505
526
|
default: u(() => o[11] || (o[11] = [
|
|
506
|
-
|
|
527
|
+
D("取消")
|
|
507
528
|
])),
|
|
508
529
|
_: 1,
|
|
509
530
|
__: [11]
|
|
510
531
|
}),
|
|
511
|
-
|
|
532
|
+
S(a, {
|
|
512
533
|
type: "primary",
|
|
513
534
|
onClick: Z,
|
|
514
535
|
loading: l.submitting
|
|
515
536
|
}, {
|
|
516
537
|
default: u(() => o[12] || (o[12] = [
|
|
517
|
-
|
|
538
|
+
D("确定")
|
|
518
539
|
])),
|
|
519
540
|
_: 1,
|
|
520
541
|
__: [12]
|
|
@@ -522,12 +543,12 @@ const fe = {
|
|
|
522
543
|
])
|
|
523
544
|
]),
|
|
524
545
|
default: u(() => [
|
|
525
|
-
M((n(),
|
|
526
|
-
|
|
546
|
+
M((n(), h("div", null, [
|
|
547
|
+
w(t.$slots, "dialog-form-content", {
|
|
527
548
|
formData: l.data,
|
|
528
549
|
mode: l.mode
|
|
529
550
|
}, () => [
|
|
530
|
-
e.dialogFormConfig.length > 0 ? (n(), f(
|
|
551
|
+
e.dialogFormConfig.length > 0 ? (n(), f(fe, {
|
|
531
552
|
key: 0,
|
|
532
553
|
"form-config": K.value,
|
|
533
554
|
modelValue: l.data,
|
|
@@ -548,5 +569,5 @@ const fe = {
|
|
|
548
569
|
}
|
|
549
570
|
});
|
|
550
571
|
export {
|
|
551
|
-
|
|
572
|
+
_e as CrudTable
|
|
552
573
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(f,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue"),require("element-plus"),require("axios")):typeof define=="function"&&define.amd?define(["exports","vue","element-plus","axios"],e):(f=typeof globalThis<"u"?globalThis:f||self,e(f.ZCrudTable={},f.Vue,f.ElementPlus,f.axios))})(this,function(f,e,C,$){"use strict";const z=e.defineComponent({__name:"DynamicForm",props:{modelValue:{},formConfig:{},rules:{}},setup(m,{expose:w}){const V=e.ref(null);return w({validate:()=>{var s;return(s=V.value)==null?void 0:s.validate()}}),(s,t)=>{const S=e.resolveComponent("el-input"),D=e.resolveComponent("el-option"),u=e.resolveComponent("el-select"),U=e.resolveComponent("el-radio"),c=e.resolveComponent("el-radio-group"),B=e.resolveComponent("el-form-item"),h=e.resolveComponent("el-form");return e.openBlock(),e.createBlock(h,e.mergeProps({model:s.modelValue,rules:s.rules,ref_key:"formRef",ref:V},s.$attrs),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(s.formConfig,n=>(e.openBlock(),e.createBlock(B,{key:n.prop,label:n.label,prop:n.prop},{default:e.withCtx(()=>[n.type==="input"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:0,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),null,16,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="textarea"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:1,type:"textarea",modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),null,16,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="select"?(e.openBlock(),e.createBlock(u,e.mergeProps({key:2,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(n.options,d=>(e.openBlock(),e.createBlock(D,{key:d.value,label:d.label,value:d.value},null,8,["label","value"]))),128))]),_:2},1040,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="radio-group"?(e.openBlock(),e.createBlock(c,e.mergeProps({key:3,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(n.options,d=>(e.openBlock(),e.createBlock(U,{key:d.value,label:d.value},{default:e.withCtx(()=>[e.createTextVNode(e.toDisplayString(d.label),1)]),_:2},1032,["label"]))),128))]),_:2},1040,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="input-disabled"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:4,"model-value":s.modelValue[n.prop],disabled:""},{ref_for:!0},n.componentProps),null,16,["model-value"])):e.createCommentVNode("",!0)]),_:2},1032,["label","prop"]))),128))]),_:1},16,["model","rules"])}}}),g=$.create({baseURL:"",timeout:1e4});g.interceptors.request.use(m=>{const w=localStorage.getItem("token");return w&&(m.headers.Authorization="Bearer "+w),m},m=>(console.log(m),Promise.reject(m))),g.interceptors.response.use(m=>m.data,m=>(console.log("err"+m),C.ElMessage({message:"接口错误,请刷新接口",type:"error",duration:5*1e3}),Promise.reject(m)));const P={key:0,class:"flex flex-wrap items-center justify-between gap-4 mb-6"},O={class:"flex items-center gap-x-2"},T={class:"flex items-center gap-x-3 action-buttons flex-shrink-0"},j={key:0,class:"flex items-center gap-x-2"},q={key:1,class:"flex justify-end"},Q={class:"dialog-footer"},M=e.defineComponent({__name:"CrudTable",props:{theme:{type:String,default:"default"},customClass:{type:String,default:""},apiUrlQuery:{type:String,required:!0},apiUrlDetail:{type:String,required:!0},apiUrlCreate:{type:String,required:!0},apiUrlUpdate:{type:String,required:!0},apiUrlDelete:{type:String,required:!0},showSearchSection:{type:Boolean,default:!0},showSearchActionButtons:{type:Boolean,default:!0},showNewBtn:{type:Boolean,default:!0},columns:{type:Array,default:()=>[]},onBeforeQuery:{type:Function},onAfterQuery:{type:Function},onBeforeOpenDialog:{type:Function},onAfterOpenDialog:{type:Function},onBeforeSubmit:{type:Function},onAfterSubmit:{type:Function},onBeforeDelete:{type:Function},onAfterDelete:{type:Function},showSelectionColumn:{type:Boolean,default:!0},showIndexColumn:{type:Boolean,default:!0},showActionsColumn:{type:Boolean,default:!0},showEditButton:{type:Boolean,default:!0},showDeleteButton:{type:Boolean,default:!0},actionsColumnWidth:{type:[String,Number],default:120},dialogWidth:{type:String,default:"50%"},initialSearchForm:{type:Object,default:()=>({pageNum:1,pageSize:10})},showPagination:{type:Boolean,default:!0},pageSizes:{type:Array,default:()=>[10,20,50,100]},paginationLayout:{type:String,default:"total, sizes, prev, pager, next, jumper"},paginationBackground:{type:Boolean,default:!0},paginationSmall:{type:Boolean,default:!1},paginationHideOnSinglePage:{type:Boolean,default:!1},dialogFormConfig:{type:Array,default:()=>[]},dialogFormRules:{type:Object,default:()=>({})},submitAsFormData:{type:Boolean,default:!1}},emits:["open-dialog","submit","delete"],setup(m,{expose:w,emit:V}){const s=V,t=m,S=e.computed(()=>["crud-table-wrapper",`theme-${t.theme}`,t.customClass]),D=e.computed(()=>t.theme==="large-screen"?"large-screen-dialog":""),u=(o,a)=>o?!0:(C.ElMessage.error(`${a} prop is required.`),!1),U=async(o,a)=>{try{let l={...a};t.onBeforeSubmit&&(l=await t.onBeforeSubmit(l));let p=l;if(t.submitAsFormData){const b=new FormData;for(const y in l)if(Object.prototype.hasOwnProperty.call(l,y)){const x=l[y];b.append(y,x??"")}p=b}if(r.submitting=!0,o==="add"){if(!u(t.apiUrlCreate,"apiUrlCreate"))throw new Error("apiUrlCreate is not configured.");await g.post(t.apiUrlCreate,p),C.ElMessage.success("新增成功")}else{if(!u(t.apiUrlUpdate,"apiUrlUpdate"))throw new Error("apiUrlUpdate is not configured.");await g.put(t.apiUrlUpdate,p),C.ElMessage.success("更新成功")}return t.onAfterSubmit&&t.onAfterSubmit(o,l),s("submit",{mode:o,data:l}),r.visible&&(r.visible=!1),k(),Promise.resolve()}catch(l){return console.error("Submit failed:",l),Promise.reject(l)}finally{r.submitting=!1}},c=e.reactive({pageNum:1,pageSize:10,...t.initialSearchForm}),B=e.ref([]),h=e.ref(0),n=e.ref(!1),d=e.ref([]),r=e.reactive({visible:!1,loading:!1,submitting:!1,mode:"add",data:{},formRef:null}),R=e.computed(()=>r.mode==="add"?"新增":"编辑"),L=e.computed(()=>{if(r.mode==="add")return t.dialogFormConfig.filter(a=>a.prop!=="id");const o=[...t.dialogFormConfig.filter(a=>a.prop!=="id")];return o.some(a=>a.prop==="id")||o.unshift({type:"input-disabled",prop:"id",label:"用户ID"}),o}),k=async()=>{if(u(t.apiUrlQuery,"apiUrlQuery")){n.value=!0;try{let o={...c};t.onBeforeQuery&&(o=await t.onBeforeQuery(o));const a=await g.get(t.apiUrlQuery,{params:o});if(a&&a.data&&Array.isArray(a.data.rows)&&typeof a.data.total=="number"){let l=a.data.rows;t.onAfterQuery&&(l=await t.onAfterQuery(l)),B.value=l,h.value=a.data.total}else console.warn("API response is not in the expected { data: { rows: [], total: 0 } } format."),B.value=[],h.value=0}catch(o){console.error("Fetch data failed:",o)}finally{n.value=!1}}},_=()=>{c.pageNum=1,k()},I=()=>{const{pageNum:o,pageSize:a,...l}=t.initialSearchForm;Object.keys(c).forEach(p=>{p!=="pageNum"&&p!=="pageSize"&&delete c[p]}),Object.assign(c,l),_()},W=o=>{d.value=o},N=async(o,a)=>{let l;if(o==="add"?l=a?{...a}:{role:"user"}:l={...a},t.onBeforeOpenDialog){const p=await t.onBeforeOpenDialog(o,l);p&&(l=p)}if(r.mode=o,r.visible=!0,o==="edit"){if(!u(t.apiUrlDetail,"apiUrlDetail"))return;r.loading=!0;try{const p=await g.get(t.apiUrlDetail+"/"+l.id.toString());r.data=p.data.data}finally{r.loading=!1,t.onAfterOpenDialog&&t.onAfterOpenDialog(o,r.data),s("open-dialog",{mode:o,data:r.data})}}else r.data=l,t.onAfterOpenDialog&&t.onAfterOpenDialog(o,r.data),s("open-dialog",{mode:o,data:r.data})},H=async()=>{try{r.formRef&&await r.formRef.validate();let o={...r.data};t.onBeforeSubmit&&(o=await t.onBeforeSubmit(o));let a=o;if(t.submitAsFormData){const l=new FormData;for(const p in o)if(Object.prototype.hasOwnProperty.call(o,p)){const b=o[p];l.append(p,b??"")}a=l;debugger}if(r.submitting=!0,r.mode==="add"){if(!u(t.apiUrlCreate,"apiUrlCreate"))return;await g.post(t.apiUrlCreate,a),C.ElMessage.success("新增成功")}else{if(!u(t.apiUrlUpdate,"apiUrlUpdate"))return;await g.put(t.apiUrlUpdate,a),C.ElMessage.success("更新成功")}t.onAfterSubmit&&t.onAfterSubmit(r.mode,o),s("submit",{mode:r.mode,data:o}),r.visible=!1,k()}catch(o){console.log("Submit error or validation failed:",o)}finally{r.submitting=!1}},A=async o=>{if(u(t.apiUrlDelete,"apiUrlDelete"))try{if(t.onBeforeDelete&&await t.onBeforeDelete(o)===!1)return;const a=o.join(",");await g.delete(t.apiUrlDelete+"/"+a.toString()),C.ElMessage.success("删除成功"),t.onAfterDelete&&t.onAfterDelete(o),s("delete",o),B.value.length===o.length&&c.pageNum>1&&c.pageNum--,k()}catch(a){console.error("Delete failed",a)}},Z=o=>{c.pageSize=o,_()},G=o=>{c.pageNum=o,k()};return e.onMounted(k),w({refresh:k,search:_,handleDelete:A,openDialog:N,submit:U}),(o,a)=>{const l=e.resolveComponent("el-button"),p=e.resolveComponent("el-form-item"),b=e.resolveComponent("el-form"),y=e.resolveComponent("el-table-column"),x=e.resolveComponent("el-popconfirm"),J=e.resolveComponent("el-table"),K=e.resolveComponent("el-pagination"),X=e.resolveComponent("el-dialog"),E=e.resolveDirective("loading");return e.openBlock(),e.createElementBlock("div",{class:e.normalizeClass(S.value)},[e.renderSlot(o.$slots,"header"),t.showSearchSection?(e.openBlock(),e.createElementBlock("div",P,[e.createVNode(b,{model:c,class:"query-form flex flex-nowrap items-center gap-x-4",style:{"overflow-x":"auto","padding-bottom":"8px"}},{default:e.withCtx(()=>[e.renderSlot(o.$slots,"query-conditions",{searchForm:c}),e.createVNode(p,{class:"!mr-0 flex-shrink-0"},{default:e.withCtx(()=>[e.createElementVNode("div",O,[e.renderSlot(o.$slots,"query-left"),t.showSearchActionButtons?(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},[e.createVNode(l,{type:"primary",onClick:_,loading:n.value},{default:e.withCtx(()=>a[6]||(a[6]=[e.createTextVNode("搜索")])),_:1,__:[6]},8,["loading"]),e.createVNode(l,{onClick:I},{default:e.withCtx(()=>a[7]||(a[7]=[e.createTextVNode("清空")])),_:1,__:[7]})],64)):e.createCommentVNode("",!0),e.renderSlot(o.$slots,"query-right")])]),_:3})]),_:3},8,["model"]),e.createElementVNode("div",T,[e.renderSlot(o.$slots,"action-left",{selections:d.value}),e.renderSlot(o.$slots,"add-button-content",{},()=>[t.showNewBtn?(e.openBlock(),e.createBlock(l,{key:0,type:"success",onClick:a[0]||(a[0]=i=>N("add"))},{default:e.withCtx(()=>a[8]||(a[8]=[e.createTextVNode("新增")])),_:1,__:[8]})):e.createCommentVNode("",!0)]),e.renderSlot(o.$slots,"action-right")])])):e.createCommentVNode("",!0),e.withDirectives((e.openBlock(),e.createBlock(J,e.mergeProps({data:B.value,onSelectionChange:W},o.$attrs,{style:{width:"100%","margin-bottom":"1.5rem"}}),{default:e.withCtx(()=>[t.showSelectionColumn?(e.openBlock(),e.createBlock(y,{key:0,type:"selection",width:"55",fixed:""})):e.createCommentVNode("",!0),t.showIndexColumn?(e.openBlock(),e.createBlock(y,{key:1,type:"index",label:"序号",width:"70",fixed:""})):e.createCommentVNode("",!0),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(t.columns,i=>(e.openBlock(),e.createBlock(y,e.mergeProps({key:i.prop,prop:i.prop,label:i.label,width:i.width,sortable:i.sortable||!1},{ref_for:!0},i.attrs),e.createSlots({_:2},[i.slot?{name:"default",fn:e.withCtx(F=>[e.renderSlot(o.$slots,i.slot,{row:F.row})]),key:"0"}:void 0]),1040,["prop","label","width","sortable"]))),128)),t.showActionsColumn?(e.openBlock(),e.createBlock(y,{key:2,label:"操作",width:m.actionsColumnWidth},{default:e.withCtx(i=>[i.row?(e.openBlock(),e.createElementBlock("div",j,[o.$slots.actions?e.renderSlot(o.$slots,"actions",{key:0,row:i.row}):(e.openBlock(),e.createElementBlock(e.Fragment,{key:1},[e.renderSlot(o.$slots,"action-before-edit",{row:i.row}),t.showEditButton?(e.openBlock(),e.createBlock(l,{key:0,size:"small",type:"primary",link:"",onClick:F=>N("edit",i.row)},{default:e.withCtx(()=>a[9]||(a[9]=[e.createTextVNode("编辑 ")])),_:2,__:[9]},1032,["onClick"])):e.createCommentVNode("",!0),t.showDeleteButton?(e.openBlock(),e.createBlock(x,{key:1,title:"确定要删除这条数据吗?",onConfirm:F=>A([i.row.id]),"confirm-button-text":"确定","cancel-button-text":"取消",width:"200"},{reference:e.withCtx(()=>[e.createVNode(l,{size:"small",type:"danger",link:""},{default:e.withCtx(()=>a[10]||(a[10]=[e.createTextVNode("删除")])),_:1,__:[10]})]),_:2},1032,["onConfirm"])):e.createCommentVNode("",!0),e.renderSlot(o.$slots,"action-after-delete",{row:i.row})],64))])):e.createCommentVNode("",!0)]),_:3},8,["width"])):e.createCommentVNode("",!0)]),_:3},16,["data"])),[[E,n.value]]),t.showPagination&&h.value>0?(e.openBlock(),e.createElementBlock("div",q,[e.createVNode(K,{"current-page":c.pageNum,"onUpdate:currentPage":a[1]||(a[1]=i=>c.pageNum=i),"page-size":c.pageSize,"onUpdate:pageSize":a[2]||(a[2]=i=>c.pageSize=i),"page-sizes":t.pageSizes,layout:t.paginationLayout,total:h.value,background:t.paginationBackground,small:t.paginationSmall,"hide-on-single-page":t.paginationHideOnSinglePage,onSizeChange:Z,onCurrentChange:G},null,8,["current-page","page-size","page-sizes","layout","total","background","small","hide-on-single-page"])])):e.createCommentVNode("",!0),e.createVNode(X,{modelValue:r.visible,"onUpdate:modelValue":a[5]||(a[5]=i=>r.visible=i),title:R.value,width:t.dialogWidth,"destroy-on-close":!0,"custom-class":D.value},{footer:e.withCtx(()=>[e.createElementVNode("div",Q,[e.createVNode(l,{onClick:a[4]||(a[4]=i=>r.visible=!1)},{default:e.withCtx(()=>a[11]||(a[11]=[e.createTextVNode("取消")])),_:1,__:[11]}),e.createVNode(l,{type:"primary",onClick:H,loading:r.submitting},{default:e.withCtx(()=>a[12]||(a[12]=[e.createTextVNode("确定")])),_:1,__:[12]},8,["loading"])])]),default:e.withCtx(()=>[e.withDirectives((e.openBlock(),e.createElementBlock("div",null,[e.renderSlot(o.$slots,"dialog-form-content",{formData:r.data,mode:r.mode},()=>[t.dialogFormConfig.length>0?(e.openBlock(),e.createBlock(z,{key:0,"form-config":L.value,modelValue:r.data,"onUpdate:modelValue":a[3]||(a[3]=i=>r.data=i),ref:i=>r.formRef=i,rules:t.dialogFormRules,"label-width":"80px"},null,8,["form-config","modelValue","rules"])):e.createCommentVNode("",!0)])])),[[E,r.loading]])]),_:3},8,["modelValue","title","width","custom-class"])],2)}}});f.CrudTable=M,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(g,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("vue"),require("element-plus"),require("axios")):typeof define=="function"&&define.amd?define(["exports","vue","element-plus","axios"],e):(g=typeof globalThis<"u"?globalThis:g||self,e(g.ZCrudTable={},g.Vue,g.ElementPlus,g.axios))})(this,function(g,e,u,$){"use strict";const z=e.defineComponent({__name:"DynamicForm",props:{modelValue:{},formConfig:{},rules:{}},setup(m,{expose:f}){const V=e.ref(null);return f({validate:()=>{var s;return(s=V.value)==null?void 0:s.validate()}}),(s,t)=>{const S=e.resolveComponent("el-input"),D=e.resolveComponent("el-option"),C=e.resolveComponent("el-select"),U=e.resolveComponent("el-radio"),c=e.resolveComponent("el-radio-group"),B=e.resolveComponent("el-form-item"),h=e.resolveComponent("el-form");return e.openBlock(),e.createBlock(h,e.mergeProps({model:s.modelValue,rules:s.rules,ref_key:"formRef",ref:V},s.$attrs),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(s.formConfig,n=>(e.openBlock(),e.createBlock(B,{key:n.prop,label:n.label,prop:n.prop},{default:e.withCtx(()=>[n.type==="input"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:0,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),null,16,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="textarea"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:1,type:"textarea",modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),null,16,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="select"?(e.openBlock(),e.createBlock(C,e.mergeProps({key:2,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(n.options,d=>(e.openBlock(),e.createBlock(D,{key:d.value,label:d.label,value:d.value},null,8,["label","value"]))),128))]),_:2},1040,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="radio-group"?(e.openBlock(),e.createBlock(c,e.mergeProps({key:3,modelValue:s.modelValue[n.prop],"onUpdate:modelValue":d=>s.modelValue[n.prop]=d},{ref_for:!0},n.componentProps),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(n.options,d=>(e.openBlock(),e.createBlock(U,{key:d.value,label:d.value},{default:e.withCtx(()=>[e.createTextVNode(e.toDisplayString(d.label),1)]),_:2},1032,["label"]))),128))]),_:2},1040,["modelValue","onUpdate:modelValue"])):e.createCommentVNode("",!0),n.type==="input-disabled"?(e.openBlock(),e.createBlock(S,e.mergeProps({key:4,"model-value":s.modelValue[n.prop],disabled:""},{ref_for:!0},n.componentProps),null,16,["model-value"])):e.createCommentVNode("",!0)]),_:2},1032,["label","prop"]))),128))]),_:1},16,["model","rules"])}}}),y=$.create({baseURL:"",timeout:1e4});y.interceptors.request.use(m=>{const f=localStorage.getItem("token");return f&&(m.headers.Authorization="Bearer "+f),m},m=>(console.log(m),Promise.reject(m))),y.interceptors.response.use(m=>{const f=m.data;return f.code!==200?(u.ElMessage({message:f.msg||"Error",type:"error",duration:5*1e3}),(f.code===401||f.code===403)&&u.ElMessageBox.confirm("您的登录状态已失效,请重新登录","确认登出",{confirmButtonText:"重新登录",cancelButtonText:"取消",type:"warning"}).then(()=>{localStorage.removeItem("token"),location.reload()}),Promise.reject(new Error(f.msg||"Error"))):f},m=>(console.log("HTTP Error: "+m),u.ElMessage({message:"网络错误,请检查您的网络连接或联系管理员",type:"error",duration:5*1e3}),Promise.reject(m)));const T={key:0,class:"flex flex-wrap items-center justify-between gap-4 mb-6"},P={class:"flex items-center gap-x-2"},O={class:"flex items-center gap-x-3 action-buttons flex-shrink-0"},j={key:0,class:"flex items-center gap-x-2"},q={key:1,class:"flex justify-end"},M={class:"dialog-footer"},Q=e.defineComponent({__name:"CrudTable",props:{theme:{type:String,default:"default"},customClass:{type:String,default:""},apiUrlQuery:{type:String,required:!0},apiUrlDetail:{type:String,required:!0},apiUrlCreate:{type:String,required:!0},apiUrlUpdate:{type:String,required:!0},apiUrlDelete:{type:String,required:!0},showSearchSection:{type:Boolean,default:!0},showSearchActionButtons:{type:Boolean,default:!0},showNewBtn:{type:Boolean,default:!0},columns:{type:Array,default:()=>[]},onBeforeQuery:{type:Function},onAfterQuery:{type:Function},onBeforeOpenDialog:{type:Function},onAfterOpenDialog:{type:Function},onBeforeSubmit:{type:Function},onAfterSubmit:{type:Function},onBeforeDelete:{type:Function},onAfterDelete:{type:Function},showSelectionColumn:{type:Boolean,default:!0},showIndexColumn:{type:Boolean,default:!0},showActionsColumn:{type:Boolean,default:!0},showEditButton:{type:Boolean,default:!0},showDeleteButton:{type:Boolean,default:!0},actionsColumnWidth:{type:[String,Number],default:120},dialogWidth:{type:String,default:"50%"},initialSearchForm:{type:Object,default:()=>({pageNum:1,pageSize:10})},showPagination:{type:Boolean,default:!0},pageSizes:{type:Array,default:()=>[10,20,50,100]},paginationLayout:{type:String,default:"total, sizes, prev, pager, next, jumper"},paginationBackground:{type:Boolean,default:!0},paginationSmall:{type:Boolean,default:!1},paginationHideOnSinglePage:{type:Boolean,default:!1},dialogFormConfig:{type:Array,default:()=>[]},dialogFormRules:{type:Object,default:()=>({})},submitAsFormData:{type:Boolean,default:!1}},emits:["open-dialog","submit","delete"],setup(m,{expose:f,emit:V}){const s=V,t=m,S=e.computed(()=>["crud-table-wrapper",`theme-${t.theme}`,t.customClass]),D=e.computed(()=>t.theme==="large-screen"?"large-screen-dialog":""),C=(o,r)=>o?!0:(u.ElMessage.error(`${r} prop is required.`),!1),U=async(o,r)=>{try{let a={...r};t.onBeforeSubmit&&(a=await t.onBeforeSubmit(a));let p=a;if(t.submitAsFormData){const b=new FormData;for(const k in a)if(Object.prototype.hasOwnProperty.call(a,k)){const x=a[k];b.append(k,x??"")}p=b}if(l.submitting=!0,o==="add"){if(!C(t.apiUrlCreate,"apiUrlCreate"))throw new Error("apiUrlCreate is not configured.");await y.post(t.apiUrlCreate,p),u.ElMessage.success("新增成功")}else{if(!C(t.apiUrlUpdate,"apiUrlUpdate"))throw new Error("apiUrlUpdate is not configured.");await y.put(t.apiUrlUpdate,p),u.ElMessage.success("更新成功")}return t.onAfterSubmit&&t.onAfterSubmit(o,a),s("submit",{mode:o,data:a}),l.visible&&(l.visible=!1),w(),Promise.resolve()}catch(a){return console.error("Submit failed:",a),Promise.reject(a)}finally{l.submitting=!1}},c=e.reactive({pageNum:1,pageSize:10,...t.initialSearchForm}),B=e.ref([]),h=e.ref(0),n=e.ref(!1),d=e.ref([]),l=e.reactive({visible:!1,loading:!1,submitting:!1,mode:"add",data:{},formRef:null}),R=e.computed(()=>l.mode==="add"?"新增":"编辑"),L=e.computed(()=>{if(l.mode==="add")return t.dialogFormConfig.filter(r=>r.prop!=="id");const o=[...t.dialogFormConfig.filter(r=>r.prop!=="id")];return o.some(r=>r.prop==="id")||o.unshift({type:"input-disabled",prop:"id",label:"用户ID"}),o}),w=async()=>{if(C(t.apiUrlQuery,"apiUrlQuery")){n.value=!0;try{let o={...c};t.onBeforeQuery&&(o=await t.onBeforeQuery(o));const r=await y.get(t.apiUrlQuery,{params:o});if(r&&r.data&&Array.isArray(r.data.rows)&&typeof r.data.total=="number"){let a=r.data.rows;t.onAfterQuery&&(a=await t.onAfterQuery(a)),B.value=a,h.value=r.data.total}else console.warn("API response is not in the expected { data: { rows: [], total: 0 } } format."),B.value=[],h.value=0}catch(o){console.error("Fetch data failed:",o)}finally{n.value=!1}}},_=()=>{c.pageNum=1,w()},I=()=>{const{pageNum:o,pageSize:r,...a}=t.initialSearchForm;Object.keys(c).forEach(p=>{p!=="pageNum"&&p!=="pageSize"&&delete c[p]}),Object.assign(c,a),_()},W=o=>{d.value=o},N=async(o,r)=>{let a;if(o==="add"?a=r?{...r}:{role:"user"}:a={...r},t.onBeforeOpenDialog){const p=await t.onBeforeOpenDialog(o,a);p&&(a=p)}if(l.mode=o,l.visible=!0,o==="edit"){if(!C(t.apiUrlDetail,"apiUrlDetail"))return;l.loading=!0;try{const p=await y.get(t.apiUrlDetail+"/"+a.id.toString());l.data=p.data.data}finally{l.loading=!1,t.onAfterOpenDialog&&t.onAfterOpenDialog(o,l.data),s("open-dialog",{mode:o,data:l.data})}}else l.data=a,t.onAfterOpenDialog&&t.onAfterOpenDialog(o,l.data),s("open-dialog",{mode:o,data:l.data})},H=async()=>{try{l.formRef&&await l.formRef.validate();let o={...l.data};t.onBeforeSubmit&&(o=await t.onBeforeSubmit(o));let r=o;if(t.submitAsFormData){const a=new FormData;for(const p in o)if(Object.prototype.hasOwnProperty.call(o,p)){const b=o[p];a.append(p,b??"")}r=a;debugger}if(l.submitting=!0,l.mode==="add"){if(!C(t.apiUrlCreate,"apiUrlCreate"))return;await y.post(t.apiUrlCreate,r),u.ElMessage.success("新增成功")}else{if(!C(t.apiUrlUpdate,"apiUrlUpdate"))return;await y.put(t.apiUrlUpdate,r),u.ElMessage.success("更新成功")}t.onAfterSubmit&&t.onAfterSubmit(l.mode,o),s("submit",{mode:l.mode,data:o}),l.visible=!1,w()}catch(o){console.log("Submit error or validation failed:",o)}finally{l.submitting=!1}},F=async o=>{if(C(t.apiUrlDelete,"apiUrlDelete"))try{if(t.onBeforeDelete&&await t.onBeforeDelete(o)===!1)return;const r=o.join(",");await y.delete(t.apiUrlDelete+"/"+r.toString()),u.ElMessage.success("删除成功"),t.onAfterDelete&&t.onAfterDelete(o),s("delete",o),B.value.length===o.length&&c.pageNum>1&&c.pageNum--,w()}catch(r){console.error("Delete failed",r)}},Z=o=>{c.pageSize=o,_()},G=o=>{c.pageNum=o,w()};return e.onMounted(w),f({refresh:w,search:_,handleDelete:F,openDialog:N,submit:U}),(o,r)=>{const a=e.resolveComponent("el-button"),p=e.resolveComponent("el-form-item"),b=e.resolveComponent("el-form"),k=e.resolveComponent("el-table-column"),x=e.resolveComponent("el-popconfirm"),J=e.resolveComponent("el-table"),K=e.resolveComponent("el-pagination"),X=e.resolveComponent("el-dialog"),A=e.resolveDirective("loading");return e.openBlock(),e.createElementBlock("div",{class:e.normalizeClass(S.value)},[e.renderSlot(o.$slots,"header"),t.showSearchSection?(e.openBlock(),e.createElementBlock("div",T,[e.createVNode(b,{model:c,class:"query-form flex flex-nowrap items-center gap-x-4",style:{"overflow-x":"auto","padding-bottom":"8px"}},{default:e.withCtx(()=>[e.renderSlot(o.$slots,"query-conditions",{searchForm:c}),e.createVNode(p,{class:"!mr-0 flex-shrink-0"},{default:e.withCtx(()=>[e.createElementVNode("div",P,[e.renderSlot(o.$slots,"query-left"),t.showSearchActionButtons?(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},[e.createVNode(a,{type:"primary",onClick:_,loading:n.value},{default:e.withCtx(()=>r[6]||(r[6]=[e.createTextVNode("搜索")])),_:1,__:[6]},8,["loading"]),e.createVNode(a,{onClick:I},{default:e.withCtx(()=>r[7]||(r[7]=[e.createTextVNode("清空")])),_:1,__:[7]})],64)):e.createCommentVNode("",!0),e.renderSlot(o.$slots,"query-right")])]),_:3})]),_:3},8,["model"]),e.createElementVNode("div",O,[e.renderSlot(o.$slots,"action-left",{selections:d.value}),e.renderSlot(o.$slots,"add-button-content",{},()=>[t.showNewBtn?(e.openBlock(),e.createBlock(a,{key:0,type:"success",onClick:r[0]||(r[0]=i=>N("add"))},{default:e.withCtx(()=>r[8]||(r[8]=[e.createTextVNode("新增")])),_:1,__:[8]})):e.createCommentVNode("",!0)]),e.renderSlot(o.$slots,"action-right")])])):e.createCommentVNode("",!0),e.withDirectives((e.openBlock(),e.createBlock(J,e.mergeProps({data:B.value,onSelectionChange:W},o.$attrs,{style:{width:"100%","margin-bottom":"1.5rem"}}),{default:e.withCtx(()=>[t.showSelectionColumn?(e.openBlock(),e.createBlock(k,{key:0,type:"selection",width:"55",fixed:""})):e.createCommentVNode("",!0),t.showIndexColumn?(e.openBlock(),e.createBlock(k,{key:1,type:"index",label:"序号",width:"70",fixed:""})):e.createCommentVNode("",!0),(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(t.columns,i=>(e.openBlock(),e.createBlock(k,e.mergeProps({key:i.prop,prop:i.prop,label:i.label,width:i.width,sortable:i.sortable||!1},{ref_for:!0},i.attrs),e.createSlots({_:2},[i.slot?{name:"default",fn:e.withCtx(E=>[e.renderSlot(o.$slots,i.slot,{row:E.row})]),key:"0"}:void 0]),1040,["prop","label","width","sortable"]))),128)),t.showActionsColumn?(e.openBlock(),e.createBlock(k,{key:2,label:"操作",width:m.actionsColumnWidth},{default:e.withCtx(i=>[i.row?(e.openBlock(),e.createElementBlock("div",j,[o.$slots.actions?e.renderSlot(o.$slots,"actions",{key:0,row:i.row}):(e.openBlock(),e.createElementBlock(e.Fragment,{key:1},[e.renderSlot(o.$slots,"action-before-edit",{row:i.row}),t.showEditButton?(e.openBlock(),e.createBlock(a,{key:0,size:"small",type:"primary",link:"",onClick:E=>N("edit",i.row)},{default:e.withCtx(()=>r[9]||(r[9]=[e.createTextVNode("编辑 ")])),_:2,__:[9]},1032,["onClick"])):e.createCommentVNode("",!0),t.showDeleteButton?(e.openBlock(),e.createBlock(x,{key:1,title:"确定要删除这条数据吗?",onConfirm:E=>F([i.row.id]),"confirm-button-text":"确定","cancel-button-text":"取消",width:"200"},{reference:e.withCtx(()=>[e.createVNode(a,{size:"small",type:"danger",link:""},{default:e.withCtx(()=>r[10]||(r[10]=[e.createTextVNode("删除")])),_:1,__:[10]})]),_:2},1032,["onConfirm"])):e.createCommentVNode("",!0),e.renderSlot(o.$slots,"action-after-delete",{row:i.row})],64))])):e.createCommentVNode("",!0)]),_:3},8,["width"])):e.createCommentVNode("",!0)]),_:3},16,["data"])),[[A,n.value]]),t.showPagination&&h.value>0?(e.openBlock(),e.createElementBlock("div",q,[e.createVNode(K,{"current-page":c.pageNum,"onUpdate:currentPage":r[1]||(r[1]=i=>c.pageNum=i),"page-size":c.pageSize,"onUpdate:pageSize":r[2]||(r[2]=i=>c.pageSize=i),"page-sizes":t.pageSizes,layout:t.paginationLayout,total:h.value,background:t.paginationBackground,small:t.paginationSmall,"hide-on-single-page":t.paginationHideOnSinglePage,onSizeChange:Z,onCurrentChange:G},null,8,["current-page","page-size","page-sizes","layout","total","background","small","hide-on-single-page"])])):e.createCommentVNode("",!0),e.createVNode(X,{modelValue:l.visible,"onUpdate:modelValue":r[5]||(r[5]=i=>l.visible=i),title:R.value,width:t.dialogWidth,"destroy-on-close":!0,"custom-class":D.value},{footer:e.withCtx(()=>[e.createElementVNode("div",M,[e.createVNode(a,{onClick:r[4]||(r[4]=i=>l.visible=!1)},{default:e.withCtx(()=>r[11]||(r[11]=[e.createTextVNode("取消")])),_:1,__:[11]}),e.createVNode(a,{type:"primary",onClick:H,loading:l.submitting},{default:e.withCtx(()=>r[12]||(r[12]=[e.createTextVNode("确定")])),_:1,__:[12]},8,["loading"])])]),default:e.withCtx(()=>[e.withDirectives((e.openBlock(),e.createElementBlock("div",null,[e.renderSlot(o.$slots,"dialog-form-content",{formData:l.data,mode:l.mode},()=>[t.dialogFormConfig.length>0?(e.openBlock(),e.createBlock(z,{key:0,"form-config":L.value,modelValue:l.data,"onUpdate:modelValue":r[3]||(r[3]=i=>l.data=i),ref:i=>l.formRef=i,rules:t.dialogFormRules,"label-width":"80px"},null,8,["form-config","modelValue","rules"])):e.createCommentVNode("",!0)])])),[[A,l.loading]])]),_:3},8,["modelValue","title","width","custom-class"])],2)}}});g.CrudTable=Q,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|