關于VanCascader默認值(地址code轉換)
更新時間:2024年07月02日 09:11:09 作者:不是LIsa.
這篇文章主要介紹了關于VanCascader默認值(地址code轉換),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
VanCascader默認值(地址code轉換)
我在使用VanCascader的時候,發(fā)現(xiàn)頁面刷新進入時組件沒有默認值,提示的是placeholder“請輸入地址”
我希望進去時顯示我傳入的值
如:
- “湖北省武漢市洪山區(qū)”,(code:110101)
- 一般傳入的是code值,故希望兩者轉化
話不多說上代碼:
<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>接下來就是運行結果圖:

點擊編輯,進入地址編輯頁面,
如下圖所示:

有默認值:
- 北京市/北京市/西城區(qū)了。
代碼說明:
- 使用findOptionWithParents()獲取你傳入的modelValue值(如:”110101“)
- 獲取你所需要得到的option選項(’東城區(qū)‘)
- 根據(jù)option獲取父級(’北京市‘)
- 父級的父級(’北京市‘),再根據(jù)join()拼接
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue 3 中使用 Element Plus 的 `el-t
在 Vue 3 中使用 Element Plus 的 `el-table` 組件實現(xiàn)自適應高度,你可以根據(jù)容器的高度動態(tài)設置表格的高度,下面通過示例代碼給大家展示,感興趣的朋友一起看看吧2024-12-12
vue+echarts實現(xiàn)動態(tài)折線圖的方法與注意
這篇文章主要給大家介紹了關于vue+echarts實現(xiàn)動態(tài)折線圖的方法與注意事項,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
vue+element實現(xiàn)動態(tài)加載表單
這篇文章主要為大家詳細介紹了vue+element實現(xiàn)動態(tài)加載表單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12
vue中el-tree結合el-switch實現(xiàn)開關狀態(tài)切換
本文主要介紹了vue中el-tree結合el-switch實現(xiàn)開關狀態(tài)切換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12

