Sortable

本文介绍如何在React应用中引入SortableJS库,实现列表项的拖拽排序功能。通过创建React组件并利用SortableJS的onEnd事件更新状态,使列表项能够响应拖拽操作并实时调整其顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

d_(:з」∠)_

import React, {Component} from 'react';
import "./app.css";

import Sortable from 'sortablejs';
// import * as Sortable from 'sortablejs'; export
default class App extends Component { constructor(props) { super(props); this.state = { arr: ['a', 'b', 'c', 'd', 'e'] }; } componentDidMount() { let oItems = document.getElementById("items"); let sortable = Sortable.create(oItems, { onEnd: (evt) => { let arr = this.state.arr; // splice 先改变之前的数组,再把替换的那些个元素作为新的数组返回 arr[evt.oldIndex] = arr.splice(evt.newIndex, 1, arr[evt.oldIndex])[0]; this.setState({ arr }, () => console.log(arr)); } }); } render() { return <ul id="items"> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> </ul> } }

 

转载于:https://www.cnblogs.com/jiujiaoyangkang/p/10058211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值