index.vue
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<template>
<div
style="
margin-top: -5vh;
border: 0.5px solid gray;
margin-left: 0.1vw;
border-radius: 5px;
padding-left: 1.5vw;
"
>
<div style="margin-top: 1.2vh">
<BasicForm :showResetButton="false" :showSubmitButton="false" @register="register">
<template #add>
<div>
<template v-for="(item, index) in dynamicSNMP.communicationConfigs" :key="item">
<span style="display: none">{{ item }}</span>
<span style="display: none">{{ index }}</span>
<!-- snmp表单项 -->
<SnmpForm
:spec="item.spec"
:index="index"
:item="item"
:ref="dynamicBindRef.SnmpFormItemRef"
:mappings="item.mappings"
@removeItem="handleRemoveItem"
@selectItem="handleSelectItem"
/>
</template>
<div
v-if="dynamicSNMP.communicationConfigs.length < 4"
style="margin-left: 0vw; margin-top: 2vh"
>
<Button size="middle" type="dashed" @click="handleAdd">
<template #icon>
<PlusCircleOutlined />
</template>
Add communication config
</Button>
</div>
</div>
</template>
</BasicForm>
</div>
</div>
</template>
<script setup lang="ts">
import { UnwrapRef, reactive, ref, unref, nextTick } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { snmpSchemas } from './config';
import { PlusCircleOutlined } from '@ant-design/icons-vue';
import { Button } from 'ant-design-vue';
import SnmpForm from './cpns/SnmpForm.vue';
interface mappingsI {
dataType: string;
key: string;
oid: string;
}
interface Params {
spec: string;
queryingFrequencyMs: number;
mappings: mappingsI[];
}
const dynamicBindRef: any = {
SnmpFormItemRef: ref([]),
};
const dynamicSNMP: UnwrapRef<{ communicationConfigs: Params[] }> = reactive({
communicationConfigs: [
{
spec: '',
mappings: [
{
dataType: 'STRING',
key: '',
oid: '',
},
],
queryingFrequencyMs: 0,
},
],
});
const [register, { validate, setFieldsValue }] = useForm({
labelWidth: 150,
schemas: snmpSchemas,
actionColOptions: {
span: 14,
},
});
// const selectArray = [
// 'TELEMETRY_QUERYING',
// 'CLIENT_ATTRIBUTES_QUERYING',
// 'SHARED_ATTRIBUTES_SETTING',
// 'TO_DEVICE_RPC_REQUEST',
// ];
// const selectIndex: any = ref('');
const handleSelectItem = (e) => {
console.log('e', e);
// selectIndex.value = e || 'CLIENT_ATTRIBUTES_QUERYING';
// selectIndex.value = selectArray.filter((f) => f !== e);
// console.log('e1', selectIndex.value);
};
const handleAdd = () => {
dynamicSNMP.communicationConfigs.push({
spec: '',
mappings: [
{
dataType: 'STRING',
key: '',
oid: '',
},
],
queryingFrequencyMs: 0,
});
// console.log('总数组', dynamicSNMP.communicationConfigs);
// dynamicSNMP.communicationConfigs.forEach((snmp, index) => {
// nextTick(() => {
// unref(dynamicBindRef.SnmpFormItemRef)[index]?.handleChange(snmp?.spec);
// });
// });
};
const handleRemoveItem = (_, index) => {
dynamicSNMP.communicationConfigs.splice(index, 1);
};
//回显表单值
const setStepFieldsValueFunc = (v) => {
setFieldsValue({
timeoutMs: v.timeoutMs,
retries: v.retries,
});
dynamicSNMP.communicationConfigs = v.communicationConfigs;
dynamicSNMP.communicationConfigs.forEach((snmp: any, index: number) => {
nextTick(() => {
unref(dynamicBindRef.SnmpFormItemRef)[index]?.setFieldsValueFunc(
snmp?.spec,
snmp?.queryingFrequencyMs
);
unref(dynamicBindRef.SnmpFormItemRef)[index]?.handleMappingsChange(snmp?.mappings);
});
});
};
//获取最终的值
const getSnmpForm = async () => {
let value = await validate();
if (!value) return;
let tempObj: any = {};
let tempArray: any = [];
unref(dynamicBindRef.SnmpFormItemRef)?.map((item: any) => {
tempArray.push(item.getSnmpFormFunc());
});
tempObj = {
...value,
...{ communicationConfigs: tempArray },
...{ type: 'SNMP' },
};
return tempObj;
};
defineExpose({
getSnmpForm,
setStepFieldsValueFunc,
});
</script>
<style lang="less" scoped></style>