上下移动

import arrayMove from 'array-move'
 
/**
 * @item 当前移动的节点
 * @direction: up | down 移动的方向 
 */
 import arrayMove = from 'arrayMove'
 
function moveDone (item, direction = 'up') {
  const arr = [...data]
  const lastPosition = arr.length - 1 // 最大的位置
  const index = arr.indexOf(item) // 查找当前位置
  let position = direction === 'down' ? index + 1 : index - 1 // 记录移动的位置
  
  // 如果没这条数据
  if (index === -1) return false
 
  // 如果上移 第一条数据
  if (direction === 'up' && index === 0) return false
  
  // 如果下移 最后一条数据
  if (direction === 'down' && lastPosition === index) return false
  
  // 移动位置 并且赋值给 data
  setData(arrayMove(arr, index, position))
}
 

上面的是不需要第一个去最后一个那种的

这个是普通的:

上移:
 const top = (text) => {
    let arr = [...data]
    let index = arr.indexOf(text)
    if(index === 0) return false
    let nowindex = index <= 0 ? arr.length - 1:arr.indexOf(text) - 1
    arr.splice(index, 1)
    arr.splice(nowindex, 0 , text)
    setData(arr)
  }
下移:
  const bom = (text) => {
    let arr = [...data]
    let index = arr.indexOf(text)
    if(index === arr.length - 1) return false
    let nowindex = index >= arr.length - 1 ? 0 :arr.indexOf(text) + 1
    arr.splice(index, 1)
    arr.splice(nowindex, 0 , text)
    setData(arr)
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值