vue treeselect獲取當(dāng)前選中項(xiàng)的label實(shí)例
我就廢話不多說了,大家還是直接看代碼吧~
<treeselect :placeholder="$t('taskManage.lockTask.selDeptId')"
:options="deptTree"
:normalizer="normalizer"
v-model="formData.deptId"
@select="selectDepart">
</treeselect>
// 獲取當(dāng)前選中部門的名稱
selectDepart(val) {
console.log('selectDepart', val)
this.formData.deptName = val.name
}
結(jié)果如下所示,可以獲取到當(dāng)前選中項(xiàng)的信息:

補(bǔ)充知識(shí):vue中element-ui 樹形控件-樹節(jié)點(diǎn)的選擇(選中當(dāng)前節(jié)點(diǎn),獲取當(dāng)前id并且獲取其父級(jí)id)
Element-ui官網(wǎng)給的方法
getCheckedKeys() { console.log(this.$refs.tree.getCheckedKeys()); },
這種只有在所有子級(jí)都被選中的情況下才能獲得父級(jí)的id,如果不選中所有的子級(jí)那么獲取得到的id就只有子級(jí)的。但是一般提交數(shù)據(jù)時(shí)后臺(tái)都需要父級(jí)id的。
有兩種方法解決:
1 ,找到項(xiàng)目中的\node_modules\element-ui\lib\element-ui.common.js文件
2,搜索文件中的TreeStore.prototype.getCheckedNodes方法中的
if (child.checked && (!leafOnly || leafOnly && child.isLeaf)) {
checkedNodes.push(child.data);
}
3,修改為
if ((child.checked || child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) {
checkedNodes.push(child.data);
}
4,然后重啟項(xiàng)目
console.log(this.$refs.tree.getCheckedKeys());就可以拿到父節(jié)點(diǎn)的ID啦
第二種方法:復(fù)制代碼
代碼:要有pid:xxx
methods: {
getCheckedNodes() {
var rad=''
var ridsa = this.$refs.tree.getCheckedKeys().join(',')// 獲取當(dāng)前的選中的數(shù)據(jù)[數(shù)組] -id, 把數(shù)組轉(zhuǎn)換成字符串
var ridsb = this.$refs.tree.getCheckedNodes()// 獲取當(dāng)前的選中的數(shù)據(jù){對(duì)象}
ridsb.forEach(ids=>{//獲取選中的所有的父級(jí)id
rad+=','+ids.pid
})
rad=rad.substr(1) // 刪除字符串前面的','
var rids=rad+','+ridsa
var arr=rids.split(',')// 把字符串轉(zhuǎn)換成數(shù)組
arr=[...new Set(arr)]; // 數(shù)組去重
rids=arr.join(',')// 把數(shù)組轉(zhuǎn)換成字符串
console.log(rids)
}
}
測試代碼
<template>
<div>
<el-tree
:data="data2"
show-checkbox
default-expand-all
node-key="id"
ref="tree"
highlight-current
:props="defaultProps">
</el-tree>
<div class="buttons">
<el-button @click="getCheckedNodes">獲取</el-button>
<el-button @click="resetChecked">清空</el-button>
</div>
</div>
</template>
<script>
export default {
methods: {
getCheckedNodes() {
var rad=''
var ridsa = this.$refs.tree.getCheckedKeys().join(',')// 獲取當(dāng)前的選中的數(shù)據(jù)[數(shù)組] -id, 把數(shù)組轉(zhuǎn)換成字符串
var ridsb = this.$refs.tree.getCheckedNodes()// 獲取當(dāng)前的選中的數(shù)據(jù){對(duì)象}
ridsb.forEach(ids=>{//獲取選中的所有的父級(jí)id
rad+=','+ids.pid
})
rad=rad.substr(1) // 刪除字符串前面的','
var rids=rad+','+ridsa
var arr=rids.split(',')// 把字符串轉(zhuǎn)換成數(shù)組
arr=[...new Set(arr)]; // 數(shù)組去重
rids=arr.join(',')// 把數(shù)組轉(zhuǎn)換成字符串
console.log(rids)
},
resetChecked() {
this.$refs.tree.setCheckedKeys([]);
}
},
data() {
return {
data2: [{
pid:0,
path:xxxx,
id: 1,
label: '一級(jí) 1',
children: [{
pid:1,
path:xxxx,
id: 11,
label: '二級(jí) 1-1'
},
{
pid:1,
path:xxxx,
id: 12,
label: '二級(jí) 1-2'
},
{
pid:1,
path:xxxx,
id: 13,
label: '二級(jí) 1-3'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
</script>
</script>
<style scoped>
</style>
如果是三級(jí)或者是多級(jí),響應(yīng)的數(shù)據(jù)格式必須要有'path:xxxx',這樣才能獲取其父級(jí)id
響應(yīng)的數(shù)據(jù)格式
{
"data": [
{
"id": 30,
"path": xxxx,
"children": [
{
"id": 101,
"path": xxxx,
"children": [
{
"id": 104,
"path": xxxx,
"children": [
{
"id": 105,
"path": xxxx
}
]
}
]
}
]
}
],
"meta": {
"msg": "獲取成功",
"status": 200
}
}
這里是引用~
以上這篇vue treeselect獲取當(dāng)前選中項(xiàng)的label實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建vue報(bào)錯(cuò)vue-cli Failed to download repo&n
通過vue-cli創(chuàng)建Vue項(xiàng)目時(shí),若遇到連接超時(shí)錯(cuò)誤,可采用離線方式解決,具體操作是下載并解壓vue-templates/webpack到本地.vue-templates目錄,再使用--offline參數(shù)重新執(zhí)行初始化命令2024-09-09
Vue動(dòng)畫之第三方類庫實(shí)現(xiàn)動(dòng)畫方式
這篇文章主要介紹了Vue動(dòng)畫之第三方類庫實(shí)現(xiàn)動(dòng)畫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue2+Elementui?Dialog實(shí)現(xiàn)封裝自定義彈窗組件
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,本文就來為大家詳細(xì)介紹一下Vue2如何結(jié)合Elementui?Dialog實(shí)現(xiàn)封裝自定義彈窗組件,希望對(duì)大家有所幫助2023-12-12
vue項(xiàng)目中路由跳轉(zhuǎn)頁面不變問題及解決
這篇文章主要介紹了vue項(xiàng)目中路由跳轉(zhuǎn)頁面不變問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
使用sessionStorage解決vuex在頁面刷新后數(shù)據(jù)被清除的問題
localStorage沒有時(shí)間期限,除非將它移除,sessionStorage即會(huì)話,當(dāng)瀏覽器關(guān)閉時(shí)會(huì)話結(jié)束,有時(shí)間期限,具有自行百度。本文使用的是sessionStorage解決vuex在頁面刷新后數(shù)據(jù)被清除的問題,需要的朋友可以參考下2018-04-04
vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例
本文主要介紹了vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例,主要是使用navigator.mediaDevices.getUserMedia這個(gè)API來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

