Vue.js Drag and Drop Tree View Component Library

Vue Drag Tree Components (Vue2.x) allow you to drag and drop a node to exchange its data.

Feature

  • Double-click a node to convert it to a folder.
  • Drag and drop tree knots, even between two different levels.
  • Customize your node (how to display the node. eg: node name and left icon).
  • Controls whether a particular node can be pulled and whether a node can be connected to other nodes.
  • append/remove node at any level (#TODO).

How to make use of it:

Install & Download:

npm install vue-drag-tree --S
or
yarn add vue-drag-tree -S

Usage

import Vue from 'vue'
import VueDragTree from 'vue-drag-tree'
import 'vue-drag-tree/dist/vue-drag-tree.min.css'

Vue.use(VueDragTree)
<template>
    <vue-drag-tree :data='data' :allowDrag='allowDrag' :allowDrop='allowDrop' :defaultText='"New Node"' @current-node-clicked='curNodeClicked' @drag="dragHandler" @drag-enter="dragEnterHandler" @drag-leave="dragLeaveHandler" @drag-over="dragOverHandler" @drag-end="dragEndHandler" @drop="dropHandler" v-slot="slotProps">
    <!-- customize your node here if don't like the default / 如果你不喜欢默认样式,可以在这里定制你自己的节点 -->
    <span :class="[slotProps.isClicked ? 'i-am-clicked' : 'i-am-not-clicked']"></span>
    <span class='i-am-node-name'>{{slotProps.nodeName}}</span>
    </vue-drag-tree>
</template>
<script>
export default{
  data(){
    return{
      data: [
        {
          name: 'Node 0-0',
          id: 0,
          children: [
            {
              name: 'Node 1-1',
              id: 3,
              children: [
                {
                  name: 'Node 2-1',
                  id: 4,
                  children: []
                },
                {
                  name: 'Node 2-2',
                  id: 10,
                  children: []
                }
              ]
            },
            {
              name: 'Node 1-2',
              id: 13,
              children: []
            }
          ]
        },
        {
          name: 'Node 0-1',
          id: 14,
          children: []
        }
      ]
    }
  },
  methods: {
   	allowDrag(model, component) {
      if (model.name === 'Node 0-1') {
        // can't be dragged
        return false;
      }
      // can be dragged
      return true;
    },
    allowDrop(model, component) {
      if (model.name === 'Node 2-2') {
        // can't be placed
        return false;
      }
      // can be placed
      return true;
    },
    curNodeClicked(model, component) {
      // console.log('curNodeClicked', model, component);
    },
    dragHandler(model, component, e) {
      // console.log('dragHandler: ', model, component, e);
    },
    dragEnterHandler(model, component, e) {
      // console.log('dragEnterHandler: ', model, component, e);
    },
    dragLeaveHandler(model, component, e) {
      // console.log('dragLeaveHandler: ', model, component, e);
    },
    dragOverHandler(model, component, e) {
      // console.log('dragOverHandler: ', model, component, e);
    },
    dragEndHandler(model, component, e) {
      // console.log('dragEndHandler: ', model, component, e);
    },
    dropHandler(model, component, e) {
      // console.log('dropHandler: ', model, component, e);
    }
  }
}
<script>

API


Attributes

NameDescriptionTypeDefault
datadata of the treeArray--
defaultTextthe default text of the new nodeString“New Node”
allowDragJudging which node can be draggedFunction()=>true
allowDropJudging which node can be plugged into other nodesFunction()=>true
disableDBClickdisable append a new child node by double click nodeBooleanfalse

Method

NameDescriptionarguments
current-node-clickedTell you which node was clicked(model, component) model: node data was clicked. component: VNode data for the node was clicked
dragThe drag event is fired every few hundred milliseconds as a node is being dragged by the user(model, component,e) model: node data was dragged. component: VNode data for the node was dragged; e: drag event
drag-enterThe drag-enter event is fired when a dragged node enters a valid drop target(model, component,e) model: data of the valid drop target; component: VNode of the valid drop target; e: drag event
drag-leaveThe drag-leave event is fired when a dragged node leaves a valid drop target(model, component,e) model: data of the valid drop target; component: VNode of the valid drop target; e: drag event
drag-overThe drag-over event is fired when a node is being dragged over a valid drop target(model, component,e) model: data of the valid drop target; component: VNode of the valid drop target; e: drag event
drag-endThe drag-end event is fired when a drag operation is ended(model, component,e) model: node data was dragged. component: VNode data for the node was dragged; e: drag event
dropThe drop event is fired when a node is dropped on a valid drop target.(model, component,e) model: data of the valid drop target; component: VNode of the valid drop target; e: drag event

Draggable Tree View Component, vue-drag-tree Plugin/Github, tree view component with checkbox


See Demo And Download

Official Website(shuiRong): Click Here

This superior jQuery/javascript plugin is developed by shuiRong. For extra advanced usage, please go to the official website.

Share