Skip to content
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

获取选中节点的路径 #157

Open
achuan9 opened this issue Apr 18, 2019 · 1 comment
Open

获取选中节点的路径 #157

achuan9 opened this issue Apr 18, 2019 · 1 comment

Comments

@achuan9
Copy link

achuan9 commented Apr 18, 2019

认为缺少一个这样点API:
当选择一个子节点,应该返回该子节点选择路径数组,也就是所有父节点。
举例:
当树结构是 省市区,那么直接选择某个区,应该有一个获取包括当前区及所在省市的信息API。
目前的实现方法:

<template>
  <v-tree
    ref="tree"
    :data="treeData"
    :tpl="tpl"
    :draggable="false"
    :radio="true"
    :maxLevel="4"
    :allowGetParentNode="true"
    @node-select="nodeSelect"
  />
</template>
<script>
import 'vue-tree-halower/dist/halower-tree.min.css'
import { VTree } from 'vue-tree-halower'
export default {
  components: { VTree },
  data() {
    return {
      treeData: [
        {
          title: '南山区',
          children: [
            {
              title: '粤海街道',
              children: [
                { title: '科苑北路1' },
                { title: '科苑北路2' },
                { title: '科苑北路3' }
              ]
            }
          ]
        }
      ]
    }
  },
  methods: {
    tpl(node, ctx, parent, index, props) {
      let titleClass = node.selected ? 'node-title node-selected' : 'node-title'
      if (node.searched) titleClass += ' node-searched'
      node.parent = () => parent
      //节点标题
      const titleNode = (
        <span
          class={titleClass}
          domPropsInnerHTML={node.title}
          onClick={() => this.$refs.tree.nodeSelected(node, props)}
        />
      )
      return <span>{titleNode}</span>
    },
    nodeSelect(node) {
      const list = [node]
      while (node.parent() !== null) {
        list.push(node.parent())
        node = node.parent()
      }
      console.log(list.map(item => item.title))
    }
  }
}
</script>
@odanzhou
Copy link
Contributor

你不是实现了嘛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants