Retrieving array of checkboxes and select boxes from view with controller (grails) -


i looking @ old code , trying reach goal of attaching contact instance id selected (checkbox) reports select box populated remote field. instance, params passed controller:

[id:1, reportstoresults:[x, y, z, ], reportsto.id:[56, 55, 55], reportsto:[id:[56, 55, 55]], contactlist:[55, 57], _contactlist:[, , , ], _select:, action:index, controller:tempcontroller]

what theoretically need such:

contactlist:[ , , 55, 57, ] (where empty spaces checkboxes not selected.     reportsto.id:[ , , 56, 56, ] (where empty spaces empty selection boxes. 

how can pass array of values? because can pass contactlist ids , reportsto.id - cannot match them correctly based on array slot or tying them together.

any idea:?

<!doctype html> <html>     <head>         <meta name="layout" content="main">     </head>     <body>         <div id="company-roster" class="company-roster">         <h1>${companyname} roster</h1>             <g:form>                 <g:hiddenfield name="id" value="${id}" />                 <div class="choicepanel">                     <div class="choicepanel-reportsto">                         <label>save reports additions:</label>                         <g:actionsubmit value="update reports to" action="addreportsto"/>                     </div>                 </div>                 <script>                     function all() {                         $(':checkbox[name=select]').click (function () {                           $(':checkbox[name=contactlist]').prop('checked', this.checked);                         });                                 }                 </script>                 <div style="position:absolute; margin-top:10px; width: 1350px; height: 650px; overflow: auto;">                 <table>                     <thead>                         <tr>                             <th><g:checkbox name="select" onclick="all();"/></th>                             <g:sortablecolumn property="firstname" action="roster" title="${message(code: 'contact.firstname.label', default: 'first name')}" />                             <g:sortablecolumn property="lastname" action="roster" title="${message(code: 'contact.lastname.label', default: 'last name')}" />                             <th>reports to</th>                         </tr>                     </thead>                     <tbody>                         <g:each in="${companyroster}" status="i" var="contactinstance">                                 <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">                                  <td><g:checkbox name="contactlist" value="${contactinstance.id}" checked="${false}"/></td>                                 <td>${fieldvalue(bean: contactinstance, field: "firstname")}</td>                                 <td>${fieldvalue(bean: contactinstance, field: "lastname")}</td>                                 <td>                                     reports to: <g:remotefield action="getreportstoresults" controller="contact" id="" update="rtresult_${contactinstance.id}" paramname="search" name="reportstoresults" value="" />                                     <br/>                                     <g:each in ="${contactinstance?.reportsto}" var="reportsto" status="x">                                         <li style="list-style-type:none;">${reportsto}<g:link controller="contact" action="removereportsto" params="${[reportstoid: reportsto.id, contactinstanceid: contactinstance.id, comp_id:id]}">remove</g:link></li>                                     </g:each>                                 </td>                                 <td>                                     <div id="rtresult_${contactinstance.id}" class="rtresult_${contactinstance.id}">                                         <g:select name="reportsto.id" from="${rtresults}" value="" />                                     </div>                                 </td>                                 </tr>                         </g:each>                     </tbody>                 </table>                 </div>             </g:form>         </div>     </body> </html> 

i giving each check box on form unique , identifiable name corresponding id references in hiddenfield like...

<g:checkbox name="contact.${i}.checkbox" checked="${false}"/> <g:hiddenfield name="contact.${i}.id" value="${contactinstance.id}" /> 

above, used i variable g:each loop ensure each in sequence , unique.

next, you'll need store number of items in g:form controller action knows how many checkbox params for, (out side of g:each in g:form)....

<g:hiddenfield name="contactcount" value="${companyroster.size()}" /> 

finally, can iterate through params map , deal selected or not selected.

def dostuff = {     def contactcount = params.contactcount.tointeger();      for( in 0..contactcount){           def contact = contact.get(params.contact."${i}".id);          if (params.contact."${i}".checkbox){ //<-- return 'on' or ''             println("you selected: ${contact}");         }else{             println("you did not select: ${contact}");         }      } } 

there couple ways deal above solution way i've done it. code not tested syntax , off top of head but, idea. enjoy!


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -