我是Grails的新手,我尝试使用文档中的分页标签来显示特定数据。
分页显示在我的页面上,但是当我浏览它时,它会在每个页面上显示我的所有数据,并且我希望每页只显示总共10个项目。
这是我的控制器
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>
答案 0 :(得分:0)
实际上,您没有将任何分页参数(max&amp; offset)传递给数据库查询,因此获取所有记录。因此,将它们传递给您的查询,这将有效。
第二件事,您将总计数作为返回结果的大小(其中值正确但编程错误)和最大/偏移量传递给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
答案 1 :(得分:0)
您好Damir使用以下说明,您将轻松实现grails中的分页:
在视图中您的代码没问题:(无需更改)
<div id="paging">
<g:paginate total="${actionTotal}" />
</div>
在控制器中:
class HomeController {
def actions(Integer max) {
params.max = Math.min(max ?: 10, 100)
//change here
List articleActionOpera = newSuggestedShoppingItemsService.getNewSuggestedShoppingItems(params)
// You don't have to pass params in model, it is bydefault available in GSPs
return [articles: articleActionOpera, actionTotal: articleActionOpera.totalCount,params:params]
}
}
在服务使用中创建标准:
class NewSuggestedShoppingItemsService {
def operaOpusDbConnectorService
def getNewSuggestedShoppingItems(def params) {
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)
}*/
returnRows=YourDomainName.createCriteria().list(params) {
projections {
property("passW")
property("ArtName")
property("Descript")
}
}
}
}
注意:必须更改YourDomainName ==映射到表的类的名称 它会正常工作。