elementui?Select選擇器嵌套tree實(shí)現(xiàn)TreeSelect方式
個(gè)人實(shí)現(xiàn)記錄
- 效果 可以設(shè)置默認(rèn)要展開和勾選的狀態(tài)
- 點(diǎn)擊select里的標(biāo)簽 關(guān)閉樹形圖對應(yīng)的取消勾選效果

html
<el-select v-model="value1" multiple placeholder="請選擇" @change="changeData">
<el-option style="height:auto" :value="SelectedArray">
<el-tree
:data="dataList"
show-checkbox
node-key="id"
ref="tree"
:check-strictly="true"
highlight-current
:props="defaultProps"
@check="checkClick"
:default-expanded-keys="setkey"
:default-expand-all="false"
>
</el-tree>
</el-option>
</el-select>default-expand-all是否默認(rèn)展開所有節(jié)點(diǎn)default-expanded-keys默認(rèn)展開的節(jié)點(diǎn)的 key 的數(shù)組check當(dāng)復(fù)選框被點(diǎn)擊的時(shí)候觸發(fā)-check-strictly父子節(jié)點(diǎn)不相互關(guān)聯(lián)highlight-current是否高亮當(dāng)前選中節(jié)點(diǎn),默認(rèn)值是 false。
代碼部分
data () {
return {
setkey: [1], // 默認(rèn)展開節(jié)點(diǎn)
dateList: [], // 遍歷用
SelectedArray: [12, 13], // 選中的數(shù)組
dataList: [
{
id: 1,
name: '總公司',
parent_id: null,
parent_name: null,
has_children: true,
children: [
{
id: 2,
name: '1xxxx部門',
parent_id: 1,
parent_name: '總公司',
has_children: true,
children: [
{
id: 12,
name: 'x1x項(xiàng)目',
parent_id: 1,
parent_name: '1xxxx部門',
has_children: false,
children: []
},
{
id: 13,
name: 'x22222x項(xiàng)目',
parent_id: 2,
parent_name: '1xxxx部門',
has_children: true,
children: [
{
id: 19,
name: 'xxx',
parent_id: 13,
parent_name: 'x22222x項(xiàng)目',
has_children: false,
children: []
}
]
}
]
},
{
id: 15,
name: '管理辦公室',
parent_id: 1,
parent_name: '總公司',
has_children: false,
children: []
},
{
id: 16,
name: '技術(shù)中心',
parent_id: 1,
parent_name: '總公司',
has_children: false,
children: []
}
]
}
], // tree數(shù)據(jù)
value1: [], // select綁定的值
// 對應(yīng)的字段
defaultProps: {
children: 'children',
label: 'name'
}
}
},<script>
// highlight-current是否高亮當(dāng)前選中節(jié)點(diǎn),默認(rèn)值是 false。
methods: {
async getTreeData () {
try {
const {
data: { data }
} = await this.$http.get('http://localhost:8848/treeData')
console.log(data)
this.dataList = data.data_list
console.log(this.dataList)
} catch (error) {
console.log(error)
}
},
changeData (e) {
console.log('選中改變的值', e, this.dateList)
const setkey = []
for (let index = 0; index < this.dateList.length; index++) {
for (let index1 = 0; index1 < e.length; index1++) {
if (this.dateList[index].name === e[index1]) {
setkey.push(this.dateList[index].id)
}
}
}
console.log(setkey)
this.setkey = setkey
// 重新 設(shè)置tree
this.$refs.tree.setCheckedKeys(this.setkey)
},
// 點(diǎn)擊樹形圖復(fù)選框觸發(fā)
checkClick (checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys) {
// console.log(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys)
// 點(diǎn)擊了復(fù)選框 使用this.$refs.tree.getCheckedNodes獲取當(dāng)前選中的節(jié)點(diǎn)
const res = this.$refs.tree.getCheckedNodes(false, true) // 這里兩個(gè)true,1. 是否只是葉子節(jié)點(diǎn) 2. 是否包含半選節(jié)點(diǎn)(就是使得選擇的時(shí)候不包含父節(jié)點(diǎn))
console.log('點(diǎn)擊樹形圖獲取當(dāng)前選中的節(jié)點(diǎn)', res)
this.dateList = res
const labelArr = []
const valueArr = []
res.forEach((element, index) => {
labelArr.push(element.name)
valueArr.push(element.id)
})
this.value1 = labelArr // select顯示的數(shù)據(jù)
this.SelectedArray = valueArr // 我要發(fā)送給后端的數(shù)據(jù)
console.log(this.value1, this.SelectedArray)
}
},
created () {
//獲取tree數(shù)據(jù) data里面寫了
// this.getTreeData()
},
// 默認(rèn)選中樹形圖的復(fù)選框 回顯的操作
mounted () {
this.$refs.tree.setCheckedKeys(this.setkey)
}
}
</script>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+element UI中如何給指定日期添加標(biāo)記
這篇文章主要介紹了vue+element UI中如何給指定日期添加標(biāo)記問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
在Vue中獲取自定義屬性方法:data-id的實(shí)例
這篇文章主要介紹了在Vue中獲取自定義屬性方法:data-id的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue利用vue meta info設(shè)置每個(gè)頁面的title與meta信息
這篇文章主要給大家介紹了關(guān)于vue如何利用vue meta info設(shè)置每個(gè)頁面的title與meta信息的相關(guān)資料,文中將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-10-10
vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
這篇文章主要介紹了vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
解決vue項(xiàng)目,npm run build后,報(bào)路徑錯(cuò)的問題
這篇文章主要介紹了解決vue項(xiàng)目,npm run build后,報(bào)路徑錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue完美實(shí)現(xiàn)el-table列寬自適應(yīng)
這篇文章主要介紹了vue完美實(shí)現(xiàn)el-table列寬自適應(yīng),對vue感興趣的同學(xué),可以參考下2021-05-05
el-tree的實(shí)現(xiàn)葉子節(jié)點(diǎn)單選的示例代碼
本文主要介紹了el-tree的實(shí)現(xiàn)葉子節(jié)點(diǎn)單選的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化詳解
這篇文章主要給大家介紹了關(guān)于vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化,在Vue.js中可以通過watch監(jiān)聽屬性變化并動(dòng)態(tài)修改其他屬性的值,watch通過監(jiān)聽器函數(shù)接收新舊值,實(shí)現(xiàn)屬性間的數(shù)據(jù)聯(lián)動(dòng),需要的朋友可以參考下2024-12-12
Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-04-04

