Grails Ajax table - how to implement? -


i've input:

<g:form role="search" class="navbar-form-custom" method="post"         controller="simple" action="addentry">    <div class="form-group">       <input type="text" placeholder="put data here"              class="form-control" name="inputdata" id="top-search">    </div> </g:form> 

and table:

<table class="table table-striped table-bordered table-hover " id="editable">     <thead>         <tr>             <th>name</th>             <th>created</th>         </tr>     </thead>     <tbody>         <g:render template="/shared/entry" var="entry"                   collection="${entries}" />     </tbody>  </table> 

controller:

@secured(['role_user', 'role_admin']) class simplecontroller {  def springsecurityservice def user  def index() {      user =  springsecurityservice.principal.username     def entries = entry.findallbycreatedby(user)     [entries: entries] }   def addentry(){          def entries = entry.findallbycreatedby(user)         render(entries: entries)     } } 

i want dynamically update table data input string. best way? grateful examples/solutions

you can update table using ajax grail's formremote tag.

input form

<g:formremote      name="entryform"      url="[controller: 'entry', action: 'add']"     update="entry">      <input type="text" name="name" placeholder="your name" />     <input type="submit" value="add" /> </g:formremote> 

html table

<table>     <thead>         <tr>             <th>name</th>             <th>created</th>         </tr>     </thead>     <tbody id="entry">         <g:render             template="/entry/entry"              var="entry"             collection="${entries}" />     </tbody> </table> 

entry template

<tr>     <td>${entry.name}</td>     <td>${entry.datecreated}</td> </tr> 

controller

import org.springframework.transaction.annotation.transactional  class entrycontroller {      def index() {         [entries: entry.list(readonly: true)]     }      @transactional     def add(string name) {         def entry = new entry(name: name).save()          render(template: '/entry/entry', collection: entry.list(), var: 'entry')     } } 

how works

when add button pressed add controller method called. controller method creates domain instance , renders _entry.gsp template. instead of refreshing browser page, template rendered ajax response. on client side, rendered template inserted dom inside of tbody element id entry, defined in formremote tag update attribute.

note approach of entries re-rendered, not new one. rendering new 1 bit trickier.

resources


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -