I recently used Vue to make a small project - a draggable tree structure diagram. Vue recursive componentThe structure is implemented through Vue's recursive components The layout uses flex, and the structural lines are implemented by CSS pseudo-classes It should be noted that for centered layout, when there are too many elements on the X-axis and the width of the child elements exceeds the view, although there is a scroll bar after the element is centered, it can only reach the content on the right, and the content on the left will be inaccessible. You can set the parent element to inline-flex and the width to auto. Of course, if it is the above structure, there will be no such problem, but when it comes to big data rendering, it still bothered me for an afternoon. drag eventFirst, bind the draggable attribute to the element you want to drag. Except for the <a> and <img> tags, which are set to true by default, all other elements need to be set as follows Then there are three events: Note that To transfer the value of the dragged element to the dropped location, you need to use methods:{ dragstart(e,nodeObj){ console.log('🐉drag moving point',nodeObj.name,); let transData = JSON.stringify(nodeObj) //The data passed by dragging is first converted to JSON format e.dataTransfer.setData("node", transData) }, dragover(e){ e.preventDefault() }, drop(e,nodeObj){ console.log('🐉dropped to',nodeObj.name); let getData = JSON.parse( e.dataTransfer.getData("node")) console.log('Get data',getData); } } Knowing this, the next thing to do is to put the acquired drag point data into the Create a bus folder and create a new index.js file import Vue from "vue" const busEvent = new Vue({ data(){ return { dragNodeIndex:-1, //The index of the drag node in the parent node children array } }, created(){ this.$on("transDragNodeIndex", res=>{//To monitor $emit through $on, you need to ensure that the custom event is monitored before it is triggered, that is, subscription precedes publishing, otherwise the data cannot be monitored. I have not used eventBus much, so this is a pit this.dragNodeIndex=res }) } }) export default busEvent Introduce The next step is to make some logical judgments. For example, if a parent node cannot be dragged to a child node, first recursively traverse the names of all child nodes on the parent node into an array. If ifFatherDragToSon(dragObj,dropObj){//Judge whether the parent node has moved to the child nodeif (dragObj.children.length === 0) return false; let newArr = []; function getAllName(dragObj) { newArr.push(...dragObj.children); if (dragObj.children.length === 0) { return; } else { for (let i = 0; i < dragObj.children.length; i++) { getAllName(dragObj.children[i]); } } } getAllName(dragObj); if (newArr.includes(dropObj)) { return true; } return false; } Use Dragging to itself doesn't work either, just return directly. A child node is added under the "Li Hu Chong" point, mainly to verify that the address of the node in the stack is used for judgment, rather than based on Another thing to mention is Code connection The above is the detailed content of Vue's implementation of a draggable tree structure diagram. For more information about Vue's implementation of a tree structure diagram, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Mysql 5.7.19 free installation version encountered pitfalls (collection)
>>: How to implement email alert in zabbix
Yesterday I bought an Alibaba Cloud server that h...
3 ways to implement tab switching in Vue 1. v-sho...
This article shares the MySQL installation and co...
Overview It is usually not what we want to presen...
Implementation ideas: First of all, the alarm inf...
Introduction: AD is the abbreviation of Active Di...
Table of contents Preface Why do we need to encap...
Table of contents 1. Anti-shake function 2. Use d...
Preface Everyone should be familiar with the watc...
1. Install the cross-system file transfer tool un...
On many websites, we have seen the input box disp...
Today I encountered a very strange situation. Aft...
The detailed steps for installing mysql5.7.19 on ...
Shtml and asp are similar. In files named shtml, s...
In the previous article, after configuring the we...