2016-08-09 74 views
2

Hallo Ich versuche, Geschäfte mit zwei Dropdown-Combobox aufzulisten. Wenn Sie nicht Land oder Stadt auswählen, listen Sie alle Geschäfte auf. Andere Weg-Liste nach Stadt Land oder beide von ihnen. Übrigens habe ich keinen Controller erstellt, den ich mit generate-all generiere. hier ist meine Ansicht;g: Wählen Sie mit zwei Combobox in 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> 

und hier ist eine Shop-Controller Index

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()] 
    } 


} 

Es alle Shops jedes Mal aufgeführt wird. Wenn ich auf Schaltfläche Seite ist erfrischend, aber nichts passiert

+0

Ich löste mein Problem schließlich. Ich schreibe hier vielleicht jemandem geholfen. noSelection = "['': message (code: 'default.select.label', standard: 'Seçiniz ...')]" Ich habe null gelöscht und durch '' ersetzt. Und auf der Controllerseite lösche ich zuerst if und else blog. Und der Code funktioniert sehr gut. –

Antwort

1

Scheint, wie es viel zu lernen:

Erstellen einer neuen Vorlage/Datei in MyController Ordner namens _index.gsp im Inneren dieses Bit setzen

<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> 

ändern Sie diese an:

  <!-- 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>    

Das Segment, das Ergebnisse in einer eigenen Vorlage angezeigt hat, wenn es normalerweise rendert, ruft es in Vorlage auf. Wenn ein Ajax-Aufruf erfolgt, wird diese spezifische Vorlage gerendert.

Nun einige Änderungen an Ihrem Controller

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 
} 

Die Steuerung reagiert, wie es normalerweise mit Ausnahme, wenn if (request.xhr) hat, die den Controller sagt, ob dies ein Ajax-Aufruf ist eher Vorlage _index.gsp machen als Index. gsp

Der Unterschied zwischen diesen beiden ist, dass index.gsp hat layout = "main" Dies ist der Sitemesh, der in den Stilen der Website geladen wird. Die Vorlage ist einfach und kann ein Segment einer vorhandenen normalen Seite, die gerendert wurde, überladen.

+0

danke @vahid ja du hast Recht Ich bin am Anfang und deine Antwort ein bisschen schwer für mich. Was ist, wenn ich Shops nach einem bestimmten Land oder einer bestimmten Stadt aufgelistet habe? Wie kann ich das tun? –

+0

Eine Auswahl muss den Wert auf etwas setzen. Sie können also ein Formular um ausgewählte Optionen legen und abschicken, das dann bei Auswahl eine neue Seite rendert. Oder .. wenn der Benutzer es auswählt, wird die jquery ajax-Methode ausgelöst, um die Ausgabe über ein gegebenes div-Segment zu rendern. Versuchen Sie oben zu folgen. Es sollte mit korrekten Controllernamen funktionieren und solange Sie eine Vorlage erstellt haben. Wenn das nicht klappt, frag, wo du feststeckst. – Vahid

+0

https://github.com/vahidhedayati/grails-liveform-example hat ein Video zu helfen könnte Ding klicken in – Vahid