selectDropdown.vue
2.82 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
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
const props = defineProps({
item: {
type: Object,
default: () => {},
},
carTypeList: {
type: Array,
default: () => {},
},
});
const emit = defineEmits(["update"]);
const showPicker = ref(false);
const pickerValue = ref([]);
const displayValueText = computed({
get: () => String(props.item.valueText ?? ""),
set: (v: string) => {
if (!v) {
emit("update", { value: "", valueText: "", valueKey: "" });
return;
}
emit("update", {
value: String(props.item.value ?? ""),
valueText: v,
valueKey: String(props.item.valueKey ?? ""),
});
},
});
// 页面加载时初始化数据
onMounted(() => {
initData();
});
// 初始化数据
function initData() {
pickerValue.value = [];
if (props?.item?.inspectionResultVO) {
const inspectionValueList = props?.item?.inspectionResultVO?.inspectionValueList || [];
if (inspectionValueList && inspectionValueList.length > 0 && inspectionValueList[0].valueKey) {
pickerValue.value = [inspectionValueList[0].valueKey];
}
}
onConfirm();
}
// 计算属性
const columnsList = computed(() => {
const optionsList = JSON.parse(props.item.options || "[]").map(item => {
const valueList = (item || "").split("-");
return {
text: valueList[1] || "",
value: valueList[0] || "",
};
});
return props.item.nodeName === "车型" ? props.carTypeList : optionsList;
});
function onConfirm() {
const picker = pickerValue.value && pickerValue.value.length > 0 ? pickerValue.value[0] : "";
showPicker.value = false;
const selected = columnsList.value.find((col) => col.value === picker) || {};
emit("update", {
value: selected.value || "",
valueText: selected.text || "",
valueKey: selected.value || "",
});
}
function getLabelWidth(nodeName: string) {
let widtPx = 76;
if (nodeName.length > 4) {
widtPx = widtPx + ((nodeName.length - 4) * 12);
}
return widtPx;
}
</script>
<template>
<div>
<van-field v-model="displayValueText"
clearable
is-link
:label="item.nodeName"
name="datePicker"
:placeholder="item.placeholder"
readonly
:required="item.required === 1"
@click="showPicker = true" input-align="right" :label-width="getLabelWidth(item.nodeName) + 'px'" />
<van-popup v-model:show="showPicker"
destroy-on-close
position="bottom"
teleport="#vant-portal-host">
<van-picker v-model="pickerValue"
:columns="columnsList"
:title="item.nodeName"
@cancel="showPicker = false"
@confirm="onConfirm" />
</van-popup>
</div>
</template>
<style lang="scss" scoped></style>