您的位置:首页 > 其它

跟着汤阳光同志做一个OA系统(十):论坛管理模块、上移下移功能

2015-12-10 08:41 435 查看
实现上移下移功能,通过在实体中添加一个position字段

1:查询的时候orderbyposition

通过复写findAll方法

@Override
public List<Forum> findAll() {
return getSession().createQuery(//
"FROM Forumf ORDER BY f.position")//
.list();
}

2:position必须唯一

通过复写save方法,把position设置为自己的id

@Override
publicvoid save(Forum forum) {
//保存
super.save(forum);
//设置position的值
forum.setPosition(forum.getId().intValue());
}

3:上移下移就是通过修改对象的position

//需要找到我的上一个对象

子查询

排序分页取第一个方式





publicvoid moveUp(Long id) {
//找出相关的Forum
Forum forum = getById(id);//
当前要移动的Forum
Forum other = (Forum) getSession().createQuery(//我上面的那个Forum
"FROM Forumf WHERE f.position<? ORDER BY f.position DESC")//
.setParameter(0, forum.getPosition())//
.setFirstResult(0)//
.setMaxResults(1)//
.uniqueResult();

//最上面的不能上移
if (other ==null) {
return;
}

//交换position的值
int temp = forum.getPosition();
forum.setPosition(other.getPosition());
other.setPosition(temp);

//更新到数据中(可以不写,因为对象现在是持久化状态)
getSession().update(forum);
getSession().update(other);
}

publicvoid moveDown(Long id) {
//找出相关的Forum
Forum forum = getById(id);//
当前要移动的Forum
Forum other = (Forum) getSession().createQuery(//我下面的那个Forum
"FROM Forumf WHERE f.position>? ORDER BY f.position ASC")//
.setParameter(0, forum.getPosition())//
.setFirstResult(0)//
.setMaxResults(1)//
.uniqueResult();

//最下面的不能下移
if (other ==null) {
return;
}

//交换position的值
int temp = forum.getPosition();
forum.setPosition(other.getPosition());
other.setPosition(temp);

//更新到数据中(可以不写,因为对象现在是持久化状态)
getSession().update(forum);
getSession().update(other);
}

4:最上面的不能上移,最下面的不能下移

<s:iteratorvalue="#forumList"status="status">
<trclass="TableDetail1template">
<td>${name} </td>
<td>${description} </td>
<td>
<s:aaction="forumManage_delete?id=%{id}"onclick="return
delConfirm()">删除</s:a>
<s:aaction="forumManage_editUI?id=%{id}">修改</s:a>

<!--最上面的不能上移 -->
<s:iftest="#status.first">
<spanclass="disabled">上移</span>
</s:if>
<s:else>

<s:a action="forumManage_moveUp?id=%{id}">上移</s:a>
</s:else>

<!--最下面的不能下移 -->
<s:iftest="#status.last">
<spanclass="disabled">下移</span>
</s:if>
<s:else>
<s:aaction="forumManage_moveDown?id=%{id}">下移</s:a>

</s:else>

</td>
</tr>
</s:iterator>

.disabled{

color: gray;

cursor: pointer;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: