Table of Contents
uni-app drag and drop sorting
Complete code
Home Web Front-end uni-app How does uni-app+sortable.js implement drag and drop sorting? Step sharing

How does uni-app+sortable.js implement drag and drop sorting? Step sharing

Nov 24, 2021 pm 07:49 PM
uni-app Drag and drop sort

How to implement drag and drop sorting in uni-app? The following article will introduce to you how to use sortable.js to implement drag and drop sorting in uni-app. I hope it will be helpful to you!

How does uni-app+sortable.js implement drag and drop sorting? Step sharing

uni-app drag and drop sorting

Preface

Make one this week On the page, there is a character sorting function that can be manually dragged to change the sorting position. After searching, I found

sortable.js, which can be used to implement this drag-and-drop function.

Thoughts

When viewing the official document of sortable.js, I saw that there is an onUpdate in it event, after dragging to change the position, the return value contains the starting index value and the changed index value. Through this, you can change the content of the array to achieve the function of changing the position after dragging.

Steps

Installationsortable.js

npm install sortablejs --save-dev
Copy after login

Get node

The node obtained here is the parent node that needs to be dragged into the list

let uls = document.getElementById('list')
Copy after login

Load node

 new Sortable(uls,{})
Copy after login

Trigger<span style="font-size: 18px;">onUpdate</span>Event

Because in the process of doing it, I found that if I use the position of the dom node to operate When modifying the order of items, a bug will occur, so after searching for information, I finally found the problem using Sortable in Vue. Therefore, before modifying the order of items, you should first modify the corresponding node.

Change Node

Delete the moved node first, then insert the moved node into the original node

newLi = uls.children[newIndex],  //移动节点
oldLi = uls.children[oldIndex];  //原有节点
uls.removeChild(newLi) // 删除移动的节点
uls.insertBefore(newLi, oldLi) // 将移动节点插入到原有节点中
Copy after login

Note: Because when changing from When dragging down and up, one node will be added, so the original index value will be one less. When newIndex < oldIndex, the next node of the oldLi node will be used.

uls.insertBefore(newLi, oldLi.nextSibling)
Copy after login

Change the array

Delete the original array and store the data

let item = _this.items.splice(oldIndex, 1)
Copy after login

Add the data to the remaining array index value

_this.items.splice(newIndex, 0, item[0])
Copy after login

Complete code

let uls = document.getElementById(&#39;list&#39;)
    new Sortable(uls, {
      onUpdate: function (event) {
        //获得基础数据
        let newIndex = event.newIndex,
          oldIndex = event.oldIndex,
          newLi = uls.children[newIndex],
          oldLi = uls.children[oldIndex];
        // 删除原有节点
        uls.removeChild(newLi)
        // 将移动的节点插入原有节点中
        if (newIndex > oldIndex) {
          uls.insertBefore(newLi, oldLi)
        } else {
          uls.insertBefore(newLi, oldLi.nextSibling)
        }
        // 修改数组
        let item = _this.items.splice(oldIndex, 1)
        _this.items.splice(newIndex, 0, item[0])
      },
Copy after login

Recommended: "uniapp tutorial"

The above is the detailed content of How does uni-app+sortable.js implement drag and drop sorting? Step sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to develop uni-app in VSCode? (Tutorial sharing) How to develop uni-app in VSCode? (Tutorial sharing) May 13, 2022 pm 08:11 PM

How to develop uni-app in VSCode? The following article will share with you a tutorial on developing uni-app in VSCode. This may be the best and most detailed tutorial. Come and take a look!

Use uniapp to develop a simple map navigation Use uniapp to develop a simple map navigation Jun 09, 2022 pm 07:46 PM

How to use uniapp to develop a simple map navigation? This article will provide you with an idea for making a simple map. I hope it will be helpful to you!

How to use Vue and Element-UI to implement drag-and-drop sorting function How to use Vue and Element-UI to implement drag-and-drop sorting function Jul 22, 2023 pm 04:12 PM

How to use Vue and Element-UI to implement drag-and-drop sorting function Preface: In web development, drag-and-drop sorting function is a common and practical function. This article will introduce how to use Vue and Element-UI to implement the drag-and-drop sorting function, and demonstrate the implementation process through code examples. 1. Environment setup and installation Node.js Before starting, you need to install Node.js. You can visit https://nodejs.org/ to download and install the version corresponding to the operating system. Install VueCL

How to use vue and Element-plus to implement drag-and-drop and sorting functions How to use vue and Element-plus to implement drag-and-drop and sorting functions Jul 17, 2023 pm 09:02 PM

How to use vue and Element-plus to implement drag-and-drop and sorting functions Introduction: In modern web development, user interaction experience is becoming more and more important. Drag-and-drop and sorting functions are common interactive operations that allow users to easily rearrange elements or adjust the position of elements. This article will introduce how to use Vue and Element-plus libraries to implement drag-and-drop and sorting functions, and provide corresponding code examples. Technical preparation: In order to start writing Vue and Element-plus related code, we

Let's talk about how to use uniapp to develop a snake game! Let's talk about how to use uniapp to develop a snake game! May 20, 2022 pm 07:56 PM

How to use uniapp to develop a snake game? The following article will take you step by step to implement the Snake game in uniapp. I hope it will be helpful to you!

How to encapsulate uni-app vue3 interface request How to encapsulate uni-app vue3 interface request May 11, 2023 pm 07:28 PM

uni-app interface, global method encapsulation 1. Create an api file in the root directory, create api.js, baseUrl.js and http.js files in the api folder 2.baseUrl.js file code exportdefault"https://XXXX .test03.qcw800.com/api/"3.http.js file code exportfunctionhttps(opts,data){lethttpDefaultOpts={url:opts.url,data:data,method:opts.method

Examples to explain how uniapp implements the all-select function of multi-select boxes Examples to explain how uniapp implements the all-select function of multi-select boxes Jun 22, 2022 am 11:57 AM

This article brings you relevant knowledge about uniapp, which mainly organizes the related issues of implementing the select-all function of the multi-select box. The reason why the select-all function cannot be implemented is that when the checked field of the checkbox is dynamically modified, the status on the interface can Real-time changes, but the change event of checkbox-group cannot be triggered. Let's take a look at it. I hope it will be helpful to everyone.

Take you step by step to develop a uni-app calendar plug-in (and publish it) Take you step by step to develop a uni-app calendar plug-in (and publish it) Jun 30, 2022 pm 08:13 PM

This article will guide you step by step in developing a uni-app calendar plug-in, and introduce how the next calendar plug-in is developed from development to release. I hope it will be helpful to you!

See all articles