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

问题】:

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

分析】:

数据库中有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>
结果显示】:

原来样式:



完成后样式:

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值