taro h5列表拖拽排序 --- sortablejs 和 react-sortable-hoc

news/2024/7/7 20:29:58

描述:列表,拖拽排序,只测试了h5

一、sortablejs

文档:http://www.sortablejs.com/

1.安装sortablejs
2、引入

import Sortable from 'sortablejs'

3、页面

const [list, setList] = useState([{
  id: 'item-1',
  content: '选项1'
}, {
  id: 'item-2',
  content: '选项2'
}, {
  id: 'item-3',
  content: '选项3'
}])

const getListRef = (instance) => {
  new Sortable(instance, {
    animation: 150,
    ghostClass: '自定义的类名',
  });
}

return (
    <View ref={getListRef}>
      {
        list.map((item, index) => {
          return (
           <View key={`item-${index}`}>
             { item.content }
           </View>
          )
        })
      }
    </View>
)

!!!注意
ghostClass控制的不时拖动中的影子,而是拖动的最终落在目标位置的item
在这里插入图片描述
4、item中有Switch的bug
在这里插入图片描述
解决方法:在switch外套个容器,设置宽高和switch一致,超出隐藏

另外,拖动中的重影没有直接的属性去除,还没有找到好的方法…

二、react-sortable-hoc

文档:https://www.npmjs.com/package/react-sortable-hoc
中文文档:https://www.5axxw.com/wiki/content/hrpw3t
1、安装 react-sortable-hoc和array-move
2、引入

import { arrayMoveImmutable } from 'array-move'
import { SortableContainer, SortableElement } from 'react-sortable-hoc'

3、单个item

const SortableItem = SortableElement(({value}) => {
  return (
    <View className="tab-item">
      <Image src='' className="logo" />
      {value.label}
    </View>
  )
})

4、列表

const SortableList = SortableContainer(({items, onSortEnd}) => {
  return (
    <View className="tab-list">
      {items.map((value, index) => (
        <SortableItem key={`tab-item-${index}`} index={index} value={value} />
      ))}
    </View>
  )
})

5、页面

const [items, setItems] = useState([{
    label: '选项1',
    checked: true
  }, {
    label: '选项2',
    checked: false
  }])

  useEffect(() => {
    // 这里可以监听到items
    console.log(items)
  }, [items])

  const onSortEnd = ({oldIndex, newIndex}) => {
    setItems(arrayMoveImmutable(items, oldIndex, newIndex))
  };
  return (
    <SortableList items={items} onSortEnd={onSortEnd} />
  )
.tab-item {
  display: flex;
  align-items: center;
  padding: 10px;
  margin-bottom: 20px;
  color: #fff;
  background: #ccc;
  .logo {
    margin-right: 10px;
    width: 50px;
    height: 50px;
    background: yellow;
  }
}

!!!注意点
设置样式的时候,tab-item要单独设置,不要嵌套在其他元素下,否则会出现鼠标按下和拖动时,元素样式消失
在这里插入图片描述

常用的属性: (加在SortableList上)
pressDelay:number, 按下之后多少毫秒可以排序,不能与distance同用
Distance: number, 按下之后鼠标移动多少像素可以拖动元素
Helpclass: string , 按下去的元素的样式名称
hideSortableGhost:bolean, 拖动时是否隐藏重影, 默认时隐藏的

默认只要按下就会在body上插入一个item, 可以用pressDelay 或distance控制时间
在这里插入图片描述

6、item中有Switch的bug
在这里插入图片描述
解决方法:在switch外套个容器,设置宽高和switch一致,超出隐藏

7、点击switch, 获取chang的值
思路:子组件item传值给最外层SortableList,通过useState更改列表中switch值,最后在effect中监听到列表


http://lihuaxi.xjx100.cn/news/1430805.html

相关文章

消息队列常见问题(1)-如何保障不丢消息

目录 1. 为什么消息队列会丢消息&#xff1f; 2. 怎么保障消息可靠传递&#xff1f; 2.1 生产者不丢消息 2.2 服务端不丢消息 2.3 消费者不丢消息 3. 消息丢失如何快速止损&#xff1f; 3.1 完善监控 3.2 完善止损工具 1. 为什么消息队列会丢消息&#xff1f; 现在主流…

springboot项目get请求下划线转驼峰@JsonProperty注解失效问题

问题&#xff1a;解决sprigboot项目get请求中有下划线的入参参数&#xff0c;如&#xff1a;first_name&#xff0c;希望在项目中将下划线格式转成firstName&#xff0c;用JsonProperty注解发现失效问题 1.核查&#xff1a;JsonProperty注解对应包是否正确 正确包&#xff1a…

k8s--使用cornJob定时执行sql文件

CronJob apiVersion: batch/v1beta1 kind: CronJob metadata:name: hello spec:schedule: "0 * * * *"jobTemplate:spec:template:spec:containers:- name: postgres-alpineimage: xxxximagePullPolicy: IfNotPresentcommand:- psql- -h- 数据库服务地址- -d- 数据库…

【PyQt5:Qtimer,QThread】创建计时器和线程,常用于信号与槽机制

文章目录 1、QTimer2、QThread 1、QTimer 计时器&#xff0c;timeout信号表示计时结束时&#xff0c;可以connect一个槽函数slot self.qtimer QTimer(self) self.qtimer.setSingleShot(True) # 设置计时器启动时只触发一次 self.qtimer.timeout.connect(slot) self.qtimer.s…

vue的axios进行ajax请求----ajax请求篇(三)

一、在vue框架中首先需要先安装依赖 npm install axios --save 二、在项目中的main.js中引用 import axios from axios;Vue.prototype.$axios axios&#xff1b; 三、在页面中进行使用 get 方式 axios({headers:{},url:“ ”, //urlparams:{},}).then(function(res){ …

(leecode)设计循环队列

&#xff08;温馨提示&#xff1a;这是博主最最喜欢的歌曲哦&#xff0c;没有之一&#xff09; 题目&#xff1a; 题解&#xff1a; 思路&#xff1a; 方法一(数组)&#xff1a; 方法二(链表)&#xff1a; 题目&#xff1a; 设计你的循环队列实现。 循环队列是一种线性数…

docker 部署 xxl-job-admin

1、先安装mysql docker pull mysql 2、运行mysql 容器 &#xff08; 端口 3306 容器名称 mysql 密码 123456 &#xff09; docker run -d --name mysql -e MYSQL_ROOT_PASSWORD123456 -p 3306:3306 mysql 3、将tables_xxl_job.sql文件&#xff08;官网地址&#xff1a;http…

java spring cloud 企业工程管理系统源码+二次开发+定制化服务 em

Java版工程项目管理系统 Spring CloudSpring BootMybatisVueElementUI前后端分离 功能清单如下&#xff1a; 首页 工作台&#xff1a;待办工作、消息通知、预警信息&#xff0c;点击可进入相应的列表 项目进度图表&#xff1a;选择&#xff08;总体或单个&#xff09;项目显…