目录

前言

一、树状图示例

1.展示需求

二、解决方案

1.分析:

2.排序代码

总结





前言

公司突然有个需求就是把之前部门树状图是按部门添加时间来排序的,今天突然说要用户可以随意拖动排序于是就有了今天的内容分享.


一、树状图示例

1.展示需求

公司的需求是可以在树状图列表里随意拖动插入比如把红框勾选的部门插入到生产部的前或后

然后其他部门的排列顺序不变

java 列表拖拽排序 java实现拖拽排序_当前对象


二、解决方案

1.分析:

其实这需求我觉得有四种情况分分别是如下图所示,每种情况都需要单独去考虑排序的从几号索引开始排序到几号索引结束排序

java 列表拖拽排序 java实现拖拽排序_java_02

  

2.排序代码

思考良久把完整的代码写了出来,但是并没有抽出来公共部分,大家如果有需求的话自己动手把公共部分抽出来就可以了,我写的时候喜欢先把问题一点一点解决然后再去简化.话不多说上代码!

/**
     * 交换部门排序
     *
     * @param idOne 部门id(主动去挪动的id)
     * @param idTwo 部门id(被动插入的id)
     * @param move  true是插入到前面 false是插入到后面
     * @return
     */
    @Override
    public Object treeSort(String idOne, String idTwo, Boolean move) {
        //拿到主动移动的部门对象
        DepartmentBean departmentBeanMove = departmentMapper.selectById(idOne);
        //拿到被动插入的部门对象
        DepartmentBean departmentBeanInsertion = departmentMapper.selectById(idTwo);

        //做一个判断怕有空值和跨级别的移动
        //因为我这个部门之前是有子父级关系目前不考虑跨级别移动如果 有大家有需要的话可以私信我
        if (departmentBeanInsertion == null || departmentBeanMove == null
                || !departmentBeanMove.getParentId().equals(departmentBeanInsertion.getParentId())) {
            //这个只是封装得返回类而已 大家自己用自己的就行
            return R.failed("禁止跨级别调换");
        }

        //拿到同级(父类)下所有的sort并从小到大依次排序
        LambdaQueryWrapper<DepartmentBean> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DepartmentBean::getParentId, departmentBeanMove.getParentId());
        wrapper.orderByAsc(DepartmentBean::getSort);
        List<DepartmentBean> list = baseMapper.selectList(wrapper);

        //根据id获取到主动挪动的对象所在的索引
        // (如果大家通过创建时间等其他方式去获取的话 只需要改一下get(x)获取的属性去对比即可)
        int moveIndex = IntStream.range(0, list.size()).filter(x -> list.get(x).getId().equals(idOne)).findFirst().orElse(-1);
        //根据id获取到被动插入的对象所在的索引
        int insertionIndex = IntStream.range(0, list.size()).filter(x -> list.get(x).getId().equals(idTwo)).findFirst().orElse(-1);

        //拿到主动挪动对象的Sort值
        Integer x = departmentBeanMove.getSort();

        //插入到前面
        if (move) {
            //说明是大数插入到小数前面那就定位循环到小数所在的位置
            if (departmentBeanMove.getSort() > departmentBeanInsertion.getSort()) {
                //从挪动对象的前一位开始循环
                for (int i = moveIndex - 1; i >= insertionIndex; i--) {
                    //保存住当前对象的sort
                    departmentBeanMove.setSort(list.get(i).getSort());
                    //把挪动对象的前一位的sort值变到挪动对象的 好像让5去等于6
                    list.get(i).setSort(x);
                    //更新
                    departmentMapper.updateById(list.get(i));
                    //再让X去等于挪动对象的值也就是5的值
                    x = departmentBeanMove.getSort();
                }
            } else {
                //小数插入到大数前面(没可能相等所以不考虑这种情况)
                //从挪动对象的后一位开始循环到被插入对象的前一位数
                for (int i = moveIndex + 1; i <= insertionIndex - 1; i++) {
                    //保存住当前对象的sort
                    departmentBeanMove.setSort(list.get(i).getSort());
                    //把挪动对象后一位的值变到挪动对象的 好像让7去等于6
                    list.get(i).setSort(x);
                    //更新
                    departmentMapper.updateById(list.get(i));
                    //再让X去等于挪动对象的值也就是7的值
                    x = departmentBeanMove.getSort();
                }
            }
        } else {
            //插入到后面
            //说明是大数插入到小数的后面那就定位循环到小数所在的后一个位置
            if (departmentBeanMove.getSort() > departmentBeanInsertion.getSort()) {
                //从挪动对象的前一位开始循环 循环到插入对象的后一位
                for (int i = moveIndex - 1; i >= insertionIndex + 1; i--) {
                    //保存住当前对象的sort
                    departmentBeanMove.setSort(list.get(i).getSort());
                    //把挪动对象前一位的值变到挪动对象的 好像让5去等于6
                    list.get(i).setSort(x);
                    //更新
                    departmentMapper.updateById(list.get(i));
                    //再让X去等于挪动对象的值也就是5的值
                    x = departmentBeanMove.getSort();
                }
            } else {
                //说明是小数插入到大数的后面那就定位循环到大数所在的位置
                //从挪动对象的后一位开始循环 循环到插入对象
                for (int i = moveIndex + 1; i <= insertionIndex; i++) {
                    //保存住当前对象的sort
                    departmentBeanMove.setSort(list.get(i).getSort());
                    //把挪动对象前一位的值变到挪动对象的 好像让7去等于6
                    list.get(i).setSort(x);
                    //更新
                    departmentMapper.updateById(list.get(i));
                    //再让X去等于挪动对象的值也就是7的值
                    x = departmentBeanMove.getSort();
                }
            }
        }
        //最后更新移动者;
        departmentMapper.updateById(departmentBeanMove);

        return R.ok();
    }

总结

这里给大家提供一种思路,希望可以帮助到大家.有更好的思路也可以在评论区里一起探讨一下