java实现列表的上下移动和置顶操作

来源:互联网 发布:龙文章的真实身份知乎 编辑:程序博客网 时间:2024/06/01 08:22

一、上下移动
1、在数据库表中新添加一列名为sort,初始值与各条数据的id相同,初始list排序按照sort的值排序。
2、思路是向上移动的时候,查询出当前数据的上一条数据的sort值,两者交换sort值,同理向下移动的时候,查询出下一条数据的sort值,两者交换。
3、ssm框架中的sql及代码实现
1) sql

    <select id="infoBySortUp" parameterType="Map" resultType="com.loan.entity.WExcellentCourseEntity">        SELECT * from w_excellent_course c WHERE c.sort &lt; #{sort} ORDER BY c.sort DESC limit 0,1    </select>    <select id="infoBySortDown" parameterType="Map" resultType="com.loan.entity.WExcellentCourseEntity">        SELECT * from w_excellent_course c WHERE c.sort &gt; #{sort} ORDER BY c.sort limit 0,1    </select>    <select id="infoBySort" parameterType="Map" resultType="com.loan.entity.WExcellentCourseEntity">        SELECT * from w_excellent_course where sort=#{sort}    </select>    <update id="updateBySort" parameterType="Map">        UPDATE w_excellent_course SET sort=#{sort} WHERE id=#{id}    </update>

2) service

    public WExcellentCourseEntity sortUp(String sort);    public WExcellentCourseEntity sortDown(String sort);    public WExcellentCourseEntity infoBySort(String sort);    public void updateBySort(int sort, int id);

3) controller

@RequestMapping(value = "/courseSortUpdate",method = RequestMethod.GET,produces = {"text/html;charset=UTF-8"})    public String courseSortUpdate(HttpServletRequest request){        String sort=request.getParameter("sort");        String sort1=sort.substring(0,6);        WExcellentCourseEntity entity=courseService.infoBySort(sort1);        if(sort.substring(sort.length()-1).equals("1")){            WExcellentCourseEntity entity1=courseService.sortUp(sort1);           courseService.updateBySort(entity1.getSort(),entity.getId());            courseService.updateBySort(Integer.parseInt(sort1),entity1.getId());        }        if(sort.substring(sort.length()-1).equals("2")){            WExcellentCourseEntity entity2=courseService.sortDown(sort1);            courseService.updateBySort(entity2.getSort(),entity.getId());            courseService.updateBySort(Integer.parseInt(sort1),entity2.getId());        }        return "operation/ecList";    }

4)页面js

<script>    $(function () {        var $up=$(".up");        $up.click(function(){            var $tr=$(this).parents("tr");            if($tr.index()!=0){                //$tr.fadeOut().fadeInt();                $tr.prev().before($tr);            }        });        var $down=$(".down");        var len=$down.length;        $down.click(function(){            var $tr=$(this).parents("tr");            if($tr.index()!=len-1){                $tr.next().after($tr);            }        });    });</script>

5) 相应位置

<input type="button" class="up"  onclick="changeSort(${course.sort}+'1')" value="↑"/><input type="button" class="down"  onclick="changeSort(${course.sort}+'2')" value="↓"/>

二、列表中某行的置顶操作
1、思路依然是sort值的交换,只是除了普通的交换之外,还需要list的Collections.swap()方法。
2、如下是一个简单的demo:

public class stringTest {    public static void main (String[] args){        List<userTest> list=new ArrayList<>();        userTest u1=new userTest();        u1.setId(1);        u1.setName("a");        u1.setSort(1);        userTest u2=new userTest();        u2.setId(2);        u2.setName("b");        u2.setSort(2);        userTest u3=new userTest();        u3.setId(3);        u3.setName("c");        u3.setSort(3);        userTest u4=new userTest();        u4.setId(4);        u4.setName("d");        u4.setSort(4);        userTest u5=new userTest();        u5.setId(5);        u5.setName("e");        u5.setSort(5);        userTest u6=new userTest();        u6.setId(6);        u6.setName("f");        u6.setSort(6);        userTest u7=new userTest();        u7.setId(7);        u7.setName("g");        u7.setSort(7);        list.add(u1);        list.add(u2);        list.add(u3);        list.add(u4);        list.add(u5);        list.add(u6);        list.add(u7);        //1、交换list的0号数据和6号数据        swap2(list, 6, 0);        for (userTest e : list) {            System.out.println(e.getId()+" "+e.getName() + " "+e.getSort()+" ");        }        System.out.println("-------------");        //2、依次交换sort数据        for(int i=0;i<list.size()-1;i++){            int sort=list.get(i+1).getSort();            list.get(i+1).setSort(list.get(i).getSort());            list.get(i).setSort(sort);        }        for (userTest e : list) {            System.out.println(e.getId()+" "+e.getName() + " "+e.getSort()+" ");        }    }    public static <T> void swap2(List<T> list, int oldPosition, int newPosition) {        if (null == list) {            throw new IllegalStateException("The list can not be empty...");        }        if (oldPosition > newPosition) {            for (int i = oldPosition; i > newPosition; i--) {                Collections.swap(list, i, i - 1);            }        }    }}

运行截图如下:
这里写图片描述
可以看到list已经按照我们所想排列。
这两个功能实现只是我个人浅薄的认识,若有更好的解决办法还望各位大神指教。