Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9213e0f

Browse files
committed
feat: (lesson16) the methods and emit events of designer
1 parent 055a4a8 commit 9213e0f

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

src/router/modules/formCreateDesigner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ export default {
6767
showParent: true,
6868
keepAlive: true
6969
}
70+
},
71+
{
72+
path: "/designer/lesson06",
73+
name: "Designer06",
74+
component: () => import("@/views/fd/lesson06.vue"),
75+
meta: {
76+
title: "06.事件与方法",
77+
showParent: true,
78+
keepAlive: true
79+
}
7080
}
7181
]
7282
} satisfies RouteConfigsTable;

src/views/fd/lesson06.vue

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<script setup lang="ts">
2+
import FcDesigner, { Config } from "@form-create/designer";
3+
import type { Rule, Options } from "@form-create/element-ui";
4+
import { ref, useTemplateRef, type Ref } from "vue";
5+
6+
defineOptions({
7+
name: "Designer06"
8+
});
9+
10+
const designer = useTemplateRef("designer") as Ref<
11+
InstanceType<typeof import("@form-create/designer").FcDesigner>
12+
>;
13+
const formCreate = FcDesigner.formCreate;
14+
const handleEmit = (type, data) => {
15+
console.log(type, data);
16+
};
17+
const binding = {
18+
config: {
19+
autoActive: true,
20+
fieldReadonly: false,
21+
showSaveBtn: true
22+
} satisfies Config,
23+
...[
24+
"active",
25+
"create",
26+
"copy",
27+
"delete",
28+
"drag",
29+
"inputData",
30+
"save",
31+
"clear"
32+
].reduce((prev, cur) => {
33+
const eventKey = "on" + cur[0].toUpperCase() + cur.slice(1);
34+
prev[eventKey] = (...args) => handleEmit(cur, args);
35+
return prev;
36+
}, {})
37+
};
38+
39+
// 弹窗相关
40+
const modalVisible = ref(false);
41+
const editMode = ref(false);
42+
const mode = ref("rule");
43+
const json = ref("");
44+
const handelOption = () => {
45+
modalVisible.value = true;
46+
json.value = designer.value?.getOptionsJson();
47+
};
48+
const handleRule = () => {
49+
modalVisible.value = true;
50+
json.value = designer.value?.getJson();
51+
};
52+
const handleData = () => {
53+
modalVisible.value = true;
54+
json.value = JSON.stringify(designer.value?.getFormData());
55+
};
56+
// 处理弹窗保存事件
57+
const handleSave = () => {
58+
if (mode.value === "rule") {
59+
designer.value?.setRule(formCreate.parseJson(json.value) as Rule[]);
60+
} else if (mode.value === "option") {
61+
designer.value?.setOptions(formCreate.parseJson(json.value) as Options);
62+
} else if (mode.value === "data") {
63+
designer.value?.setFormData(JSON.parse(json.value));
64+
}
65+
modalVisible.value = false;
66+
};
67+
const handleEditOption = () => {
68+
mode.value = "option";
69+
editMode.value = true;
70+
modalVisible.value = true;
71+
};
72+
const handleEditRule = () => {
73+
mode.value = "rule";
74+
editMode.value = true;
75+
modalVisible.value = true;
76+
};
77+
const handleEditData = () => {
78+
mode.value = "data";
79+
editMode.value = true;
80+
modalVisible.value = true;
81+
};
82+
83+
const handleEvent = (type: String, value: any) => {
84+
console.log(type, value);
85+
};
86+
</script>
87+
88+
<template>
89+
<div class="fd-container">
90+
<div class="flex justify-center p-2">
91+
<el-button type="primary" @click="handleEditOption">
92+
导入 Option
93+
</el-button>
94+
<el-button type="primary" @click="handleEditRule">导入 Rule</el-button>
95+
<el-button type="primary" @click="handleEditData">导入 Data</el-button>
96+
<el-button type="success" @click="handelOption">导出 Option</el-button>
97+
<el-button type="success" @click="handleRule">导出 Rule</el-button>
98+
<el-button type="success" @click="handleData">导出 Data</el-button>
99+
<el-button type="primary" plain @click="designer?.openPreview()">
100+
预览
101+
</el-button>
102+
<el-button type="danger" plain @click="designer?.clearDragRule()">
103+
清空 Rule
104+
</el-button>
105+
</div>
106+
<FcDesigner ref="designer" v-bind="binding" />
107+
<el-dialog
108+
v-model="modalVisible"
109+
title="导出JSON"
110+
width="40%"
111+
@close="() => (json = '')"
112+
>
113+
<el-input v-model="json" type="textarea" :rows="10" />
114+
<template #footer>
115+
<div class="dialog-footer">
116+
<el-button @click="handleSave">保存</el-button>
117+
</div>
118+
</template>
119+
</el-dialog>
120+
</div>
121+
</template>
122+
123+
<style lang="scss">
124+
.fd-container {
125+
display: flex;
126+
flex-direction: column;
127+
height: 100%;
128+
padding: 8px 24px;
129+
margin: 0 !important;
130+
}
131+
132+
._fd-event-title {
133+
line-height: 100%;
134+
}
135+
</style>

0 commit comments

Comments
 (0)