Reputation: 11665
I am new to grails, I want to write the content of html table 'cart_table' (currently in a shoppingcart.gsp) to a _carttemplate.gsp and show this table in another gsp (like summary.gsp) by include this new template.
Currently I have the table defined in shoppingcart.gsp as follows:
<table id="newTable" style="display:none">
<thead><th>item name</th><th>desc</th><th>business justification</th><th>start date</th><th>end date</th></thead>
<tbody></tbody>
</table>
and I populate the body as follows
$("#confirmbutton").click(function(){
var ntr='',//to store html for new table row
rows=[],//to collect new rows
$tbl=$("#table_rolecart tbody"),//original table
l=$("tr", $tbl).length;//length of rows in original table's tbody section
var row, brow, drow;
:
: /* getting rows from other tables */
}
$("#newTable tbody").append(rows.join(''));
How do I put the table into the template.gsp? and in the confirm page how do I display the table?
Upvotes: 2
Views: 4130
Reputation: 3243
This will explain in further detail about rendering a template: http://grails.org/doc/latest/ref/Tags/render.html
For your code it sounds like you would just do this in shoppingcart.gsp and summary.gsp:
<g:render template="carttemplate" />
Then _carttemplate.gsp would look like this:
<table id="newTable" style="display:none">
<thead><th>item name</th><th>desc</th><th>business justification</th><th>start date</th> <th>end date</th></thead>
<tbody></tbody>
</table>
The only kinda tricky part is how you're populating the data via javascript. If the multiple places you need it can reuse the same javascript than you can put your script in a .js file and reference it from any gsp that renders your template. If they are different then you're not going to get a whole lot of reuse.
Also note that you may need to specify the directory of the template in the g:render tag. Let's assume this structure:
shoppingcart.gsp can do:
<g:render template="carttemplate" />
But summary.gsp would need to do:
<g:render template="/cart/carttemplate" />
Upvotes: 3