MappingsForm.vue
4.94 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<template>
<div
v-for="(param, index) in dynamicInput.params"
:key="index"
style="display: flex; margin-top: 0.25vh"
>
<a-input
:disabled="true"
placeholder="设备"
v-model:value="param.key"
style="width: 38%; margin-bottom: 5px; margin-left: 1vh"
@change="emitChange"
/>
<Select
placeholder="请选择设备属性"
v-model:value="param.dataType"
style="width: 160px; margin-left: 1.8vw"
:options="selectOptions"
@change="emitChange"
/>
<MinusCircleOutlined
v-if="dynamicInput.params.length > min"
class="dynamic-delete-button"
@click="remove(param)"
style="width: 50px; display: none"
/>
</div>
<div style="display: none">
<a-button type="text" style="width: 28%; margin-top: 0.25vh" @click="add">
<PlusCircleOutlined />
Add mapping
</a-button>
</div>
</template>
<script lang="ts">
import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';
import { defineComponent, reactive, UnwrapRef, watchEffect, ref } from 'vue';
import { propTypes } from '/@/utils/propTypes';
import { SelectTypes } from 'ant-design-vue/es/select';
import { Select } from 'ant-design-vue';
import { screenLinkPageByDeptIdGetDevice } from '/@/api/ruleengine/ruleengineApi';
import { getAttribute } from '/@/api/ruleengine/ruleengineApi';
interface Params {
[x: string]: string;
dataType: string;
key: string;
}
export default defineComponent({
name: 'JAddInput',
components: {
MinusCircleOutlined,
PlusCircleOutlined,
Select,
},
//--------------不继承Antd Design Vue Input的所有属性 否则控制台报大片警告--------------
inheritAttrs: false,
props: {
value: propTypes.array.def([]),
//自定义删除按钮多少才会显示
min: propTypes.integer.def(0),
orgId: propTypes.string.def(''),
},
emits: ['change', 'update:value', 'dynamicReduceHeight', 'dynamicAddHeight'],
setup(props, { emit }) {
const selectOptions = ref<SelectTypes['options']>([]);
//input动态数据
const dynamicInput: UnwrapRef<{ params: Params[] }> = reactive({ params: [] });
//删除Input
const remove = (item: Params) => {
let index = dynamicInput.params.indexOf(item);
if (index !== -1) {
dynamicInput.params.splice(index, 1);
}
emitChange();
emit('dynamicReduceHeight');
};
//新增Input
const add = () => {
dynamicInput.params.push({
dataType: 'STRING',
key: '',
});
emitChange();
emit('dynamicAddHeight');
};
//监听传入数据value
watchEffect(() => {
initVal();
});
/**
* 初始化数值
*/
async function initVal() {
dynamicInput.params = [];
if (props.value) {
if (props.orgId) {
const res = await getAttribute(props.orgId, props.value.join(','));
selectOptions.value = res.map((o) => {
return {
label: o,
value: o,
};
});
const { items } = await screenLinkPageByDeptIdGetDevice({
organizationId: props.orgId,
});
const options = items.map((item) => {
return {
label: item.name,
value: item.tbDeviceId,
};
});
const temp: any = [];
options.forEach((f) => {
props.value.forEach((f1) => {
if (f1 == f.value) {
temp.push({
label: f.label,
value: f.value,
});
}
});
});
let jsonObj = temp;
jsonObj.forEach((item: Params) => {
dynamicInput.params.push({
dataType: item.dataType,
key: item.label,
value: item.value,
});
});
}
}
}
/**
* 数值改变
*/
function emitChange() {
let obj: any = [];
if (dynamicInput.params.length > 0) {
dynamicInput.params.forEach((item: Params) => {
obj.push({
attr: item.dataType,
tbDeviceId: item.value,
});
});
}
console.log('数值改变', obj);
emit('change', obj);
emit('update:value', obj);
}
return {
dynamicInput,
emitChange,
remove,
add,
selectOptions,
};
},
});
</script>
<style scoped>
.dynamic-delete-button {
cursor: pointer;
position: relative;
top: 4px;
font-size: 24px;
color: #999;
transition: all 0.3s;
}
.dynamic-delete-button:hover {
color: #777;
}
.dynamic-delete-button[disabled] {
cursor: not-allowed;
opacity: 0.5;
}
</style>