当前位置:   article > 正文

VUE elementUI el-ree 树形控件之回显数据_element-ui的el-tree组件

element-ui的el-tree组件

背景:el-ree 树形控件回显数据的问题,在网上有很多坑,也有许多对这个问题的解决方案及代码,但大都说的不明白

本解决方案如下:

直接上代码:

VIEW:

  1. <el-form :model="selForm" ref="selForm" label-width="0px" class="demo-ruleForm">
  2. <el-form-item>
  3. <el-col :span="8">
  4. <el-tree class="flow-tree"
  5. :data="groupTreeData"
  6. :props="defaultProps"
  7. show-checkbox
  8. node-key="code"
  9. ref="tree"
  10. :default-expand-all="true"
  11. :expand-on-click-node="false"
  12. :default-checked-keys="[resourceCheckedKey]"
  13. v-loading="loadingFunction">
  14. </el-tree>
  15. </el-col>
  16. </el-form-item>
  17. </el-form>

关键点示意:

数据示例:[{"code":"10001","name":"北京"}]

a、node-key="code"  索引绑定字段,此 code 要与绑定的数据的参数字段一致

b、ref="tree" 指定的绑定控件名

c、:default-checked-keys="[resourceCheckedKey]"  绑定的回显数组数据,resourceCheckedKey 索引数组值,即 code 值

d、:data="groupTreeData"  绑定的数组数据

DATA定义:

data(){

.......

loadingFunction: false,
defaultProps: { "id": "code", "label": 'name', "children": 'children' },
groupTreeData: [],//此定义一定要为空,否则不能回显数据
resourceCheckedKey: [],

},
methods: {

  1. bindCheckedFunction() {//绑定已有数据
  2. this.loadingFunction = true;
  3. var obj = {
  4. id: this.infoid
  5. }
  6. getDataList(obj).then(res => {
  7. if (res.code == 0 && res.data != "") {
  8. this.resourceCheckedKey = res.data;
  9. let that = this;
  10. setTimeout(function() {
  11. res.data.forEach(value => {
  12. that.$refs.tree.setChecked(value.code, true, false);
  13. });
  14. }, 500);
  15. this.$refs.tree.setCheckedKeys(this.resourceCheckedKey)
  16. } else {
  17. this.resourceCheckedKey = [];
  18. this.$refs.tree.setCheckedKeys(this.resourceCheckedKey)
  19. }
  20. this.loadingFunction = false;
  21. });
  22. }

...................

}

数据示意:this.$refs.tree  的 tree 即为 VIEW中的 b 所指内容

以上完结。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/734847
推荐阅读
相关标签