我有域"country",在list.gsp上我有搜索块和输入字段.第一个问题是,当我尝试在我的列表上使用paginate时总是显示所有结果,在这种情况下我设置了解决方案并且我只发送了10个值用于显示(如果你知道另一个解决方案请告诉我).我的搜索看起来像这样
def search = {
if(query){
def srchResults = searchableService.search(query, params)
def result = Country.executeQuery("select new map(a.name as name, a.datacenter as datacenter) from Country a where a.name like '%"+ params.q + "%'")
if (params.offset)
{
x = params.offset.toInteger()
y = Math.min(params.offset.toInteger()+9, result.size()-1)
} else
{
x = 0
size = result.size() - 1
y = Math.min(size, 9)
}
def q=params.q
[countryInstanceList: result.getAt(x .. y), countryInstanceTotal:result.size(), q:params.q]
}else{
redirect(action: "list")
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有另一个问题,当我按下一页然后我的搜索字段中的参数是清理,结果为空.我试图将字段值作为参数发送,但我做错了.
我的搜索页面如下:
<g:form action="search">
<div class="search" >
Search Country
<g:hiddenField name="q" value="${params.q}" />
<input type="text" name="q" value="${params.q}" />
<input type="submit" value="Search"/>
</div>
</g:form>
Run Code Online (Sandbox Code Playgroud)
...... ......
如果表中有大量行,这样的分页将会中断。即使在中等大小的表上,它也会非常慢,因为它从数据库中加载每一行。
更好的解决方案是在查询中进行分页。您可以传入用于分页的查询参数映射作为可选的第三个参数executeQuery:
def maxResults = 10
def result = Country.executeQuery(
"select new map(a.name as name, a.datacenter as datacenter) from Country a where a.name like ?",
[params.q],
[max: maxResults, offset: params.offset])
Run Code Online (Sandbox Code Playgroud)
此外,您的表单有两个名为 name 的字段q。不要使用与文本输入同名的隐藏字段。可以使用该value属性指定文本输入的默认值。
最后,您必须将偏移量作为参数传递。Grails 有一个标签可以为您处理所有这些事情:g:paginate。
| 归档时间: |
|
| 查看次数: |
2674 次 |
| 最近记录: |