鍍金池/ 問答/HTML/ element-ui el-tree選中子節(jié)點時默認選擇父節(jié)點

element-ui el-tree選中子節(jié)點時默認選擇父節(jié)點

element-ui里el-tree選中子節(jié)點用getCheckedKeys()只返回子節(jié)點
現(xiàn)在需求是:選中任何一個子節(jié)點都默認選擇父節(jié)點

回答
編輯回答
雅痞

修改node_modules/element-ui/lib/element-ui.common.js文件
TreeStore.prototype.getCheckedKeys函數(shù)中的if(node.checked)改為
if (node.checked||node.indeterminate)

2017年2月28日 13:08
編輯回答
孤毒

官方v2.2.1版本已經(jīng)提供獲取半選中狀態(tài)節(jié)點對象。
clipboard.png
具體實現(xiàn)思路:

// 1. 先合并選中/半選中節(jié)點Id,請求傳給后臺。
[].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())

this.tempKey === -666666 // 臨時key, 用于解決tree半選中狀態(tài)項不能傳給后臺接口問題. # 待優(yōu)化

// 2. 給tree綁定數(shù)據(jù)時,通過tempKey移除之前添加的半選中狀態(tài)節(jié)點Id。
var idx = this.menuIdList.indexOf(this.tempKey)
if (idx !== -1) {
  this.menuIdList.splice(idx, this.menuIdList.length - idx)
}
this.$refs.menuListTree.setCheckedKeys(this.menuIdList)

詳細實現(xiàn)代碼,可在https://github.com/daxiongYan...查閱。

2017年2月26日 11:42
編輯回答
淺淺

能不能不改源碼 我用的是iview組件 要怎么改??

2018年1月26日 02:00