search
HomeWeb Front-enduni-appHow does uni-app+sortable.js implement drag and drop sorting? Step sharing

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

Get node

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

let uls = document.getElementById('list')

Load node

 new Sortable(uls,{})

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) // 将移动节点插入到原有节点中

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 , the next node of the <code>oldLi node will be used.

uls.insertBefore(newLi, oldLi.nextSibling)

Change the array

Delete the original array and store the data

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

Add the data to the remaining array index value

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

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])
      },

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
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools