2013-11-09 31 views
0

我有一個搜索網頁,用戶可以通過一個人的種族篩選搜索結果,作爲複選框組。有12個「種族」複選框。該PARAMS獲得通過到G:分頁爲以下,從而使用戶可以通過搜索結果頁面,並保留什麼的種族複選框選中:擺脫在Grails分頁中的不需要的參數

<g:paginate controller="search" action="list" total="${resultCount}" params="${params}"/> 

什麼獲取輸出的鏈接,其中包含了一堆不必要的數據每個內置網址:

<a href="/myapp/search/list?_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_ethnicity=&amp;_gender=&amp;_gender=&amp;_gender=&amp;accountType=2&amp;ethnicity=1&amp;ethnicity=5&amp;max=3&amp;offset=3" class="step">2</a> 

我想分頁鏈接URL可輸出無所有的額外_ethnicity變量,它們會在原始搜索後傳回:

<a href="/myapp/search/list?accountType=2&amp;ethnicity=1&amp;ethnicity=5&amp;max=3&amp;offset=3" class="step">2</a> 

我怎樣才能得到參數paginate標籤沒有所有額外的不必要的領域?在功能上它有效,但分頁get請求的URL太長,看起來很可怕。

回答

1

以前的用戶,這個工程可以過濾掉額外的字段,儘管它很醜。

params="${params.findAll { a -> 
    if (!a.key.toString().startsWith("_")) { 
     return a.value 
    } 
    } 
}" 

編輯:

其實一個更清潔的方式是把這個控制器:

params.keySet().asList().each { if (it.toString().startsWith("_")) params.remove(it) } 

然後在g:paginate你可以堅持

params="${params}" 
2

試試這個..,。

<g:paginate controller="search" action="list" total="${resultCount}" params="${params.findAll { it.key == 'ethnicity' && it.value }}"/> 

它給你

<a href="/myapp/search/list?ethnicity=1&ethnicity=5" class="step">2</a> 

一個骯髒的方式來實現你想要的是

<g:paginate controller="search" action="list" params="${ 
    params.findAll { a -> 
     if (a.value instanceof Collection) { 
      def c = a.value.findAll { b -> 
       return b 
      } 
      if (c) { 
       return c 
      } 
     } else { 
      return a.value 
     } 
    } 
}"/> 

編輯:

spock99答案比我的好多了,還有一種方法是

params="${params.findAll { !it.key.toString().startsWith("_") }}"