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è)參考,也希望大家多多支持腳本之家。
- Vue使用vant實(shí)現(xiàn)日期選擇器功能
- 關(guān)于vant的時(shí)間選擇器使用方式
- vant4 封裝日期段選擇器的實(shí)現(xiàn)
- Vue配合Vant使用時(shí)area省市區(qū)選擇器的使用方式
- Vant?Weapp組件picker選擇器初始默認(rèn)選中問題
- vant中的picker選擇器自定義選項(xiàng)內(nèi)容
- 基于Vant UI框架實(shí)現(xiàn)時(shí)間段選擇器
- Vue 用Vant實(shí)現(xiàn)時(shí)間選擇器的示例代碼
- vant實(shí)現(xiàn)自定義日期時(shí)間選擇器(年月日時(shí)分秒)
相關(guān)文章
vue和element上傳圖片以及進(jìn)行拖拽圖片排序問題
這篇文章主要介紹了vue和element上傳圖片以及進(jìn)行拖拽圖片排序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
圖文詳解vue中proto文件的函數(shù)調(diào)用
這篇文章主要給大家介紹了vue中proto文件函數(shù)調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08
Vue Element UI + OSS實(shí)現(xiàn)上傳文件功能
這篇文章主要為大家詳細(xì)介紹了Vue Element UI + OSS實(shí)現(xiàn)上傳文件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
前端實(shí)現(xiàn)不同角色登入展示不同頁(yè)面效果實(shí)例
要實(shí)現(xiàn)不同角色登錄跳轉(zhuǎn)不同的前端頁(yè)面,可以在登錄成功后,根據(jù)用戶的角色信息,使用路由跳轉(zhuǎn)到不同的頁(yè)面,這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)不同角色登入展示不同頁(yè)面效果的相關(guān)資料,需要的朋友可以參考下2024-08-08
vuex2中使用mapGetters/mapActions報(bào)錯(cuò)的解決方法
這篇文章主要介紹了vuex2中使用mapGetters/mapActions報(bào)錯(cuò)解決方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10

