最近写了一个简单的分页查询如下
输入用户名和密码查询
实现效果如图
queryList中的文件代码
def adminInstance=new Admin(params)
//如果没有输入内容则返回
if(adminInstance.userName.trim()==”"&&adminInstance.userPass.trim()==”"){
flash.message=”${message(code:’query.error’,default:’query error!!’)}”
return
}else{
//创建queryList的查询对象
def adminQueryList=Admin.createCriteria()
params.max = params.max ? params.int(‘max’) : 5
//创建查询条件
def queryResult=adminQueryList.list(max:params.max,offset:params.offset){
like(“userName”,”%”+params.userName+”%”)
or{
like(“userPass”,”%”+params.userPass+”%”)
}
//根据id倒叙排列
order(“id”,”desc”)
}
def adminQuerySize=Admin.createCriteria()
def queryResultSize=adminQuerySize{
like(“userName”,”%”+params.userName.trim()+”%”)
or{
like(“userPass”,”%”+params.userPass.trim()+”%”)
}
projections{ rowCount() }
}
render(view:”queryList”,model:[adminInstances: queryResult,queryResultSize:queryResultSize[0],params:params])}
gsp中的分页为
<div class=”list”>
<g:each in=”${adminInstances}” status=”i” var=”adminInstance”>
<tr class=”${(i % 2) == 0 ? ‘odd’ : ‘even’}”>
<td><g:link action=”show” id=”${adminInstance.id}”>
${fieldValue(bean: adminInstance, field: “id”)}
</g:link></td>
<td>
${fieldValue(bean: adminInstance, field: “userName”)}
</td>
<td>
${fieldValue(bean: adminInstance, field: “userPass”)}
</td>
<td width=”140px”><g:link action=”show”
id=”${adminInstance.id}”>查看</g:link> | <g:link action=”edit”
id=”${adminInstance.id}”>编辑</g:link> |
<g:link url=”delete?id=${adminInstance.id}” οnclick=”return confirm(‘您确定删除吗?’)” >删除</g:link>
</td>
</tr>
</g:each>
</div>
<div class=”paginateButtons” style=”width: 100%”>
<div style=”float: left; width: 300px”>
<g:paginate total=”${queryResultSize}” params=”${params}” ></g:paginate>
</div>
<div style=”float: right; width: 200px;”>
</div>
</div>
</div>