Home  >  Article  >  Backend Development  >  PHP interface implements drag-and-drop sorting function example

PHP interface implements drag-and-drop sorting function example

不言
不言Original
2018-05-03 15:05:551668browse

This article mainly introduces the examples of the drag and drop sorting function implemented by the PHP interface. It has certain reference value. Now I share it with you. Friends in need can refer to it

How do weLearnThe list drag and drop sorting function, but how to deal with the back-end interface is a tangled problem. How to achieve the highest efficiency?

Analysis first In a scenario, if there is a page with ten pieces of data, the so-called dragging means dragging the ten pieces of data back and forth, but each drag will affect other data
For example, if you drag the last piece to the front, then the following The nine bars will automatically move backward, and vice versa. Well~~~

First imagine that the sorting number is fixed, just like there are ten chairs, and each chair is fixed there, and the moving People above, this will not affect the data on other pages
And each person changes the table and chair number of other people before, so there is no need to think about how much to add to rank.

Interface design

//$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];
     }    //保存到数据库省略}

Related recommendations:
WeChat public account development mode PHP interface implementation

The above is the detailed content of PHP interface implements drag-and-drop sorting function example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn