项目作者: megafetis

项目描述 :
A vue3 block organization tree view component. Hierarchical horizontal or vertical tree
高级语言: JavaScript
项目地址: git://github.com/megafetis/vue3-blocks-tree.git
创建时间: 2021-01-11T13:58:09Z
项目社区:https://github.com/megafetis/vue3-blocks-tree

开源协议:MIT License

下载


vue3-blocks-tree

A simple organization structure tree view based on Vue3.x. It supports events, slots, horizontal vision and nodes manipulation

Thanks to hukaibaihu and his sources for vue 2 taken as basis.

Usage

  1. <template>
  2. <h1>Basic</h1>
  3. <div>
  4. <blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true"></blocks-tree>
  5. </div>
  6. <h1>With slots</h1>
  7. <div>
  8. <blocks-tree :data="treeData" :horizontal="treeOrientation=='1'" :collapsable="true" :props="{label: 'label', expand: 'expand', children: 'children', key:'some_id'}">
  9. <template #node="{data,context}">
  10. <span>
  11. <input type="checkbox" :checked="selected.indexOf(data.some_id)> -1" @change="(e)=>toggleSelect(data,e.target.checked)"/> {{data.label}}
  12. </span>
  13. <br/>
  14. <span v-if="data.children && data.children.length">
  15. <a href="#" @click="context.toggleExpand">toggle expand</a>
  16. </span>
  17. </template>
  18. </blocks-tree>
  19. <div>
  20. Selected: {{selected}}
  21. </div>
  22. </div>
  23. <h1>Change orientation</h1>
  24. <select v-model="treeOrientation">
  25. <option value="0">Vertical</option>
  26. <option value="1">Horizontal</option>
  27. </select>
  28. </template>
  29. <script>
  30. import { defineComponent,ref,reactive } from 'vue';
  31. export default defineComponent({
  32. setup() {
  33. let selected = ref([]);
  34. let treeOrientation = ref("0");
  35. let treeData = reactive({
  36. label: 'root',
  37. expand: true,
  38. some_id: 1,
  39. children: [
  40. { label: 'child 1', some_id: 2, },
  41. { label: 'child 2', some_id: 3, },
  42. {
  43. label: 'subparent 1',
  44. some_id: 4,
  45. expand: false,
  46. children: [
  47. { label: 'subchild 1', some_id: 5 },
  48. {
  49. label: 'subchild 2',
  50. some_id: 6,
  51. expand: false,
  52. children: [
  53. { label: 'subchild 11', some_id: 7 },
  54. { label: 'subchild 22', some_id: 8 },
  55. ]
  56. },
  57. ]
  58. },
  59. ]
  60. });
  61. const toggleSelect = (node, isSelected) => {
  62. isSelected ? selected.value.push(node.some_id) : selected.value.splice(selected.value.indexOf(node.some_id), 1);
  63. if(node.children && node.children.length) {
  64. node.children.forEach(ch=>{
  65. toggleSelect(ch,isSelected)
  66. })
  67. }
  68. }
  69. return {
  70. treeData,
  71. selected,
  72. toggleSelect,
  73. treeOrientation
  74. }
  75. }
  76. })
  77. </script>

Demo

[https://megafetis.github.io/vue3-blocks-tree-demo]

NPM

  1. # use npm
  2. npm i vue3-blocks-tree
  3. # use yarn
  4. yarn add vue3-blocks-tree

Import Plugins

  1. import {createApp} from 'vue';
  2. import VueBlocksTree from 'vue3-blocks-tree';
  3. import 'vue3-blocks-tree/dist/vue3-blocks-tree.css';
  4. // or import 'vue3-blocks-tree/src/styles/blocks-tree.less';
  5. let defaultoptions = {treeName:'blocks-tree'}
  6. createApp(App)
  7. .use(VueBlocksTree,defaultoptions)
  8. // or .component('blocks-tree',VueBlocksTree)
  9. // ...

API

api descripton type
node context Context to node manipulation in slot or in event callback interface NodeContext { isExpanded():boolean; toggleExpand():void; }

props

prop descripton type default
data Object
props configure props Object {label: 'label', children: 'children', expand: 'expand',key: 'id'}
labelWidth node label width String \ Number auto
collapsable children node is collapsable Boolean true
renderContent how to render node label Function -
nodeClassName custom node class Function \ String -
labelClassName node label class Function \ String -

events

event name descripton type
node-click Click event Function
node-mouseover onMouseOver event Function
node-mouseout onMouseOut event Function
node-expand click expand button event Function

Slots

slot name descripton params
node current node scoped slot data - node data, context - node context

node-expand

well be called when the collapse-btn clicked

  • params e Event
  • params data Current node data
  • params context Node context

node-click

well be called when the node-label clicked

  • params e Event
  • params data Current node data
  • params context Node context

node-mouseover

It is called when the mouse hovers over the label.

  • params e Event
  • params data Current node data
  • params context Node context

node-mouseout

It is called when the mouse leaves the label.

  • params e Event
  • params data Current node data
  • params context Node context

Example

  • default

    default

  • horizontal

    horizontal

  • use node slot

    horizontal

License

MIT

Buy Me A Coffee