不同的下拉框 就会显示不同的表单,表单配置是灵活匹配的,还有就是 一定要知道都有哪些类型的数据做到兼容起来。
app.vue
<template>
<a-select v-model:value="FormDataSelect" :options="FormDataSelectList" />
<a-form class="FormView" :label-col="labelCol" v-if="FormData.BasicParam">
<template v-for="(item, index) in formList.Data[FormDataSelect]">
<div class="title" v-if="item.show">{{ item.title }}</div>
<span v-if="item.show" v-for="(itemc, indexc) in item.children">
<InputVue :key="'InputVue' + indexc" v-if="itemc.type === 'InputVue' && itemc.show" :label="itemc.name"
v-model="itemc.value" />
<SelectVue :key="'SelectVue' + indexc" v-if="itemc.type === 'SelectVue' && itemc.show" :label="itemc.name"
:dist-name="itemc.distName" v-model="itemc.value" @select="selectValue(itemc)" />
<InputNumberVue :key="'InputNumberVue' + indexc" v-if="itemc.type === 'InputNumberVue' && itemc.show"
:label="itemc.name" :unit="itemc.unit" v-model="itemc.value" />
</span>
</template>
<a-form-item class="ButtonView">
<a-button type="primary" style="margin-left:150px" @click="ShowFormData">提交</a-button>
</a-form-item>
</a-form>
<a-modal v-model:visible="visible" title="测试数据结果">
<code>
<pre>{{ Code }}</pre>
</code>
</a-modal>
</template>
<script setup>
import { nextTick, onMounted, ref, watch, reactive } from 'vue';
import { GetFormData } from './Apis.js'
import InputVue from './Input.vue';
import InputNumberVue from './InputNumber.vue';
import SelectVue from './Select.vue';
// 表单标签布局
const labelCol = ref({ style: { width: '150px', } });
// 表单数据
const FormData = ref({});
const FormDataSelect = ref('TestFormData1');
const FormDataSelectList = ref([
{ value: 'TestFormData1', label: '测试表单数据1', },
{ value: 'TestFormData2', label: '测试表单数据2', },
{ value: 'TestFormData3', label: '测试表单数据3', }
]);
let formList = reactive({ Data: {} })
function dataS(datas, key) {
if (!formList.Data.hasOwnProperty(key)) {
formList.Data[key] = []
if (datas.BasicParam) {
let BasicParam = {
title: '装备信息',
show: true,
children: [{
name: '装备名称',
key: 'BasicParam.EquipModel',
FormType: datas.BasicParam.FormType,
EquipId: datas.BasicParam.EquipId,
value: datas.BasicParam.EquipModel,
type: 'InputVue',
show: true,
}, {
name: '最大作用距离',
key: 'BasicParam.Distance',
value: datas.BasicParam.Distance,
type: 'InputNumberVue',
unit: 'km',
show: true,
}, {
name: '收发共用天线',
key: 'TranRevList[0].IsER',
value: datas.TranRevList[0].IsER,
type: 'SelectVue',
distName: '是否类型',
show: datas.TranRevList[0].TranParam != null && datas.TranRevList[0].RevParam != null,
}]
}
formList.Data[key].push(BasicParam)
}
if (datas.TranRevList.length > 0) {
if (datas.TranRevList[0].TranParam) {
let TranParam = {
title: '发射机参数',
show: true,
children: [{
name: '频率类型',
key: 'TranRevList[0].TranParam.FreqType',
value: datas.TranRevList[0].TranParam.FreqType,
type: 'SelectVue',
distName: '频率类型',
show: true,
}, {
name: '发射频率起',
key: 'TranRevList[0].TranParam.FreqFrom',
value: datas.TranRevList[0].TranParam.FreqFrom,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].TranParam.FreqType === '1' ? true : false,
}, {
name: '发射频率止',
key: 'TranRevList[0].TranParam.FreqTo',
value: datas.TranRevList[0].TranParam.FreqTo,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].TranParam.FreqType === '1' ? true : false,
}, {
name: '中心频率',
key: 'TranRevList[0].TranParam.FreqCenter',
value: datas.TranRevList[0].TranParam.FreqCenter,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].TranParam.FreqType === '0' ? true : false,
}, {
name: '占用带宽',
key: 'TranRevList[0].TranParam.OccupyBand',
value: datas.TranRevList[0].TranParam.OccupyBand,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].TranParam.FreqType === '0' ? true : false,
}]
}
formList.Data[key].push(TranParam)
}
if (datas.TranRevList[0].TranAntData) {
let TranAntData = {
title: datas.TranRevList[0].IsER === '0' ? '发射机天线参数' : '公共天线参数',
show: datas.TranRevList[0].IsER === '0' ? true : false,
children: [{
name: '天线类型',
key: 'TranRevList[0].TranAntData.AntType',
value: datas.TranRevList[0].TranAntData.AntType,
type: 'SelectVue',
distName: '天线类型',
show: true,
}, {
name: '极化方式',
key: 'TranRevList[0].TranAntData.Pola',
value: datas.TranRevList[0].TranAntData.Pola,
type: 'SelectVue',
distName: '极化方式',
show: true,
}]
}
formList.Data[key].push(TranAntData)
}
if (datas.TranRevList[0].RevParam) {
let RevParam = {
title: '接收机参数',
show: true,
children: [{
name: '频率类型',
key: 'TranRevList[0].RevParam.FreqType',
value: datas.TranRevList[0].RevParam.FreqType,
type: 'SelectVue',
distName: '频率类型',
show: true,
}, {
name: '发射频率起',
key: 'TranRevList[0].RevParam.FreqFrom',
value: datas.TranRevList[0].RevParam.FreqFrom,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].RevParam.FreqType === '1' ? true : false,
}, {
name: '发射频率止',
key: 'TranRevList[0].RevParam.FreqTo',
value: datas.TranRevList[0].RevParam.FreqTo,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].RevParam.FreqType === '1' ? true : false,
}, {
name: '接收中心频率',
key: 'TranRevList[0].RevParam.FreqCenter',
value: datas.TranRevList[0].RevParam.FreqCenter,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].RevParam.FreqType === '0' ? true : false,
}, {
name: '接收带宽',
key: 'TranRevList[0].RevParam.BandWidth',
value: datas.TranRevList[0].RevParam.BandWidth,
type: 'InputNumberVue',
unit: 'MHz',
show: datas.TranRevList[0].RevParam.FreqType === '0' ? true : false,
}]
}
formList.Data[key].push(RevParam)
}
if (datas.TranRevList[0].RevAntData) {
let RevAntData = {
title: '接收机天线参数',
show: true,
children: [{
name: '天线类型',
key: 'TranRevList[0].RevAntData.AntType',
value: datas.TranRevList[0].RevAntData.AntType,
type: 'SelectVue',
distName: '天线类型',
show: true,
}, {
name: '极化方式',
key: 'TranRevList[0].RevAntData.Pola',
value: datas.TranRevList[0].RevAntData.Pola,
type: 'SelectVue',
distName: '极化方式',
show: true,
}]
}
formList.Data[key].push(RevAntData)
}
if (datas.TranRevList[0].TranAntData) {
let TranAntData = {
title: '公共天线参数',
show: datas.TranRevList[0].IsER === '1' ? true : false,
children: [{
name: '天线类型',
key: 'TranRevList[0].TranAntData.AntType',
value: datas.TranRevList[0].TranAntData.AntType,
type: 'SelectVue',
distName: '天线类型',
show: true,
}, {
name: '极化方式',
key: 'TranRevList[0].TranAntData.Pola',
value: datas.TranRevList[0].TranAntData.Pola,
type: 'SelectVue',
distName: '极化方式',
show: true,
}]
}
formList.Data[key].push(TranAntData)
}
}
}
}
function selectValue(params) {
// 收发共用天线
if (params.key === "TranRevList[0].IsER") {
let index = formList.Data[FormDataSelect.value].findIndex(item => { return item.title === '发射机天线参数' })
let index2 = formList.Data[FormDataSelect.value].findIndex(item => { return item.title === '公共天线参数' })
let index3 = formList.Data[FormDataSelect.value].findIndex(item => { return item.title === '接收机天线参数' })
// 是1 否0
formList.Data[FormDataSelect.value][index].show = params.value === '1' ? false : true
formList.Data[FormDataSelect.value][index2].show = params.value === '1' ? true : false
formList.Data[FormDataSelect.value][index3].show = params.value === '1' ? false : true
}
let arrl = ["TranRevList[0].RevParam.FreqType", "TranRevList[0].TranParam.FreqType"]
if (arrl.includes(params.key)) {
var TranParamindex1 = null;
for (let index = 0; index < formList.Data[FormDataSelect.value].length; index++) {
const array = formList.Data[FormDataSelect.value][index].children;
for (let i = 0; i < array.length; i++) {
const element = array[i];
if (element.key === params.key) {
TranParamindex1 = index
break
}
}
}
formList.Data[FormDataSelect.value][TranParamindex1].children[1].show = params.value === '1' ? true : false
formList.Data[FormDataSelect.value][TranParamindex1].children[2].show = params.value === '1' ? true : false
formList.Data[FormDataSelect.value][TranParamindex1].children[3].show = params.value === '0' ? true : false
formList.Data[FormDataSelect.value][TranParamindex1].children[4].show = params.value === '0' ? true : false
}
}
onMounted(async () => {
FormData.value = await GetFormData(FormDataSelect.value);
dataS(FormData.value, FormDataSelect.value)
});
watch(FormDataSelect, (val) => {
FormData.value = {};
nextTick(async () => {
FormData.value = await GetFormData(val);
dataS(FormData.value, val)
})
})
const Code = ref('');
const visible = ref(false);
const ShowFormData = () => {
let formV = JSON.parse(JSON.stringify(formList.Data[FormDataSelect.value]))
for (let index = 0; index < formV.length; index++) {
const array = formV[index].children;
for (let i = 0; i < array.length; i++) {
let val = array[i].value
new Function("val", "FormData", "FormData.value." + array[i].key + " = val")(val, FormData);
}
}
Code.value = JSON.stringify(FormData.value, 1, ' ');
visible.value = true
}
</script>
<style scoped>
.ButtonView {
margin-top: 20px;
}
.FormView {
width: 500px;
}
.title {
padding: 10px;
margin-bottom: 10px;
border-bottom: 1px solid #434343;
}
:deep(.ant-form-item) {
margin-bottom: 0px;
}
</style>
Input.vue
<template>
<a-form-item :label="label">
<a-input size="small" v-model:value="InputValue">
<template #suffix>
{{ unit }}
</template>
</a-input>
</a-form-item>
</template>
<script setup>
import { ref, watch } from 'vue';
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
// 名称
label: String,
// 单位
unit: String,
// 表单数据
modelValue: String,
});
const InputValue = ref();
watch(() => props.modelValue, val => {
InputValue.value = val;
}, { immediate: true });
watch(InputValue, (val) => {
emit('update:modelValue', val);
})
</script>
<style lang="less" scoped>
</style>
InputNumber.vue
<template>
<a-form-item :label="label">
<a-input-number size="small" style="width:100%" v-model:value="InputValue" :controls="false"
:addon-after="unit" />
</a-form-item>
</template>
<script setup>
import { ref, watch } from 'vue';
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
// 名称
label: String,
// 单位
unit: String,
// 表单数据
modelValue: Number,
});
const InputValue = ref();
watch(() => props.modelValue, val => {
InputValue.value = val;
}, { immediate: true });
watch(InputValue, (val) => {
emit('update:modelValue', val);
})
</script>
<style lang="less" scoped>
</style>
Select.vue
<template>
<a-form-item :label="label">
<a-select size="small" v-model:value="SelectValue">
<a-select-option v-for="item in Dist" :key="item.DM" :value="item.DM">
{{ item.MC }}
</a-select-option>
</a-select>
</a-form-item>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue';
import { GetDist } from './Apis.js'
import { UseRateStore } from './store.js'
const store = UseRateStore();
const emit = defineEmits(['update:modelValue', 'select'])
const props = defineProps({
// 名称
label: String,
// 字典名称
distName: String || Array,
// 表单数据
modelValue: String || Number,
});
const SelectValue = ref();
watch(() => props.modelValue, val => {
SelectValue.value = val;
}, { immediate: true });
watch(SelectValue, (val) => {
emit('update:modelValue', val);
emit('select', val);
})
// 字典列表
const Dist = ref([]);
onMounted(async () => {
let item = store.getData(props.distName)
if (item) {
Dist.value = item
} else {
GetDist(props.distName).then(res => {
store.setData({ key: props.distName, value: res })
Dist.value = res
});
}
});
</script>
<style lang="less" scoped>
</style>
store.js
import {
defineStore
} from 'pinia'
export const UseRateStore = defineStore('storeDist', {
state: () => {
return {
DistData: {},
}
},
getters: {
},
actions: {
setData(data) {
this.DistData[data.key] = data.value
},
getData(key) {
return this.DistData[key] ? this.DistData[key] : null
},
}
});
Apis.js
// DMLX 代码类型
// DM 代码
// MC 名称
const Dist = {
'频率类型': [
{ "DMLX": "频率类型", "DM": "0", "MC": "频点" },
{ "DMLX": "频率类型", "DM": "1", "MC": "频段" }
],
'天线类型': [
{ "DMLX": "天线类型", "DM": "0", "MC": "全向天线" },
{ "DMLX": "天线类型", "DM": "1", "MC": "非全向天线" }
],
'极化方式': [
{ "DMLX": "极化方式", "DM": "H", "MC": "水平线极化" },
{ "DMLX": "极化方式", "DM": "V", "MC": "垂直线极化" },
{ "DMLX": "极化方式", "DM": "CL", "MC": "左旋圆极化" },
{ "DMLX": "极化方式", "DM": "CR", "MC": "右旋圆极化" },
{ "DMLX": "极化方式", "DM": "OD", "MC": "正交双线极化" }
],
'是否类型': [
{ "DMLX": "是否类型", "DM": "0", "MC": "否" },
{ "DMLX": "是否类型", "DM": "1", "MC": "是" }
],
}
const FormData = {
'TestFormData1': {
"BasicParam": {
"FormType": 10,
"EquipId": 21985,
"EquipModel": "测试装备类型1", // 装备名称
"Distance": 555.6, // 最大作用距离
},
"TranRevList": [{
"IsER": "0", // 是否公用天线
"TranParam": { // 发射机
"FreqType": "0", // 频率类型
"FreqFrom": 7.4985, // 发射频率起
"FreqTo": 7.5015, // 发射频率止
"FreqCenter": 7.5, // 中心频率
"OccupyBand": 0.003, // 占用带宽
},
"RevParam": { // 接收机
"FreqType": "0",
"FreqFrom": 7.4985, // 接收频率起
"FreqTo": 7.5015, // 接收频率止
"FreqCenter": 7.5, // 接收中心频率
"BandWidth": 0.003, // 接收带宽
},
"TranAntData": { // 发射机天线参数
"AntType": "0", // 天线类型
"Pola": "H", // 极化方式
},
"RevAntData": { // 接收机天线参数
"AntType": "0", // 天线类型
"Pola": "H",// 极化方式
}
}]
},
'TestFormData2': {
"BasicParam": {
"FormType": 20,
"EquipId": 21985,
"EquipModel": "测试装备类型2", // 装备名称
"Distance": 555.6, // 最大作用距离
},
"TranRevList": [{
"IsER": "0", // 是否公用天线
"TranParam": null,
"RevParam": { // 接收机
"FreqType": "0",
"FreqFrom": 7.4985, // 接收频率起
"FreqTo": 7.5015, // 接收频率止
"FreqCenter": 7.5, // 接收中心频率
"BandWidth": 0.003, // 接收带宽
},
"TranAntData": null,
"RevAntData": { // 接收机天线参数
"AntType": "0", // 天线类型
"Pola": "H",// 极化方式
},
}]
},
'TestFormData3': {
"BasicParam": {
"FormType": 30,
"EquipId": 21985,
"EquipModel": "测试装备类型3", // 装备名称
"Distance": 555.6, // 最大作用距离
},
"TranRevList": [{
"IsER": "0", // 是否公用天线
"TranParam": { // 发射机
"FreqType": "0", // 频率类型
"FreqFrom": 7.4985, // 发射频率起
"FreqTo": 7.5015, // 发射频率止
"FreqCenter": 7.5, // 中心频率
"OccupyBand": 0.003, // 占用带宽
},
"RevParam": null,
"TranAntData": { // 发射机天线参数
"AntType": "0", // 天线类型
"Pola": "H", // 极化方式
},
"RevAntData": null
}]
}
};
/**
* 获取表单数据
*
* @export
* @return {*}
*/
export function GetFormData(TestKey = 'TestFormData1') {
return new Promise((resolve, reject) => {
resolve(FormData[TestKey]);
});
};
/**
* 获取字典数据
*
* @export
* @param {String} DistName 字典名称
* @return {Array} 字典项
*/
export function GetDist(DistName) {
return new Promise((resolve, reject) => {
console.log('获取字典项' + DistName);
if (Dist[DistName]) {
resolve(Dist[DistName]);
} else {
console.error('字典不存在');
reject('字典不存在');
}
});
};