vue中使用Element-Ui的Tree 树形控件点击子级获取上级及以上的全部id ( 修改element-ui底层文件实现方法 )
vue中使用Element-Ui的Tree 树形控件点击子级获取上级及以上的全部id ( 修改element-ui底层文件实现方法 )Element-ui官网给的方法getCheckedKeys() { console.log(this.$refs.tree.getCheckedKeys()); },这种只有在所有子级都被选中的情况下才能获得父级的id,如果不选中所有的子级那么获取得到的id就只有
·
vue中使用Element-Ui的Tree 树形控件点击子级获取上级及以上的全部id ( 修改element-ui底层文件实现方法 )
Element-ui官网给的方法
getCheckedKeys() { console.log(this.$refs.tree.getCheckedKeys()); },
这种只有在所有子级都被选中的情况下才能获得父级的id,如果不选中所有的子级那么获取得到的id就只有子级的。但是一般提交数据时后台都需要父级id的。
解决方法
1,找到项目中的\node_modules\element-ui\lib\element-ui.common.js文件
2,搜索文件中的TreeStore.prototype.getCheckedNodes
方法中的
// if ((child.checked || includeHalfChecked && child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) {
// checkedNodes.push(child.data);
// }
修改为
if ((child.checked || child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) {
checkedNodes.push(child.data);
}
3,然后重启项目
4,然后 console.log(this.$refs.tree.getCheckedKeys())
就可以拿到父节点以上的全部id了
更多推荐
已为社区贡献21条内容
所有评论(0)