2017-01-21 34 views
0

我是Groovy Grails的新手,想要在圖像文件列表上實現分頁。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> 

在此先感謝。

回答

1

基本上,您將需要一個功能,將給定的列表拆分爲特定的塊。分頁是發送依賴,你是知道的偏移和MAX,與相同的分頁值,你可以使自己的自定義的方法與給定名單的工作:

你需要的實際方法是這樣的:

/** 
* 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]) 

所以實際上使用分頁與上述。不建議,但嘿粘滯的情況需要非常規的解決方案。

這裏的區別以及典型的分頁操作應該是明顯的,但是在典型的DB查詢模型中。在這種情況下沒有整體列表myList。它給出了起點和最大值返回,然後去做一個新的查詢。 這種情況下的分頁取決於列表大小,並且在工作時,每次分頁更改時不得不一次又一次地加載整個列表。 您可能希望使用某種形式的緩存解決方案來減少這裏的工作。

+0

謝謝。這正是我所期待的。 –