简体   繁体   English

使用Javascript中的GSP标记

[英]using GSP tags from Javascript

I am trying to create a dynamic HTML table using javascript. 我正在尝试使用javascript创建动态HTML表。 I have written a small function to add rows to the table on the fly. 我写了一个小函数来动态地向表中添加行。 However the individual cells themselves should be gsp tag elemets. 然而,单个细胞本身应该是gsp标签元素。 In the example below I am trying to use the autoComplete tag provided by the grails-UI plugin. 在下面的示例中,我尝试使用grails-UI插件提供的autoComplete标记。 Even though I set the innerHTML of the cell to the gsp tag, it is not being rendered on the page. 即使我将单元格的innerHTML设置为gsp标记,它也不会在页面上呈现。

function addIngredientRow(tableName,element){

var table = document.getElementById(tableName);
var lastRow = table.rows.length;
var row = table.insertRow(lastRow);
var leftCell = row.insertCell(0);
var autoCompleteDiv = document.createElement('div');
leftCell.appendChild(autoCompleteDiv);
autoCompleteDiv.innerHTML= '<gui:autoComplete id="autoCompleteIngredient" resultName="ingredients" labelField="name" idField="id" controller="recipe" \
        action="getIngredientAsJSON" forceSelection="true" \
        useShadow="true" width=60px  queryDelay=0.5   />';

var rightCell = row.insertCell(1);
var autoCompleteDivR = document.createElement('div');
rightCell.appendChild(autoCompleteDivR);
autoCompleteDivR.innerHTML= '<p>test</p>';

} }

The HTML snippet invoking this code is as follows: 调用此代码的HTML代码段如下:

<tr>
                     <td>
                         <gui:autoComplete
                                id="autoCompleteIngredient"
                                resultName="ingredients"
                                labelField="name"
                                idField="id"
                                controller="recipe"
                                action="getIngredientAsJSON"
                                forceSelection="true"
                                useShadow="true"
                                  width=60px
                                  queryDelay=0.5      
                        />
                     </td>
                     <td>
                         <div onclick="addIngredientRow('createRecipeTable',this)"><img alt="Add Ingredient" src="${resource(dir:'images/icons',file:'Add16.png')}" ></div>
                     </td>
                 </tr>

I have checked that the javascript is being invoked by putting an alert. 我已经检查过通过发出警报来调用javascript。 So all the wiring is fine. 所以所有的接线都很好。 I think that the issue is with resolving the gsp tags by the browser. 我认为问题在于浏览器解析gsp标签。 How can I invoke gsp tags from javascript?? 如何从javascript中调用gsp标签?

GSP pages are rendered on the server, long long long before the browser EVER gets hold of the html and can start running the Javascript. GSP页面在服务器上呈现,很久很久就在浏览器EVER获取html并且可以开始运行Javascript之前很久。 You'd need to use an AJAX call to send the gsp tag back to the server, where it can (hopefully) be rendered into HTML, then take the HTML from the AJAX response and insert it into your document. 您需要使用AJAX调用将gsp标记发送回服务器,在那里它可以(希望)呈现为HTML,然后从AJAX响应中获取HTML并将其插入到文档中。

GSPs are evaluated prior to rendering to the browser. 在渲染到浏览器之前评估GSP。 If you want javascript to use them, the javascript source will have to be served/evaluated by the grails engine. 如果你想让javascript使用它们,那么jils源必须由grails引擎提供/评估。

You can create a controller for the js, and use a gsp to render it like any other page of your grails application. 您可以为js创建一个控制器,并使用gsp将其呈现为grails应用程序的任何其他页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM