今天在项目中遇到一个需求,就是vue下拉树只能选择第三级选项,为了解决这个问题,查阅了官方文档
https://vue-treeselect.js.org/#disable-item-selection
然后看到isDisabled属性可以禁止选择,具体实现代码如下:
<treeselect
:disable-branch-nodes="true"
:normalizer="normalizer"
v-model="formData.goodsTypeId"
:multiple="false"
:options="goodsType"
placeholder='请选择'>
<label slot="option-label" slot-scope="{ node, shouldShowCount, count, labelClassName,
countClassName }">
{{ node.label }}
</label>
</treeselect>
如何给数据添加isDisabled属性呢?
自己尝试着在下拉树配置中添加了一下,竟然成功了
【提示】node这个对象包含的字段打印了一下,(这些数据是后台接口提供的)如下图展示:
【isLeaf】Y是叶子节点,N是分支节点【typeLevel】第?级
normalizer(node) {
//去掉children=[]的children属性
if(node.children && !node.children.length) {
delete node.children;
}
if(node.isLeaf=='Y' && node.typeLevel!=3){
node['isDisabled'] = true;
}
return {
id: node.typeId,
label: node.typeName,
children: node.children,
}
},