首页 > 解决方案 > 对使用 grails 分页感到困惑

问题描述

我是 grails 的新手,对如何使用 <g:paginate> 有点困惑。我有一张桌子,我想将主菜的最大数量设置为 10。我在网上找到的大多数示例都使用了一个列表,并且有一组有助于创建导航的作业和服务。我对此感到困惑,因为我没有在控制器中使用列表。主菜的总数正在通过,但仍然显示每个主菜。如果我单击第 2 页或“下一步”按钮,我的控制器似乎失去了对用户选择生成此列表的 sourceVolume 的跟踪。

对于上下文 - 用户看到的第一页提示他们选择源卷 - 并从那里生成一个表,其中显示源卷中所有主条目(快照)的列表。

我的 selectSnapshotVolume.gsp

<g:form class = "myForm" url="[controller:'Vaultrecovery', action:'doRecoveryConfirmation', method:'post']">
                <table class = "content-table">
                    <thead>
                        <tr>
                            <th>Select</th>
                            <th>Cluster</th>
                            <th>Vserver</th>
                            <th>Volume</th>
                            <th>SnapShot</th>
                        </tr>
                    </thead>
                    <tbody>
                            <g:each in="${destinationVolumes}" var="destinationVolume">
                                <tr>
                                    <td><g:radio name="snapshot" value="$destinationVolume.snapshot" required="true"></g: radio></td>
                                        <td>
                                            ${destinationVolume.cluster}
                                        </td>
                                        <td>
                                            ${destinationVolume.vserver}
                                        </td>
                                        <td>
                                            ${destinationVolume.volume}
                                        </td>
                                        <td><b>
                                                ${destinationVolume.snapshot}
                                            </b></td>
                                    </tr>

                                    <g:hiddenField name="cluster" value="${destinationVolume.cluster}" />
                                    <g:hiddenField name="vserver" value="${destinationVolume.vserver}" />
                                    <g:hiddenField name="volume" value="${destinationVolume.volume}" />
                            </g:each>
                      </tbody>
                                </table> 
                                <div class = "centerMe">
                                  <div class="pagination">
                                    <g:paginate controller='Vaultrecovery' total="${entreeCount.total[0] ?: 0}" />
                                  </div>
                                </div>
                                <div class = "centerMeDescription">
                                  <g:submitButton class="submit " name="submit" value="Submit"/>
                                </div>
</g:form>

这是我的控制器。我没有任何服务、工作或域类。我还删除了控制器中不相关的部分。

class VaultrecoveryController {

    def dataSource_model   // netapp_model
    def dataSource          // dataautomation
    def mailService
    File testFile = new File("/opt/tomcat/work/TEST")
    boolean TEST = testFile.exists()
    
    def index() { }
def selectSourceVolume() {
        log.info "Vaultrecovery Controller - selectSourceVolume"

        def foo = new Sql(dataSource_model)
        String SQL
        SQL = "SELECT distinct(name) FROM volume WHERE (name NOT LIKE '%_dest%' AND name NOT LIKE 'MDV%' AND name NOT LIKE '%\\_\\_%') ORDER BY name"
        log.info "Getting source volumes: " + SQL
        def sourceVolumes = foo.rows(SQL)
        [sourceVolumes: sourceVolumes]
    } // end selectSourceVolume


def selectSnapshotVolume() {
        log.info "Vaultrecovery Controller - selectSnapshotVolume"
       
        def sourceVolume = params.srcvolume
        log.info "SOURCE VOLUME IS: " + sourceVolume
        def foo = new Sql(dataSource)
        String SQL
        SQL = "SELECT cluster, vserver, volume, snapshot FROM netapp_vault WHERE volume LIKE '%" + sourceVolume + "%' ORDER BY snapshot"
        log.info SQL
        def destinationVolumes = foo.rows(SQL)

       
        SQL = "SELECT COUNT(cluster) as total FROM netapp_vault WHERE volume LIKE '%" + sourceVolume + "%' ORDER BY snapshot"

        def entreeCount = foo.rows(SQL);

        [sourceVolume: sourceVolume, destinationVolumes: destinationVolumes, entreeCount: entreeCount]
    } // end selectSnapshotVolume
}

标签: javahtmlgrailsgroovypagination

解决方案


您丢失了搜索条件,因为 Prev 或 Next 按钮没有将您的 Source Volume 值发布回控制器。

我在我的项目中遇到了同样的问题。花了我半天时间才弄明白。如果您检查 HTML 上的 Prev 和 Next 按钮是什么,它们只会将 Max 和 Offset 值回传给控制器。请记住这一点。这种项目是无状态的。因此,您的操作将重置回初始状态。

<a href="/book/index?offset=10&max=10" class="nextLink">Next</a>

我使用私有变量来存储我上次使用的搜索条件。我的操作检查私有变量是否有任何内容。如果是这样,它将使用私有变量中的条件来运行 SQL。所以,我会从之前的页面中得到相同的记录。然后我应用 Max 和 Offset 来获得正确的记录。

像这样的东西:

class BookController {
   private String _searchString = ''

   def index() {
       _searchString = (params.searchString == null) ? _searchString : params.searchString
       bookList = Book.findAllByName(_searchString, params)
   }
}

params.searchString 将为空,因为 Prev/Next 按钮不会 POST 回 searchString。如果它为空,我从私有变量 _searchString 中获取 searchString 并在我的动态查找器中使用它。我很幸运 Grails 在 Prev/Next 调用操作时没有重置 _searchString 变量。


推荐阅读