Skip to content

TreeSelect - Vant 4

TreeSelect ​

Intro ​

Used to select from a set of related data sets.

Install ​

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { TreeSelect } from'vant'; const app = createApp(); app.use(TreeSelect);

Usage ​

Radio Mode ​

html
js
import { ref } from'vue'; exportdefault { setup() { const activeId = ref(1); const activeIndex = ref(0); const items = [ { text: 'Group 1', children: [ { text: 'Delaware', id: 1 }, { text: 'Florida', id: 2 }, { text: 'Georgia', id: 3, disabled: true }, ], }, { text: 'Group 2', children: [ { text: 'Alabama', id: 4 }, { text: 'Kansas', id: 5 }, { text: 'Louisiana', id: 6 }, ], }, { text: 'Group 3', disabled: true }, ]; return { items, activeId, activeIndex, }; }, };

Multiple Mode ​

html
js
import { ref } from'vue'; exportdefault { setup() { const activeIds = ref([1, 2]); const activeIndex = ref(0); const items = [ { text: 'Group 1', children: [ { text: 'Delaware', id: 1 }, { text: 'Florida', id: 2 }, { text: 'Georgia', id: 3, disabled: true }, ], }, { text: 'Group 2', children: [ { text: 'Alabama', id: 4 }, { text: 'Kansas', id: 5 }, { text: 'Louisiana', id: 6 }, ], }, { text: 'Group 3', disabled: true }, ]; return { items, activeIds, activeIndex, }; }, };

Custom Content ​

html
js
import { ref } from'vue'; exportdefault { setup() { const activeIndex = ref(0); return { activeIndex, items: [{ text: 'Group 1' }, { text: 'Group 2' }], }; }, };

Show Badge ​

html
js
import { ref } from'vue'; exportdefault { setup() { const activeIndex = ref(0); return { activeIndex, items: [ { text: 'Group 1', children: [ { text: 'Delaware', id: 1 }, { text: 'Florida', id: 2 }, { text: 'Georgia', id: 3, disabled: true }, ], dot: true, }, { text: 'Group 2', children: [ { text: 'Alabama', id: 4 }, { text: 'Kansas', id: 5 }, { text: 'Louisiana', id: 6 }, ], badge: 5, }, ], }; }, };

API ​

Props ​

AttributeDescriptionTypeDefault
v-model:main-active-indexThe index of selected parent node*numberstring*
v-model:active-idId of selected item*numberstring
itemsRequired datasets for the componentTreeSelectItem[][]
heightHeight*numberstring*
maxMaximum number of selected items*numberstring*
selected-iconSelected iconstringsuccess

Events ​

EventDescriptionArguments
click-navEmitted when parent node is selectedindex: number
click-itemEmitted when item is selecteditem: TreeSelectChild

Slots ​

NameDescriptionSlotProps
nav-text v4.1.0Custom name of the parent nodeitem: TreeSelectChild
contentCustom right content-

Data Structure of TreeSelectItem ​

TreeSelectItem should be an array contains specified tree objects.

In every tree object, text property defines id stands for the unique key while the children contains sub-tree objects.

js
[ { // name of the parent nodetext: 'Group 1', // badgebadge: 3, // Whether to show red dotdot: true, // ClassName of parent nodeclassName: 'my-class', // leaves of this parent nodechildren: [ { // name of the leaf nodetext: 'Washington', // id of the leaf node, component highlights leaf node by comparing the activeId with this.id: 1, // disable optionsdisabled: true, }, { text: 'Baltimore', id: 2, }, ], }, ];

Types ​

The component exports the following type definitions:

ts
importtype { TreeSelectItem, TreeSelectChild, TreeSelectProps } from'vant';

Theming ​

CSS Variables ​

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-tree-select-font-sizevar(--van-font-size-md)-
--van-tree-select-nav-backgroundvar(--van-background)-
--van-tree-select-content-backgroundvar(--van-background-2)-
--van-tree-select-nav-item-padding14px var(--van-padding-sm)-
--van-tree-select-item-height48px-
--van-tree-select-item-active-colorvar(--van-primary-color)-
--van-tree-select-item-disabled-colorvar(--van-gray-5)-
--van-tree-select-item-selected-size16px-

Enterprise-level mobile solution based on Vant