在gsp模板中呈现内容

时间:2011-12-15 08:32:03

标签: html grails groovy

我是grails的新手,我想将html表'cart_table'(目前在shoppingcart.gsp中)的内容写入_carttemplate.gsp并在另一个gsp中显示此表(如summary.gsp)新模板。

目前我在shoppingcart.gsp中定义了如下表:

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

我填充身体如下

$("#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(''));

如何将表格放入template.gsp?并在确认页面中如何显示表格?

1 个答案:

答案 0 :(得分:3)

这将更详细地解释渲染模板: http://grails.org/doc/latest/ref/Tags/render.html

对于你的代码,听起来你只需要在shoppingcart.gsp和summary.gsp中执行此操作:

<g:render template="carttemplate" />

然后_carttemplate.gsp看起来像这样:

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

唯一有点棘手的部分是你如何通过javascript填充数据。如果您需要的多个位置可以重复使用相同的javascript,那么您可以将脚本放在.js文件中,并从呈现模板的任何gsp中引用它。如果它们不同,那么你就不会得到大量的重用。

另请注意,您可能需要在g:render标记中指定模板的目录。我们假设这个结构:

  • 的观点
      • shoppingcart.gsp
      • _carttemplate.gsp
    • 摘要
      • summary.gsp

shoppingcart.gsp可以:

<g:render template="carttemplate" />

但是summary.gsp需要这样做:

<g:render template="/cart/carttemplate" />
相关问题