Skip to content

a-directory-tree 使用 treeData 时无法正确的全部展开子节点 #2858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task
vxhly opened this issue Sep 17, 2020 · 6 comments · Fixed by #2869
Closed
1 task

a-directory-tree 使用 treeData 时无法正确的全部展开子节点 #2858

vxhly opened this issue Sep 17, 2020 · 6 comments · Fixed by #2869
Labels

Comments

@vxhly
Copy link
Contributor

vxhly commented Sep 17, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Version

1.6.5

Environment

Windows,Google Chrome ,[email protected]

Reproduction link

Edit on CodeSandbox

Steps to reproduce

对 a-directory-tree 配置了 treeData 和 default-expand-all 两个配置项

What is expected?

能够完全展开子节点

What is actually happening?

不能完全展开子节点

@vxhly
Copy link
Contributor Author

vxhly commented Sep 18, 2020

定位到 DirectoryTree.js 源代码中的以下代码

    var treeProps = {
      props: (0, _extends3['default'])({
        icon: getIcon
      }, props, {
        prefixCls: prefixCls,
        // expandedKeys: expandedKeys, // 通过调试,删除改行代码是可以展开全部节点的
        selectedKeys: selectedKeys,
        switcherIcon: (0, _propsUtil.getComponentFromProp)(this, 'switcherIcon')
      }),
      ref: 'tree',
      'class': prefixCls + '-directory',
      on: (0, _extends3['default'])({}, (0, _omit2['default'])(listeners, ['update:selectedKeys']), {
        select: this.onSelect,
        click: this.onClick,
        dblclick: this.onDoubleClick,
        expand: this.onExpand
      })
    };

utils.js 中的代码

function getFullKeyListByTreeData(treeData) {
  console.log(treeData)
  var keys = [];

  (treeData || []).forEach(function (item) {
    keys.push(item.key); // 此处写死了,本应该我们替换了默认的键名 key ,这里应该使用 替换好的
    if (item.children) {
      keys = [].concat((0, _toConsumableArray3['default'])(keys), (0, _toConsumableArray3['default'])(getFullKeyListByTreeData(item.children)));
    }
  });
  return keys;
}

@vxhly
Copy link
Contributor Author

vxhly commented Sep 18, 2020

我将我的组件代码修改如下,是可以支持默认展开功能,主要还是自定义的 replaceFields 在源代码中没有应用

<template>
  <a-directory-tree
    v-model="checkedKeys"
    v-bind="$attrs"
    v-on="$listeners"
    checkable
    defaultExpandAll
    :tree-data="directoryTreeData"
    :selectable="false"
    :replaceFields="replaceFields"
  >
    <!-- 官方slots插槽 " -->
    <template v-for="slot in Object.keys($slots)" #[slot]>
      <slot :name="slot" />
    </template>
  </a-directory-tree>
</template>

<script>
export default {
  name: 'Ant_DirectoryTree',
  props: {
    value: {
      type: Array,
      default: () => []
    },
    dataSource: {
      type: Array,
      default: () => []
    },
    replaceFields: {
      type: Object,
      default: () => {
        return {
          children: 'children',
          title: 'name',
          key: 'id'
        }
      }
    }
  },
  computed: {
    checkedKeys: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    directoryTreeData: {
      get () {
        const { dataSource } = this
        const {
          children: CHILDREN = 'children',
          key: KEY = 'key'
        } = this.replaceFields

        const setIsLeaf = (val) => {
          if (val[CHILDREN] && val[CHILDREN].length) {
            val[CHILDREN].map(item => {
              setIsLeaf(item)
            })
          } else {
            val.isLeaf = true
            val.key = val[KEY]
          }
        }
        dataSource.map(item => {
          return setIsLeaf(item)
        })
        return dataSource
      }
    }
  }
}
</script>

@tangjinzhou
Copy link
Member

如果已经定位问题,欢迎给我们提交 pr 感谢

@vxhly vxhly mentioned this issue Sep 20, 2020
13 tasks
@gnineh
Copy link

gnineh commented Sep 22, 2020

这个问题我也撞见了。希望尽快合并PR.

@vxhly
Copy link
Contributor Author

vxhly commented Sep 22, 2020

原谅我不知道怎么做 PR,可以看下我的做的 更改,这样的更改是可以支持了 https://github.com/vxhly/ant-design-vue/commit/9e5ce3ed517779254eaedaa4465020936c647e55

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants