關(guān)于VanCascader默認值(地址code轉(zhuǎn)換)
更新時間:2024年07月02日 09:11:09 作者:不是LIsa.
這篇文章主要介紹了關(guān)于VanCascader默認值(地址code轉(zhuǎn)換),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
VanCascader默認值(地址code轉(zhuǎn)換)
我在使用VanCascader的時候,發(fā)現(xiàn)頁面刷新進入時組件沒有默認值,提示的是placeholder“請輸入地址”
我希望進去時顯示我傳入的值
如:
- “湖北省武漢市洪山區(qū)”,(code:110101)
- 一般傳入的是code值,故希望兩者轉(zhuǎn)化
話不多說上代碼:
<script lang="ts" setup> import { useCascaderAreaData } from '@vant/area-data' import type { CascaderOption } from 'vant' const props = defineProps<{ modelValue: string }>() const emit = defineEmits<{ (e: 'update:modelValue', v: string): void }>() const show = ref(false) interface OptionWithParents { option: CascaderOption | null parent: CascaderOption | null grandparent: CascaderOption | null } function findOptionWithParents(options: CascaderOption[], targetValue: string, parent: CascaderOption | null, grandparent: CascaderOption | null): OptionWithParents | null { for (const option of options) { if (option.value === targetValue) return { option, parent, grandparent } if (option.children && option.children.length > 0) { const result = findOptionWithParents(option.children, targetValue, option, parent) if (result) return result } } return null } function getLabelByValue(options: CascaderOption[], targetValue: string) { const { option, parent, grandparent } = findOptionWithParents(options, targetValue, null, null) if (option && parent && grandparent) return [grandparent.text, parent.text, option.text].join('/') return '' } const fieldValue = ref(props.modelValue) const options = useCascaderAreaData() function onFinish({ selectedOptions, value }: { selectedOptions: CascaderOption[]; value: string }) { show.value = false fieldValue.value = selectedOptions.map(option => option.text).join('/') emit('update:modelValue', value) } nextTick(() => { fieldValue.value = getLabelByValue(options, props.modelValue) }) </script>
<template> <van-field :model-value="fieldValue" is-link readonly label="所在地區(qū)" placeholder="選擇地區(qū)" @click="show = true" /> <van-popup v-model:show="show" round position="bottom"> <van-cascader :model-value="modelValue" title="請選擇所在地區(qū)" :options="options" @close="show = false" @finish="onFinish" /> </van-popup> </template>
接下來就是運行結(jié)果圖:
點擊編輯,進入地址編輯頁面,
如下圖所示:
有默認值:
- 北京市/北京市/西城區(qū)了。
代碼說明:
- 使用findOptionWithParents()獲取你傳入的modelValue值(如:”110101“)
- 獲取你所需要得到的option選項(’東城區(qū)‘)
- 根據(jù)option獲取父級(’北京市‘)
- 父級的父級(’北京市‘),再根據(jù)join()拼接
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue CLI4 Vue.config.js標準配置(最全注釋)
這篇文章主要介紹了Vue CLI4 Vue.config.js標準配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06Vue如何使用patch-package優(yōu)雅地修改第三方依賴庫
在前端開發(fā)中,有時我們需要對第三方依賴庫進行修改以滿足項目需求,patch-package 是一個優(yōu)秀的工具,可以幫助我們優(yōu)雅地管理這些修改,下面我們來看看具體操作吧2025-03-03從Echarts報錯中學習Vue3?ref和shallowRef區(qū)別及其組件二次封裝demo
這篇文章主要介紹了從Echarts報錯中學習Vue3?ref和shallowRef區(qū)別及其組件二次封裝demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Vue3訪問頁面時自動獲取數(shù)據(jù)的方法實現(xiàn)
本文介紹了在Vue3中如何利用生命周期鉤子函數(shù)和定時器實現(xiàn)訪問頁面時自動獲取數(shù)據(jù)的方法,這種方法適用于需要在頁面加載時即時更新數(shù)據(jù)顯示的場景,感興趣的可以了解一下2024-11-11