W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
用于從一組相關(guān)聯(lián)的數(shù)據(jù)集合中進行選擇。
通過以下方式來全局注冊組件,更多注冊方式請參考組件注冊。
import { createApp } from 'vue';
import { TreeSelect } from 'vant';
const app = createApp();
app.use(TreeSelect);
?item
? 為分類顯示所需的數(shù)據(jù),數(shù)據(jù)格式見下方示例。?main-active-index
? 表示左側(cè)高亮選項的索引,?active-id
? 表示右側(cè)高亮選項的 id。
<van-tree-select
v-model:active-id="activeId"
v-model:main-active-index="activeIndex"
:items="items"
/>
import { ref } from 'vue';
export default {
setup() {
const activeId = ref(1);
const activeIndex = ref(0);
const items = [
{
text: '浙江',
children: [
{ text: '杭州', id: 1 },
{ text: '溫州', id: 2 },
{ text: '寧波', id: 3, disabled: true },
],
},
{
text: '江蘇',
children: [
{ text: '南京', id: 4 },
{ text: '無錫', id: 5 },
{ text: '徐州', id: 6 },
],
},
{ text: '福建', disabled: true },
];
return {
items,
activeId,
activeIndex,
};
},
};
?active-id
? 為數(shù)組格式時,可以選中多個右側(cè)選項。
<van-tree-select
v-model:active-id="activeIds"
v-model:main-active-index="activeIndex"
:items="items"
/>
import { ref } from 'vue';
export default {
setup() {
const activeId = ref([1, 2]);
const activeIndex = ref(0);
const items = [
{
text: '浙江',
children: [
{ text: '杭州', id: 1 },
{ text: '溫州', id: 2 },
{ text: '寧波', id: 3, disabled: true },
],
},
{
text: '江蘇',
children: [
{ text: '南京', id: 4 },
{ text: '無錫', id: 5 },
{ text: '徐州', id: 6 },
],
},
{ text: '福建', disabled: true },
];
return {
items,
activeId,
activeIndex,
};
},
};
通過 ?content
? 插槽可以自定義右側(cè)區(qū)域的內(nèi)容。
<van-tree-select
v-model:main-active-index="activeIndex"
height="55vw"
:items="items"
>
<template #content>
<van-image
v-if="activeIndex === 0"
src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg" rel="external nofollow"
/>
<van-image
v-if="activeIndex === 1"
src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg" rel="external nofollow"
/>
</template>
</van-tree-select>
import { ref } from 'vue';
export default {
setup() {
const activeIndex = ref(0);
return {
activeIndex,
items: [{ text: '分組 1' }, { text: '分組 2' }],
};
},
};
設(shè)置 ?dot
? 屬性后,會在圖標(biāo)右上角展示一個小紅點;設(shè)置 ?badge
? 屬性后,會在圖標(biāo)右上角展示相應(yīng)的徽標(biāo)。
<van-tree-select
v-model:main-active-index="activeIndex"
height="55vw"
:items="items"
/>
import { ref } from 'vue';
export default {
setup() {
const activeIndex = ref(0);
return {
activeIndex,
items: [
{
text: '浙江',
children: [
{ text: '杭州', id: 1 },
{ text: '溫州', id: 2 },
{ text: '寧波', id: 3, disabled: true },
],
dot: true,
},
{
text: '江蘇',
children: [
{ text: '南京', id: 4 },
{ text: '無錫', id: 5 },
{ text: '徐州', id: 6 },
],
badge: 5,
},
],
};
},
};
參數(shù) | 說明 | 類型 | 默認值 |
---|---|---|---|
items | 分類顯示所需的數(shù)據(jù) | TreeSelectItem[] | []
|
height | 高度,默認單位為px
|
number | string | 300
|
main-active-index | 左側(cè)選中項的索引 | number | string | 0
|
active-id | 右側(cè)選中項的 id,支持傳入數(shù)組 | number | string | (number | string)[] |
0
|
max | 右側(cè)項最大選中個數(shù) | number | string | Infinity
|
selected-icon | 自定義右側(cè)欄選中狀態(tài)的圖標(biāo) | string | success
|
事件名 | 說明 | 回調(diào)參數(shù) |
---|---|---|
click-nav | 點擊左側(cè)導(dǎo)航時觸發(fā) | index: number |
click-item | 點擊右側(cè)選擇項時觸發(fā) | item: TreeSelectChild |
名稱 | 說明 |
---|---|
content | 自定義右側(cè)區(qū)域內(nèi)容 |
?TreeSelectItem
? 整體為一個數(shù)組,數(shù)組內(nèi)包含一系列描述分類的對象,每個分類里,?text
? 表示當(dāng)前分類的名稱,?children
? 表示分類里的可選項。
[
{
// 導(dǎo)航名稱
text: '所有城市',
// 導(dǎo)航名稱右上角徽標(biāo)
badge: 3,
// 是否在導(dǎo)航名稱右上角顯示小紅點
dot: true,
// 導(dǎo)航節(jié)點額外類名
className: 'my-class',
// 該導(dǎo)航下所有的可選項
children: [
{
// 名稱
text: '溫州',
// id,作為匹配選中狀態(tài)的標(biāo)識符
id: 1,
// 禁用選項
disabled: true,
},
{
text: '杭州',
id: 2,
},
],
},
];
組件導(dǎo)出以下類型定義:
import type { TreeSelectItem, TreeSelectChild, TreeSelectProps } from 'vant';
組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請參考 ConfigProvider 組件。
名稱 | 默認值 | 描述 |
---|---|---|
--van-tree-select-font-size | var(--van-font-size-md) | - |
--van-tree-select-nav-background | var(--van-background) | - |
--van-tree-select-content-background | var(--van-background-2) | - |
--van-tree-select-nav-item-padding | 14px var(--van-padding-sm) | - |
--van-tree-select-item-height | 48px | - |
--van-tree-select-item-active-color | var(--van-primary-color) | - |
--van-tree-select-item-disabled-color | var(--van-gray-5) | - |
--van-tree-select-item-selected-size | 16px | - |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: