2016-08-09 33 views
2

嗨我試圖使用兩個下拉組合框列出商店。如果你沒有選擇國家或城市列出所有商店。根據城市國家或兩者的其他方式列出。順便說一下,我沒有創建控制器,我使用generate-all生成了tehm。 這裏是我的看法;g:選擇在grails中使用兩個組合框

<g:form action="index" method="POST"> 
      <div class="fieldcontain"> 
       <g:select name="ddlCountry" noSelection="[null:message(code:'default.select.label',default:'Seçiniz...')]" 
       from="['UK', 'NL', 'DE']" 
       value="${params.ddlCountry}"/> 
       <g:select name="ddlCity" 
          from="['AMSTERDAM', 'Erfurt', 'Manchester','London']" 
          value="${params.ddlCity}"/> 

       <input class="btn btn-danger" type="submit" value="Listele" /> 
       <g:each in="${shopList}" status="i" var="shopInstance"> 
        <tr class="${(i % 2) == 0 ? 'even' : 'odd'}"> 
         <td> 
          <g:link controller="shop" action="show" params="[id:shopInstance.id]"> 
           ${fieldValue(bean: shopInstance, field: "shopName")} 
          </g:link> 
         </td> 
         <td>${fieldValue(bean: shopInstance, field: "shopAdress1")}</td> 
         <td>${fieldValue(bean: shopInstance, field: "shopPostcode")}</td> 
         <td>${fieldValue(bean: shopInstance, field: "shopCity")}</td> 
         <td>${fieldValue(bean: shopInstance, field: "shopCountry")}</td> 
         <td>${fieldValue(bean: shopInstance, field: "shopDateEdited")}</td> 

        </tr> 
       </g:each> 
      </div> 
     </g:form> 

,這裏是店控制器指數

def index(Integer max) { 
    params.max = Math.min(max ?: 10, 100) 
    if(params.ddlCountry || params.ddlCity) { 
     def shops = Shop.withCriteria { 
      if (params.ddlCountry) { 
       like('shopCountry', '%${params.ddlCountry}%') 
      } 
      if (params.ddlCity) { 
       like('shopCity', '%${params.ddlCity}%') 
      } 

     } 
     [shopList:shops] 
    } 
    else{ 
     respond Shop.list(params), model:[shopCount: Shop.count()] 
    } 


} 

它的上市,每次所有的商店。當我點擊按鈕,頁面清爽,但沒有發生

+0

我終於解決了我的問題。我在這裏寫信也許幫助了某人。 noSelection =「['':message(code:'default.select.label',default:'Seçiniz...')]」我刪除了空值並替換爲''。並在控制器頁面我刪除第一如果和其他博客。代碼工作得很好。 –

回答

1

好像有很多東西需要學習:

創建myController的文件夾內的一個新的模板/文件名爲_index.gsp 裏面把這個位

<g:each in="${shopList}" status="i" var="shopInstance"> 
         <tr class="${(i % 2) == 0 ? 'even' : 'odd'}"> 
          <td> 
           <g:link controller="shop" action="show" params="[id:shopInstance.id]"> 
            ${fieldValue(bean: shopInstance, field: "shopName")} 
           </g:link> 
          </td> 
          <td>${fieldValue(bean: shopInstance, field: "shopAdress1")}</td> 
          <td>${fieldValue(bean: shopInstance, field: "shopPostcode")}</td> 
          <td>${fieldValue(bean: shopInstance, field: "shopCity")}</td> 
          <td>${fieldValue(bean: shopInstance, field: "shopCountry")}</td> 
          <td>${fieldValue(bean: shopInstance, field: "shopDateEdited")}</td> 

         </tr> 
        </g:each> 

更改爲:

  <!-- add onChange function to select you could look up item on change through jquery instead--> 
      <g:select name="ddlCity" 
         from="['AMSTERDAM', 'Erfurt', 'Manchester','London']" 
         value="${params.ddlCity}" onChange="verifyCity(this.value)"/> 

      <input class="btn btn-danger" type="submit" value="Listele" /> 
      <!-- put a wrapper div ID around actual results --> 
      <div id="results"> 
      <!-- make it render template now the controller action renders same content for this bit --> 
      <g:render template="/myController/index" /> 
      </div> 
      <!-- END Wrapper --> 
     </div> 


     <script> 
     //Write some java script to handle the actions clicked 

     //VerifyCity will work on city click 
     //Hopefully this should be all it needs it gets a value builds a data array passes it to load results 
     function verifyCity(value) { 
     // 
      var data={ddlCity:value} 
      loadResults(data); 
     } 

     //Same as above for country 
     function verifyCountry(value) { 
      var data={ddlCountry:value} 
      loadResults(data); 
     } 

     //This runs a jquery post to the CONTROLLERNAME - define this and your action 
     //when it has a success it updates results DIV with the content 
     function loadResults(data) { 
      $.ajax({timeout:1000,cache:true,type: 'post',url: "${g.createLink(controller: 'CONTROLLERNAME', action: 'index')}", 
      data:data, 
      success: function(output) { 
       $('#results').html(output); 
      } 
     }); 
     </script>    

當它正常呈現它在模板中調用時,顯示結果的段現在位於其自己的模板中。當ajax調用時,它會呈現特定的模板。

現在一些更改控制器

def index(Integer max) { 
    params.max = Math.min(max ?: 10, 100) 

    if(params.ddlCountry || params.ddlCity) { 
     def shops = Shop.withCriteria { 
      if (params.ddlCountry) { 
       like('shopCountry', '%${params.ddlCountry}%') 
      } 
      if (params.ddlCity) { 
       like('shopCity', '%${params.ddlCity}%') 
      } 

     } 
     //If request is coming in via ajax load in a template of the actual results so the bit that is within div id='results' should be actually a template.  
     if (request.xhr) { 
      render (template: 'myController/index', model:[shopList:shops]) 
      return 
     } 
     //This will handle non ajax calls and will load in the index.gsp which includes the site mesh 
     [shopList:shops] 
     return 
    } 

    //no need for else just use return to stop a if statement in a controller 
    respond Shop.list(params), model:[shopCount: Shop.count()] 
    return 
} 

控制器的行爲,因爲它與正常的,如果if (request.xhr)的異常通知控制器,如果這是一個Ajax調用呈現模板_index.gsp而不是指數一樣。 gsp

這兩者之間的區別在於index.gsp具有layout =「main」,這是在網站樣式中加載的sitemesh。該模板是平淡的,並且可以重載現有正常頁面呈現的一段。

+0

謝謝@vahid是的,你是對的我在begininig,你的回答對我來說有點難。如果我在選擇國家或地區後列出商店怎麼辦? –

+0

選擇必須將值發佈到某個東西。所以你可以有一個表單,包含選擇選項並提交,然後在選擇時呈現一個新頁面。或者..當用戶選擇它觸發jquery ajax方法來渲染輸出在給定的div分段。嘗試遵循以上。它應該使用正確的控制器名稱,只要你已經制作了模板。如果沒有問你困擾什麼。 – Vahid

+0

https://github.com/vahidhedayati/grails-liveform-example有一個視頻也可以幫助點擊 – Vahid