表格数据的上移与下移功能

来源:互联网 发布:淘宝上网络解锁可靠吗 编辑:程序博客网 时间:2024/06/11 21:42

问题】:

在做项目过程中遇到这个需求,根据显示的需要把列表数据进行上移或者下移的操作。

分析】:

数据库中有position这个字段来管理板块列表显示的顺序,那么当需要上移时,我们需要取到比它position小的,离它最近的一条数据的position值,并把两者进行交换即可。

当数据处于第一条数据时,不能在上移;当数据处于最后一条数据时,不能再下移。

实现】:

Service:

public void moveDown(Long id) {Forum forum=getById(id);Forum otherForum=(Forum) getSession().createQuery("from Forum f where f.position>? order by f.position desc").setParameter(0, forum.getPosition()).setFirstResult(0).setMaxResults(1).uniqueResult();//最下面的不能下移if(otherForum==null){return;}//交换position的值int temp=forum.getPosition();forum.setPosition(otherForum.getPosition());otherForum.setPosition(temp);//更新到数据库中getSession().update(forum);getSession().update(otherForum);}public void moveUp(Long id) {Forum forum=getById(id);Forum otherForum=(Forum) getSession().createQuery("from Forum f where f.position<? order by f.position asc").setParameter(0, forum.getPosition()).setFirstResult(0).setMaxResults(1).uniqueResult();//最上面的不能上移if(otherForum==null){return;}//交换position的值int temp=forum.getPosition();forum.setPosition(otherForum.getPosition());otherForum.setPosition(temp);//更新到数据库中getSession().update(forum);getSession().update(otherForum);}}
Action:

     /** * 上移 */public String moveUp() throws Exception{forumManageService.moveUp(model.getId());return "toList";}/** * 下移 */public String moveDown() throws Exception{forumManageService.moveDown(model.getId());return "toList";}
页面显示jsp:

 <!-- 最上面的不能上移 --><s:if test="#status.first"><span class="disabled">上移</span></s:if><s:else><s:a action="forumManage_moveUp?id=%{id}">上移</s:a></s:else><!-- 最下面的不能下移 --><s:if test="#status.last"><span class="disabled">下移</span></s:if><s:else><s:a action="forumManage_moveDown?id=%{id}">下移</s:a></s:else>
结果显示】:

原来样式:



完成后样式:

0 0
原创粉丝点击