Vue+Sortable project practical code
This time I will bring you the actual code of the Vue Sortable project. What are the precautions for the actual implementation of the Vue Sortable project? The following is a practical case, let's take a look.
I previously developed a backend management system, which used the component library Vue and Element-UI. I encountered a very interesting problem and would like to share it with you.
The scene is like this. On a list display page, I used the table component of Element-UI. The new requirement is to support drag-and-drop sorting based on the original table. However, the original component itself does not support drag-and-drop sorting, and since it is directly introduced from Element-UI, it is inconvenient to modify its source code, so the only feasible method is to directly operate the DOM.
The specific method is to perform real DOM operations on this.$el in the mounted life cycle function, listen to a series of drag events, move the DOM in the event callback, and update the data.
There are quite a lot of HTML5 Drag events, which are similar to Touch events. You can also implement them manually. However, I am lazy here and directly use an open source Sortable library and directly pass in this.$el. Listen to the encapsulated callback, and according to Vue's development model, update the actual Data data in the callback of the mobile DOM to maintain consistency between the data and the DOM.
If you think it’s over here, you are totally wrong. Sooner or later, you will have to pay back the laziness you have stolen. . . I thought this solution was very good, but as soon as I wanted to debug it, a strange phenomenon occurred: after A and B were dragged and swapped, B and A were magically swapped back again! How is this going? It seems that there is no problem with our operation. After the real DOM is moved, we also move the corresponding data. The order of the data array and the order of the rendered DOM should be consistent.
where is the problem? Let's recall the implementation principle of Vue. Before Vue2.0, two-way binding was achieved through defineProperty dependency injection and tracking. For the v-for array instruction, if a unique Key is specified, the difference of elements in the array will be calculated through an efficient Diff algorithm, and minimal movement or deletion operations will be performed. After the introduction of Virtual Dom after Vue2.0, the Dom Diff algorithm of the Children element is actually similar to the former. The only difference is that before 2.0, Diff directly targeted the array object of the v-for instruction, while after 2.0, it targeted Virtual Dom. The DOM Diff algorithm will not be described in detail here. The virtual-dom diff algorithm is explained more clearly here
Assume that our list element array is
['A','B', 'C','D']
The rendered DOM node is
[$A,$B,$C,$D]
Then the corresponding structure of Virtual Dom is
[{elm:$A,data:'A'},
{elm:$B,data:'B'} ,
{elm:$C,data:'C'},
{elm:$D,data:'D'}]
Assuming that after drag and drop sorting, the real The DOM becomes
[$B,$A,$C,$D]
At this time we only operate the real DOM and modify its position, and The structure of Virtual Dom has not changed, it is still
[{elm:$A,data:'A'},
{elm:$B,data:'B'},
{elm:$C,data:'C'},
{elm:$D,data:'D'}]
At this time, we also change the list elements according to the real DOM After sorting, it becomes
['B','A','C','D']
At this time, according to the Diff algorithm, the calculated Patch is , the first two items of VNode are nodes of the same type, so they are updated directly, that is, the $A node is updated to $B, the $B node is updated to $A, and the real DOM changes back to
[ $A,$B,$C,$D]
So there is a problem that it is updated by the Patch algorithm after dragging. The operation path can be simply understood as
Drag and move the real DOM -> Manipulate the data array -> Patch algorithm and then update the real DOM
The root cause
The root cause is between the Virtual DOM and the real DOM There is an inconsistency.
So before Vue2.0, because Virtual DOM was not introduced, this problem did not exist.
When using the Vue framework, try to avoid directly manipulating the DOM
Solution
1. Uniquely mark each VNode by setting the key. This is also the way Vue recommends using the v-for instruction. Because the sameVnode method will be called when judging whether two VNodes are of the same type, the priority is to judge whether the key is the same
1 2 3 4 5 6 7 8 9 |
|
2. Because the fundamental reason is that the real DOM and VNode are inconsistent, you can move the real DOM by dragging Operation restoration, that is, in the callback function, restore [$B,$A,$C,$D] to [$A,$B,$C,$D], and return the DOM operation to Vue
Drag and move the real DOM ->Restore the move operation-> Manipulate the data array-> Patch algorithm and then update the real DOM
The code is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
3. Violent solution! Without patch update, directly re-render through v-if settings. Of course, it is not recommended to do this, but I just provide this idea~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
So, when we usually use the framework, we must also understand the implementation principle of the framework, otherwise we will not know how to deal with some difficult situations. ~
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
What specifications for writing JS code has been released by Google
Vue Nuxt.js makes server-side rendering
The above is the detailed content of Vue+Sortable project practical code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.
