在遇到 省市区多级联动数据的时候,经常会用到的就是 a-cascader
级联选择器。
options
数据结构是 对象数组嵌套——value/label/children
数据源结构如下:
var options = [
{
value: 'zhejiang',
label: '浙江',
children: [
{
value: 'hangzhou',
label: '杭州',
children: [
{
value: 'xihu',
label: '西湖',
},
],
},
],
},
]
change-on-select
改为任意选中一级:show-search="{ filter }"
来实现检索筛选功能<a-cascader
:options="options"
:show-search="{ filter }"
placeholder="Please select"
@change="onChange"
/>
过滤方法如下:
filter(inputValue, path) {
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
},
value/label/children
,可以通过fieldNames
来指定其他参数<a-cascader
:field-names="{ label: 'name', value: 'code', children: 'items' }"
:options="options"
placeholder="Please select"
@change="onChange"
/>
此时支持的数据源可以是下面的形式了
var options = [
{
code: 'zhejiang',
name: '浙江',
items: [
{
code: 'hangzhou',
name: '杭州',
items: [
{
code: 'xihu',
name: '西湖',
},
],
},
],
},
]
treeData
数据结构是 对象数组嵌套——title/value/key/children
<a-tree-select
v-model="value"
style="width: 100%"
:tree-data="treeData"
show-search
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
placeholder="Please select"
allow-clear
tree-default-expand-all
/>
数据源结构如下:
var treeData = [
{
title: 'Node1',
value: '0-0',
key: '0-0',
children: [
{
title: 'Child Node1',
value: '0-0-0',
key: '0-0-0',
},
],
},
]
multiple
多选,则绑定的值是一个字符串的数组 <a-tree-select
show-search
style="width: 100%"
:value="value"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
placeholder="Please select"
:tree-data="treeData"
allow-clear
multiple
tree-default-expand-all
/>
:show-search="{ filter }"
来实现检索筛选功能<a-cascader
:options="options"
:show-search="{ filter }"
placeholder="Please select"
@change="onChange"
/>
过滤方法如下:
filter(inputValue, path) {
return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
},
完成!!!
最后附上elementUI
中的级联选择器的代码:
<el-cascader v-model="shelfSelect" clearable size="mini" :show-all-levels="true" style="width:200px;"
filterable
placeholder="全部货架"
v-bind:options="shelfOptions"
v-bind:props="{ expandTrigger: 'click',checkStrictly: true }">
<template slot-scope="{ node, data }">
<div>
<i v-if="data.children" class="el-icon-map-location"></i>
<i v-else class="el-icon-notebook-2"></i>
<span>{{ data.label }}</span>
</div>
</template>
</el-cascader>