gpt4 book ai didi

grails - Grails分页无法正常工作

转载 作者:行者123 更新时间:2023-12-02 14:50:54 25 4
gpt4 key购买 nike

我是Grails的新手,并且尝试使用文档中的分页标签显示特定数据。

分页显示在我的页面上,但是当我在页面上滑动时,它会在每页上显示我的所有数据,我希望每页仅显示10个项目。

这是我的 Controller

class HomeController {

def actions() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)

def articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems()

return [articles: articleActionOpera, actionTotal: articleActionOpera.size(), params:params]
}
}

这是我的服务
class NewSuggestedShoppingItemsService {

def operaOpusDbConnectorService

def getNewSuggestedShoppingItems() {

def returnRows = null
def sqlInstance = operaOpusDbConnectorService.openOperaOpusConnection()

try {
def sqlQuery = "SELECT passW, ArtName, Descript FROM dbo.fnWS_Newarticles()"

returnRows = sqlInstance.rows(sqlQuery)

return returnRows
}
catch(Exception ex) {
log.error("Fail, item are not shown.")
throw new Exception (ex)
}
}
}

和我的.gsp

...
<div id="paging">
<g:paginate total="${actionTotal}" />
</div>

最佳答案

实际上,您实际上没有将任何分页参数(最大和偏移)传递给数据库查询,因此无法获取所有记录。因此,将它们传递给您的查询即可。

第二件事,您将总数作为返回结果的大小(值正确但在编程上错误)传递给Grails分页指令,并将最大/偏移传递给Grails分页指令,即为什么它呈现正确的分页HTML代码段。

例:

class HomeController {

def actions(Integer max) {
params.max = Math.min(max ?: 10, 100)

List articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems()

// You don't have to pass params in model, it is bydefault available in GSPs
return [articles: articleActionOpera, actionTotal: articleActionOpera.totalCount]
}
}

服务:
class NewSuggestedShoppingItemsService {

List getNewSuggestedShoppingItems(Map params) {
// Consider ShoppingItem is a domain class and params is containing max &
// offset parameters then it will return an instance of PagedResultList (List)
// which will have a totalCount field

List shoppingItems = ShoppingItem.createCriteria().list(params) {
// Your criteria
eq("status", "ACTIVE")
}


return shoppingItems
}
}

在此处阅读更多信息 http://grails.github.io/grails-doc/latest/ref/Domain%20Classes/createCriteria.html

关于grails - Grails分页无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com