如何在php中使用接口实现拖拽排序功能

今天就跟大家聊聊有关如何在php中使用接口实现拖拽排序功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

接口设计:

//$ids 这十条数据的id集合,逗号隔开的字符串
//$oldIndex 原始位置,从0开始算
//$newIndex 要拖动的位置
function dragSort($ids,$oldIndex,$newIndex)
{
  //保证查找出来的数据跟前台提交的顺序一致,这里要order by field
  //id 主键 sort 排序值
  $sql = "select id,sort from 表名字 where id in ($ids) order by field(id, " . $ids . ") ";
  $list = "这里省略,就是去数据库找嘛";
  //id集合
  $idArr  = [];
  //排序集合
  $sortArr = [];
  foreach ($list as $item) {
    $idArr[]  = $item['id'];
    $sortArr[] = $item['sort'];
  }
  //记录要拖动的id
  $oldValue = $idArr[$oldIndex];
  //删除这个要拖动的id
  unset($idArr[$oldIndex]);
  //插入新的位置,并自动移位
  array_splice($idArr, $newIndex, 0, $oldValue);
  //重新设置排序
  $set = [];
  for ($i = 0; $i < count($idArr); $i++) {
     $set[$i]['id']  = $idArr[$i];
     $set[$i]['sort'] = $sortArr[$i];
   }
  //保存到数据库省略
}

看完上述内容,你们对如何在php中使用接口实现拖拽排序功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。