Grails中的分页概率 [英] Pagination prob in Grails
问题描述
我想在分页中显示某个类别下的所有游戏(每页10个)这是我的控制器
I want to display all games under a certain category in pagination (10/page) This is my controller
def listGame(){
def category = GameCategory.list()
def platform = Platform.list()
def currentCategory = params.categoryName
def myCategory=GameCategory.findByCategoryName(currentCategory)
def games = myCategory.games
[currentCategory:currentCategory, category:category, games:games, platforms:platform, gameCount:Game.count(), chosenPlatform:params.platform]
}
这是我看来的分页代码
<g:each in="${games}" status="i" var="game">
${game.gameTitle}
</g:each>
<div class="pagination">
<g:paginate action="listGame" total="${gameCount}" />
</div>
当我在当前类别中有9场比赛时,第12页出现了,不应出现
When I have 9 games in the current category the page 12 showed up wherein it shouldn't
当我当前类别中有11个游戏时,所有11个游戏都显示在第1页中,而单击第2页将导致此错误
When I have 11 games in the current category all 11 games are displayed in page 1 and clicking page 2 will result in this error
推荐答案
有几件事要解决.
- 确保
categoryName
不会以null
的形式进入控制器. - 利用分页器提供的
max
和offset
参数来选择适当的Game
.
- Make sure
categoryName
is not coming into the controller asnull
. - Make use of the
max
andoffset
parameters that the paginator is providing to select the appropriateGame
s.
类别名称
由于分页器正在创建调用控制器的URL,并且您的控制器需要类别名称,因此分页器必须将类别名称添加到调用控制器操作的参数中.您可以使用代码的 params
属性
<g:paginate action="listGame" total="${gameCount}" params="${[categoryName: currentCategory]}"/>
Max&偏移量
控制器需要使用 max
和 offset
参数来了解要从数据库中检索 Game
的哪些子集并在数据库中呈现看法.但是,在某些情况下,例如访问URL http://blah/someController/listGame 时,没有 max
和 offset
参数.这是您的控制器动作必须能够处理的.它是这样的:
Max & offset
The controller needs to use max
and offset
parameters to know what subset of the Game
s to retrieve from the database and render in the view. However, in some cases, such as when visiting the URL http://blah/someController/listGame, there would be no max
and offset
params. This is something your controller action must be able to handle. It goes something like this:
def max = params.max ?: 10
def offset = params.offset ?: 0
因此,将使用 max
和 offset
参数(如果可用).否则,它们将使用默认值.
So the max
and offset
params will be used if they are available. Otherwise they'll use default values.
最后,您需要使用这些参数来选择 Game
.为此,您可以使用 where 查询:
Finally, you need to use these parameters to select the Game
s. For that you can use a where query:
def games = Game.where {
categories.categoryName == currentCategory
}.list(max: max, offset: offset)
关于返回的 games
对象的一些有趣的事情:它像一个列表,但实际上是一个 grails.gorm.PagedResultList
.这是一件好事,因为它在结果中包含记录的 total 总数(忽略max和offset).换句话说,这就是您的新 gameCount
.
Something interesting about the returned games
object: It acts like a list, but it's actually a grails.gorm.PagedResultList
. That's a good thing because it contains the total number of records in the result (ignoring max and offset). In other words, it's your new gameCount
.
您可以在中阅读有关 where 查询的更多信息.Grails文档.我也有一系列的文章,其中涵盖了位置,条件和HQL查询更详细.
You can read more about where queries in the Grails documentation. I also have a series of articles which cover where, criteria and HQL queries in more detail.
将所有内容放在一起, listGame()
看起来像这样:
Putting it all together, listGame()
would look like this:
def listGame(){
def category = GameCategory.list()
def platform = Platform.list()
def currentCategory = params.categoryName
def max = params.max ?: 10
def offset = params.offset ?: 0
def games = Game.where {
categories.categoryName == currentCategory
}.list(max: max, offset: offset)
[currentCategory:currentCategory, category:category, games:games, platforms:platform, gameCount:games.totalCount, chosenPlatform:params.platform]
}
这篇关于Grails中的分页概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!