title: Grails filter data with dropdown
body: <p>I want to add a data filter for my table. Can somebody help me?
  I already have written a code for the list controller but I don't
  know how to implement "mitarbeiterSelectedId" in my .gsp file.</p>
  
  <p>At the moment I have the following code snippets.</p>
  
  <p>This is my controller 'list'</p>
  
  <pre><code>def list(Integer max) {
      def mitarbeiterList = Mitarbeiter.listOrderByNachname()
  
      if (params.selectedMitarbeiterId != null &amp;&amp; params.selectedMitarbeiterId != '-1'){
          mitarbeiterList = [Mitarbeiter.findById(params.selectedMitarbeiterId)]
      }
      [mitarbeiterInstanceList: mitarbeiterList, mitarbeiterInstanceTotal: Mitarbeiter.count() ]
  } 
  </code></pre>
  
  <p>This is my list.gsp</p>
  
  <pre><code>    &lt;g:select id="mitarbeiter" name="mitarbeiterId" from="${Mitarbeiter.list()}" optionValue="nachname"
            optionKey="id"
            noSelection="${['-1': 'Bitte auswählen...']}" onchange="selectedMitarbeiterId"/&gt;
  
  &lt;g:actionSubmit value="submit" onclick="" &gt;OK&lt;/g:actionSubmit&gt;
  
  
  &lt;table&gt;
      &lt;thead&gt;
      &lt;tr&gt;
  
          &lt;g:sortableColumn property="nachname" params="[controller: 'mitarbeiter']"
                            title="${message(code: 'mitarbeiter.nachname.label', default: 'Nachname')}"/&gt;
  
          &lt;g:sortableColumn property="vorname" params="[controller: 'mitarbeiter']"
                            title="${message(code: 'mitarbeiter.vorname.label', default: 'Vorname')}"/&gt;
  
          &lt;g:sortableColumn property="sollpensum" params="[controller: 'mitarbeiter']"
                            title="${message(code: 'mitarbeiterHistory.sollpensum.label', default: 'Sollpensum')}"/&gt;
  
          &lt;sec:ifAllGranted roles="ROLE_ADMIN"&gt;
  
              &lt;g:sortableColumn property="gehalt" params="[controller: 'mitarbeiter']"
                                title="${message(code: 'mitarbeiterHistory.gehalt.label', default: 'Gehalt')}"/&gt;
  
          &lt;/sec:ifAllGranted&gt;
          &lt;th&gt;&lt;g:message code="mitarbeiterHistory.bereich.label" default="Bereich"/&gt;&lt;/th&gt;
  
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;g:each in="${Mitarbeiter.list()}" status="i" var="mitarbeiterInstance"&gt;
          &lt;tr class="${(i % 2) == 0 ? 'even' : 'odd'}"&gt;
  
              &lt;td&gt;&lt;g:link action="show" controller="mitarbeiter"
                          id="${mitarbeiterInstance.id}"&gt;${fieldValue(bean: mitarbeiterInstance, field: "nachname")}&lt;/g:link&gt;&lt;/td&gt;
  
              &lt;td&gt;${fieldValue(bean: mitarbeiterInstance, field: "vorname")}&lt;/td&gt;
  
              &lt;td&gt;${fieldValue(bean: mitarbeiterInstance.histories, field: "sollpensum")}&lt;/td&gt;
  
              &lt;sec:ifAllGranted roles="ROLE_ADMIN"&gt;
                  &lt;td&gt;${fieldValue(bean: mitarbeiterInstance.histories, field: "gehalt")}&lt;/td&gt;
              &lt;/sec:ifAllGranted&gt;
  
              &lt;td&gt;${fieldValue(bean: mitarbeiterInstance.histories, field: "bereich")}&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/g:each&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Can somebody help my to fix this?</p>
  
  <h2>EDIT :</h2>
  
  <p>Okay at the moment my class looks like this.</p>
  
  <pre><code>class Employee{
  
  String firstname
  String lastname
  
  static hasMany = [histories:EmployeeHistory]
  
  
  static constraints = {
      firstname nullable: false, blank: false, minSize: 2, matches: "[a-zA-ZäöüÄÖÜßéèáàúùóò'\\- ]+"
      lastname nullable: false, blank: false, minSize: 2, matches: "[a-zA-ZäöüÄÖÜßéèáàúùóò'\\- ]+"
  
  }
  }
  </code></pre>
  
  <p>so when I'm inserting your code it looks like this:</p>
  
  <pre><code>class Employee{
  
  String firstname
  String lastname
  
  static hasMany = [histories:EmployeeHistory]
  
  
  static constraints = {
      firstname nullable: false, blank: false, minSize: 2, matches: "[a-zA-ZäöüÄÖÜßéèáàúùóò'\\- ]+"
      lastname nullable: false, blank: false, minSize: 2, matches: "[a-zA-ZäöüÄÖÜßéèáàúùóò'\\- ]+"
  }
  
  static namedQueries = {
      filteronLastname { String inLastname -&gt;
          if (inLastname &amp;&amp; inLastname?.size() &gt; 0) {
              ilike 'lastname', "%${inLastname}%"
          }
      }
  }
  
  }
  </code></pre>
  
  <p>If nothing is selected in dropdown I want to show the whole table data.<br>
  If I select a employee in dropdown I only want to show table with data of selected employee.</p>
  
  <p>at the moment this is my list controller of employee</p>
  
  <pre><code>def list() {        
      params.max = Math.min(params.max ? params.int('max') : 10, 100)
      def employeeList = Employee.filteronLastname(params?.lastName).list(params)
      [employeeInstanceList: employeeList, employeeInstanceTotal: employeeList.totalCount ]
  }
  </code></pre>
  
  <p>where do I have to insert this:</p>
  
  <pre><code>NewsItem.filterLastname(params?.Lastname).
  list()
  </code></pre>
  
  <p>and how I have to change the <code>&lt;g:selected&gt;</code>?</p>