grails - Grails:实现没有域类的分页

标签 grails pagination

我是Groovy Grails的新手,并且想在图像文件列表上实现分页。

在我的代码中,我没有域类,我只是从文件系统中获取图像列表,并在gsp页面上显示图像。

现在,我要对正在显示的图像进行分页。

以下是我的gsp页面,在该页面上显示图像文件。

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="filterList" %>
<html>
<head>
    <title>PC Screen Shots</title>
    <meta content="xenonPC" name="layout">
    <style>
    .mycontent-left {
        border-right: 1px solid #808080;
    }
    </style>
</head>

<body>

<div class="col-md-12">

    <section class="gallery-env">
        <div class="">

            <div class="album-images row">

                <g:set var="selectedUser" value="${selectedUser}"/>
                <g:each in="${imageList}" var="imageName">
                    <!-- Album Image -->
                    <div class="col-md-3 col-sm-4 col-xs-6">
                        <div class="album-image">
                            <a href="#" class="thumb" data-action="edit">
                                <img style="width: auto; height: 160px;"
                                     src="${createLink(controller: "customer", action: "displayImage", params: [imageName: imageName])}"
                                     class="img-responsive">
                            </a>

                            <div>
                                <g:set var="imageNameToDisplay" value="${imageName.toString()}"/>
                                <g:set var="imageNameToDisplay"
                                       value="${imageNameToDisplay.substring(imageNameToDisplay.lastIndexOf("\\") + 1)}"/>

                                <label>${imageNameToDisplay}</label>
                            </div>

                            <div>
                                <a href="#" class="thumb" data-action="edit">
                                    <img style="width: auto; height: 160px;"
                                         src="${createLink(controller: "customer", action: "displayImageDate", params: [imageName: imageName])}"
                                         class="img-responsive"
                                         style="cursor:pointer;">
                                </a>
                            </div>

                            <div class="image-options">

                                <g:link controller="customer" action="downloadImage" params="[imageName: imageName]">
                                    <i class="fa-download"></i>
                                </g:link>

                               <g:link controller="customer" action="deleteImage" params="[imageName: imageName, selectedUser: selectedUser]">
                                    <i class="fa-trash"></i>
                                </g:link>
                            </div>
                        </div>
                    </div>
                </g:each>
            </div>
        </div>
    </section>
</div>
</body>
</html>

提前致谢。

最佳答案

基本上,您将需要将给定列表拆分为特定块的功能。分页依赖于发送,因为您知道偏移量和最大值,使用相同的分页值,您可以使自己的自定义方法与给定列表一起使用:

您需要的实际方法是:

/**
 * paginate usage:
 * paginate(inputList,pagination-params)
 * paginationParams=[offset:params.offset,max:params.max]
 * instanceList=PaginationHelper.paginate(instanceList,paginationParams)
 * @param inputList
 * @param input
 * @return list split based on offset and max
 */
public static List splitList(List inputList, Map input) {
    input.max =  input.max ? (input.max as int) : 1
    input.offset =  input.offset ? (input.offset as int) : 0
    if (input.max < 0 ) return inputList

    def instanceTotal = inputList?.size()
    if ( input.offset < 0 ||  input.offset > instanceTotal) {
        input.offset = 0
    }
    // Ensure pagination does not exceed from array size
    Integer borderNumber =  input.max +  input.offset
    if (borderNumber > instanceTotal) {
        borderNumber = instanceTotal
    }
    // Extract sublist based on pagination
    def objectSubList = inputList.subList(input.offset, borderNumber)
    return objectSubList
}

要使用此方法:
    //your current list
    def myList=[['id':1L,name:'name'],['id':2L,name:'name']]

    //your page total for pagination within gsp
    def instanceListTotal=myList?.size() ?: 0 

    //set your pagination params as per pagination input by user
    def paginationParams = [offset: params.offset, max: params.max]

    // get the helper class above to split it 
    def instanceList= Helper.splitList(myList,paginationParams)

    //respond back with split result and actual total for pagination
    render (view:'view', model:[instanceList:instanceList,instanceListTotal:instanceListTotal])

因此实际上使用上述方法进行分页。不建议使用,但是棘手的情况需要非常规的解决方案。

此处的区别和典型的分页将要执行的操作应该很明显,但应在典型的数据库查询模型中。在这种情况下,没有像myList这样的总体列表。给定起点和返回最大值,然后执行新的查询。
在这种情况下,分页取决于列表大小,并且在工作时,每次分页更改时,都必须一次又一次地加载整个列表。
您可能希望使用某种形式的缓存解决方案来减少此处的工作。

关于grails - Grails:实现没有域类的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775700/

相关文章:

php - 缓存分页结果,清除更新 - 如何解决?

Elasticsearch 分页和限制最大页数

grails - 如何从插件将 Controller 搭建到域类?

grails - 创建新用户时出现Grails Spring Security UI插件错误-处理GroovyPageView时出错

grails - 版本在DateTime之前发布域类

iOS tableView 分页

json - 使用 angularjs 进行 django-rest 分页

php - PHP-MySQL 分页中的序列号

mysql - Grails:如何减少数据库查询

Grails - 更改区域设置行为以使用破折号而不是下划线