關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)
VanCascader默認(rèn)值(地址code轉(zhuǎn)換)
我在使用VanCascader的時(shí)候,發(fā)現(xiàn)頁(yè)面刷新進(jìn)入時(shí)組件沒(méi)有默認(rèn)值,提示的是placeholder“請(qǐng)輸入地址”
我希望進(jìn)去時(shí)顯示我傳入的值
如:
- “湖北省武漢市洪山區(qū)”,(code:110101)
- 一般傳入的是code值,故希望兩者轉(zhuǎn)化
話不多說(shuō)上代碼:
<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ǐng)選擇所在地區(qū)" :options="options" @close="show = false" @finish="onFinish" /> </van-popup> </template>
接下來(lái)就是運(yùn)行結(jié)果圖:
點(diǎn)擊編輯,進(jìn)入地址編輯頁(yè)面,
如下圖所示:
有默認(rèn)值:
- 北京市/北京市/西城區(qū)了。
代碼說(shuō)明:
- 使用findOptionWithParents()獲取你傳入的modelValue值(如:”110101“)
- 獲取你所需要得到的option選項(xiàng)(’東城區(qū)‘)
- 根據(jù)option獲取父級(jí)(’北京市‘)
- 父級(jí)的父級(jí)(’北京市‘),再根據(jù)join()拼接
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Antd表格滾動(dòng) 寬度自適應(yīng) 不換行的實(shí)例
這篇文章主要介紹了Antd表格滾動(dòng) 寬度自適應(yīng) 不換行的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue CLI4 Vue.config.js標(biāo)準(zhǔn)配置(最全注釋)
這篇文章主要介紹了Vue CLI4 Vue.config.js標(biāo)準(zhǔn)配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06vue實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)雙向綁定
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)雙向綁定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04前端實(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-08Vue如何使用patch-package優(yōu)雅地修改第三方依賴(lài)庫(kù)
在前端開(kāi)發(fā)中,有時(shí)我們需要對(duì)第三方依賴(lài)庫(kù)進(jìn)行修改以滿足項(xiàng)目需求,patch-package 是一個(gè)優(yōu)秀的工具,可以幫助我們優(yōu)雅地管理這些修改,下面我們來(lái)看看具體操作吧2025-03-03從Echarts報(bào)錯(cuò)中學(xué)習(xí)Vue3?ref和shallowRef區(qū)別及其組件二次封裝demo
這篇文章主要介紹了從Echarts報(bào)錯(cuò)中學(xué)習(xí)Vue3?ref和shallowRef區(qū)別及其組件二次封裝demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Vue3訪問(wèn)頁(yè)面時(shí)自動(dòng)獲取數(shù)據(jù)的方法實(shí)現(xiàn)
本文介紹了在Vue3中如何利用生命周期鉤子函數(shù)和定時(shí)器實(shí)現(xiàn)訪問(wèn)頁(yè)面時(shí)自動(dòng)獲取數(shù)據(jù)的方法,這種方法適用于需要在頁(yè)面加載時(shí)即時(shí)更新數(shù)據(jù)顯示的場(chǎng)景,感興趣的可以了解一下2024-11-11