van-tree-select三级选择
时间: 2023-11-03 18:02:14 浏览: 122
r-tree入门简单介绍
4星 · 用户满意度95%
van-tree-select 是一个基于 Vue.js 的树形选择器组件。要实现三级选择,需要在 van-tree-select 的 data 中设置三层数据,例如:
```javascript
data: [
{
id: 1,
label: '一级选项1',
children: [
{
id: 11,
label: '二级选项1-1',
children: [
{
id: 111,
label: '三级选项1-1-1'
},
{
id: 112,
label: '三级选项1-1-2'
}
]
},
{
id: 12,
label: '二级选项1-2',
children: [
{
id: 121,
label: '三级选项1-2-1'
},
{
id: 122,
label: '三级选项1-2-2'
}
]
}
]
},
{
id: 2,
label: '一级选项2',
children: [
{
id: 21,
label: '二级选项2-1',
children: [
{
id: 211,
label: '三级选项2-1-1'
},
{
id: 212,
label: '三级选项2-1-2'
}
]
},
{
id: 22,
label: '二级选项2-2',
children: [
{
id: 221,
label: '三级选项2-2-1'
},
{
id: 222,
label: '三级选项2-2-2'
}
]
}
]
}
]
```
然后在 van-tree-select 的 props 中设置 `props` 属性,指定数据中的 id、label 和 children 属性名:
```javascript
props: {
value: 'id',
label: 'label',
children: 'children'
}
```
最后,将 van-tree-select 放入父组件中,并监听 `change` 事件获取选中的三级选项:
```html
<van-tree-select
:data="data"
:props="{ value: 'id', label: 'label', children: 'children' }"
@change="handleTreeSelectChange"
></van-tree-select>
```
```javascript
methods: {
handleTreeSelectChange(selectedValues) {
const [firstLevel, secondLevel, thirdLevel] = selectedValues;
console.log('一级选项:', firstLevel);
console.log('二级选项:', secondLevel);
console.log('三级选项:', thirdLevel);
}
}
```
阅读全文