el-tree 获取选中节点的路径名称

技术 置顶 精帖
0 955
peng49
peng49 2021-12-27 15:24:23
 
  1. document.querySelectorAll('.el-checkbox.is-checked').forEach(ele => {
  2. let id = $(ele).parents('.el-tree-node')[0].dataset.key
  3. let parents = $(ele).parents('.el-tree-node__children')
  4. let path = ele.parentNode.innerText
  5. parents.each(i => {
  6. path = $(parents[i]).prev().text() + ' => '+path
  7. })
  8. console.log(path)
  9. });

不依赖jQuery

  1. document.querySelectorAll('.el-checkbox.is-checked').forEach(ele => {
  2. let nodeChild = ele
  3. while(nodeChild.parentElement.className.indexOf('el-tree-node ') === -1){
  4. nodeChild = nodeChild.parentElement
  5. }
  6. let id = nodeChild.parentElement.dataset.key
  7. let parents = []
  8. while(!!ele.parentElement){
  9. if(ele.parentElement.className.indexOf('el-tree-node ') > -1){
  10. parents.push(ele.parentElement.querySelector('.el-tree-node__label').innerText)
  11. }
  12. ele = ele.parentElement
  13. }
  14. console.log(parents)
  15. console.log(id)
  16. });
回帖
登录
忘记密码?