vue+elementUI組件tree如何實現單選加條件禁用
vue+elementUI組件tree單選加條件禁用
elementUI tree:http://element.eleme.io/#/zh-CN/component/tree
官方給出的API還是挺全的,但是示例木有給全,有幾個API的說明也不是很能理解~
tree實現帶選擇框的要加上node-key="id" show-checkbox兩個屬性,
<el-tree ref="tree2" :data="data2" :props="defaultProps" class="filter-tree" node-key="id" show-checkbox />
但是要單選呢?!組件默認如果選中了父節(jié)點,相應的子節(jié)點也會被選中,要切斷關聯,那就要加check-strictly這個屬性,默認是false,props形式傳入設置成true即可~

組件還提供了設置勾選的節(jié)點的方法setCheckedKeys,參數接收節(jié)點的key,已數組的形式傳入

this.$refs.tree2.setCheckedKeys([3]);
有個設置的方法,還需要一個觸發(fā)的事件以及當前選中的節(jié)點數據,剛好API有個check事件,啊!瘋狂點贊中~

此方法會傳遞兩個參數,node是當前節(jié)點的數據,data選中狀態(tài)的詳情數據,使用如下:
<el-tree ref="tree2" :data="data2" :props="defaultProps" node-key="id" show-checkbox :check-strictly="true" @check="checkFn" />
checkFn(node, data) {
console.log(11111, node, data);
let checkedKeys = data.checkedKeys;
let currKey = node.id;
this.$refs.tree2.setCheckedKeys([currKey]);
}使用如上方法就可以完成實現單選
禁用的話,組件提供了props設置disabled,
![]()
可以直接以布爾形式設置,也可以函數返回值的形式設置,下面給出兩種設置方法的代碼~
1. 以布爾形式設置,需要在節(jié)點數據里加上disabled屬性;
<el-tree
:data="data3"
show-checkbox
node-key="id"
:default-expanded-keys="[2, 3]"
:default-checked-keys="[5]">
</el-tree>
<script>
export default {
data() {
return {
data3: [{
id: 1,
label: '一級 2',
children: [{
id: 3,
label: '二級 2-1',
children: [{
id: 4,
label: '三級 3-1-1'
}, {
id: 5,
label: '三級 3-1-2',
disabled: true
}]
}, {
id: 2,
label: '二級 2-2',
disabled: true,
children: [{
id: 6,
label: '三級 3-2-1'
}, {
id: 7,
label: '三級 3-2-2',
disabled: true
}]
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
</script>2. 以函數返回值的形式設置,disabled函數會返回當前節(jié)點的數據,return一個狀態(tài)回去就OK~
<el-tree
:data="data3"
show-checkbox
node-key="id"
:default-expanded-keys="[2, 3]"
:default-checked-keys="[5]">
</el-tree>
<script>
export default {
data() {
return {
data3: [],
defaultProps: {
children: 'children',
label: 'label',
disabled: this.disabledFn,
}
};
},
methods: {
/**
* disabled函數
*/
disabledFn(data, node) {
// console.log("disabled函數: ", data, node);
if (!data.isAggregation) { // 根據自己的節(jié)點數據條件設置判斷,我只提供了個參考
return true;
} else {
return false;
}
},
}
};
</script>完結!付上一個完成的代碼~
<template>
<div class="app-container">
<el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;" />
<el-tree ref="tree2" :data="data2" :props="defaultProps" :filter-node-method="filterNode" class="filter-tree" default-expand-all node-key="id" show-checkbox :check-strictly="true" @check="checkFn" />
</div>
</template>
<script>
export default {
data() {
return {
filterText: "",
data2: [
{
id: 1,
label: "Level one 1",
disabled: true,
children: [
{
id: 4,
label: "Level two 1-1",
children: [
{
id: 9,
label: "Level three 1-1-1"
},
{
id: 10,
label: "Level three 1-1-2"
}
]
}
]
},
{
id: 2,
label: "Level one 2",
children: [
{
id: 5,
label: "Level two 2-1"
},
{
id: 6,
label: "Level two 2-2"
}
]
},
{
id: 3,
label: "Level one 3",
children: [
{
id: 7,
label: "Level two 3-1"
},
{
id: 8,
label: "Level two 3-2"
}
]
}
],
defaultProps: {
children: "children",
label: "label",
}
};
},
watch: {
filterText(val) {
this.$refs.tree2.filter(val);
}
},
methods: {
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
},
checkFn(node, data) {
console.log(11111, node, data);
let checkedKeys = data.checkedKeys;
let currKey = node.id;
this.$refs.tree2.setCheckedKeys([currKey]);
}
}
};
</script>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于element-ui中el-form自定義驗證(調用后端接口)
這篇文章主要介紹了關于element-ui中el-form自定義驗證(調用后端接口),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

