Vant picker選擇器設(shè)置默認(rèn)值導(dǎo)致選擇器失效的解決
Vant picker選擇器設(shè)置默認(rèn)值導(dǎo)致選擇器失效
vant 版本 >=2.12.27
說下場(chǎng)景
自定義選擇器數(shù)據(jù)結(jié)構(gòu)是數(shù)組對(duì)象,picker默認(rèn)顯示第一個(gè)或上傳選擇的項(xiàng),切換選擇器失效。
html 代碼:
<template> ? <div class="van-cell van-cell-padding0"> ? ? <van-field ? ? ? readonly ? ? ? clickable ? ? ? :value="newValue" ? ? ? :label="data.label" ? ? ? :placeholder="data.placeholder" ? ? ? :required="data.required" ? ? ? is-link ? ? ? :disabled="disabled" ? ? ? @click="clickAction" ? ? ? :error-message="errorMessage" ? ? > ? ? </van-field> ? ? <van-popup v-model="showPicker" position="bottom"> ? ? ? <van-picker ? ? ? ? :title="data.title" ? ? ? ? show-toolbar ? ? ? ? :columns="data.data" ? ? ? ? :defaultIndex="defaultIndex" ? ? ? ? @confirm="onConfirm" ? ? ? ? @cancel="showPicker = false" ? ? ? ? @change="onChange" ? ? ? /> ? ? </van-popup> ? </div> </template>
js代碼:
<script> export default { ? name: "VantPicker", ? model: { ? ? prop: "value", ? ? event: "changed", ? }, ? props: { ? ? value: { ? ? ? type: [Number, String], ? ? ? default: function () { ? ? ? ? return ""; ? ? ? }, ? ? }, ? ? data: { ? ? ? type: Object, ? ? ? default: function () { ? ? ? ? return { ? ? ? ? ? label: "下拉框", ? ? ? ? ? rules: "required", ? ? ? ? ? title: "下拉框", ? ? ? ? ? data: [], ? ? ? ? }; ? ? ? }, ? ? }, ? ? "error-message": { ? ? ? type: String, ? ? ? default: function () { ? ? ? ? return ""; ? ? ? }, ? ? }, ? }, ? data() { ? ? return { ? ? ? defaultIndex: "", ? ? ? newValue: "", ? ? ? showPicker: false, ? ? ? disabled: false, ? ? }; ? }, ? mounted() { ? ? if (this.value) { ? ? ? this.defaultIndex = this.value; ?// bug在這里 ? ? ? this.setSelectedValue(this.value); ? ? } ? ? this.disabled = this.data.disabled; ? }, ? methods: { ? ? clickAction() { ? ? ? if (!this.data.disabled && !this.disabled) { ? ? ? ? this.showPicker = true; ? ? ? } ? ? }, ? ? onConfirm(values) { ? ? ? this.newValue = values.text; ? ? ? this.$emit("changed", values.id); ? ? ? this.showPicker = false; ? ? }, ? ? onChange(picker, value) { ? ? ? console.info(picker, value); ? ? }, ? ? getCheckedItemByid(id) { ? ? ? let lists = this.data.data ? this.data.data : []; ? ? ? let item = null; ? ? ? for (let i = 0; i < lists.length; i++) { ? ? ? ? if (id == lists[i].id) { ? ? ? ? ? item = lists[i]; ? ? ? ? ? break; ? ? ? ? } ? ? ? } ? ? ? return item; ? ? }, ? ? getCheckedIndex(id) { ? ? ? let lists = this.data.data ? this.data.data : []; ? ? ? let index = null; ? ? ? for (let i = 0; i < lists.length; i++) { ? ? ? ? if (id == lists[i].id) { ? ? ? ? ? index = i; ? ? ? ? ? break; ? ? ? ? } ? ? ? } ? ? ? return index; ? ? }, ? ? setSelectedValue(newVal) { ? ? ? let checkedItem = this.getCheckedItemByid(newVal); ? ? ? ? if (checkedItem) { ? ? ? ? this.newValue = checkedItem.text; ? ? ? } else { ? ? ? ? this.$emit("changed", ""); ? ? ? ? this.newValue = ""; ? ? ? ? this.defaultIndex = ""; ? ? ? } ? ? }, ? ? setDisabled(disabled) { ? ? ? this.disabled = disabled; ? ? }, ? }, ? watch: { ? ? value(newVal) { ? ? ? if (!this.$utils.isEmpty(newVal)) { ? ? ? ? this.defaultIndex = this.getCheckedIndex(this.value); ? ? ? ? this.setSelectedValue(newVal); ? ? ? } else { ? ? ? ? this.newValue = ""; ? ? ? ? this.defaultIndex = ""; ? ? ? } ? ? }, ? }, }; </script>
問題
選擇器的在做選中,點(diǎn)擊確認(rèn)時(shí),選中的值沒有發(fā)生變化,還是未切換選擇之前的值。
問題定為
js代碼中行位置: this.defaultIndex = this.value; // bug在這里。
問題分析:這里服務(wù)器使用的key值,導(dǎo)致服務(wù)器計(jì)算picker選擇的索引index出錯(cuò),用戶選中picker值的某一項(xiàng),然后點(diǎn)擊右上角的“確認(rèn)”按鈕,picker選中的值沒有發(fā)生變化。
再看官方API defaultIndex屬性的描述:defaultIndex 初始選中項(xiàng)的索引(類型為number),默認(rèn)為 0。
Column 數(shù)據(jù)結(jié)構(gòu)
當(dāng)傳入多列數(shù)據(jù)時(shí),columns 為一個(gè)對(duì)象數(shù)組,數(shù)組中的每一個(gè)對(duì)象配置每一列,每一列有以下 key:
鍵名 | 說明 | 類型 |
---|---|---|
values | 列中對(duì)應(yīng)的備選值 | Array<string | number> |
defaultIndex | 初始選中項(xiàng)的索引,默認(rèn)為 0 | number |
className | 為對(duì)應(yīng)列添加額外的類名 | string | Array | object |
children | 級(jí)聯(lián)選項(xiàng) | Column |
解決方案
this.defaultIndex = this.value修改如下:
this.defaultIndex = this.getCheckedIndex(this.value) ?// 根據(jù)this.value循環(huán)數(shù)組找到key值選擇器中的索引值,賦值給defaultIndex,問題得到解決。
選中器colums數(shù)據(jù)結(jié)構(gòu)如下:
[ ? ? { ? ? ? ? id: "101" ? ? ? ? text: "選項(xiàng)2" ? ? },{ ? ? ? ? id: "102" ? ? ? ? text: "選項(xiàng)2" ? ? } ]
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue js IOS H5focus無法自動(dòng)彈出鍵盤的問題
今天小編就為大家分享一篇解決vue js IOS H5focus無法自動(dòng)彈出鍵盤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Vue聯(lián)動(dòng)Echarts實(shí)現(xiàn)數(shù)據(jù)大屏展示
這篇文章主要為大家介紹了Vue聯(lián)動(dòng)Echarts實(shí)現(xiàn)數(shù)據(jù)大屏的展示示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03淺談關(guān)于.vue文件中style的scoped屬性
本篇文章主要主要介紹了淺談關(guān)于.vue文件中style的scoped屬性,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08