问题
I have a template containing a g:each that renders a set of search results in tabular format as follows:
<g:if test="${company}">
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th class="small"><g:message code="Company Name"/></th>
<th class="medium"><g:message code="TDCID"/></th>
<th class="tiny"><g:message code="STREET"/></th>
</tr>
</thead>
<tbody>
<g:each in="${company}" status="idx" var="PNo">
<tr>
<td>
<a: href="javascript:void(0)" onClick="javascript:asd();" params="[tdc:PNo.tdcId,companyid:PNo.companyId,companytype:PNo.companyType]">//params: passing not working now.Have to pass the tdcId,companyId,companyType of clicked result
${PNo.officialName}
</a:>
</td>
<td>
${PNo.tdcId}
</td>
<td>
${PNo.street}
</td>
</tr>
</g:each>
</tbody>
</table>
</g:if>
I need to pass tdcId,companyId,companyType contained in company object that is clicked by user to the following JavaScript function:
function asd(){
window.open("${g.createLink(controller: 'Customer', action: 'showcontacts',params:'[tdc:company.tdcId,companyid:company.companyId,companytype:company.companyType]')}",'', 'width=200,height=100');
}
Here from asd() I need to pass the params to action:'showcontacts' through createLink tag. What is the right way to do this?
This is the calling gsp: _customers.gsp:
<script type="text/javascript">
function asd(tdcId, companyId, companyType) {
alert(tdcId);
var link = "${g.createLink(controller: 'Customer', action: 'showcontacts')}?tdc=" + tdcId + "&companyid=" + companyId + "&companytype=" + companyType;
window.open(link, '', 'width=200,height=100');
}
</script>
<div class="form-edit">
<div class="heading">
<strong>
<g:message code="Firm Profile"/>
</strong>
</div>
<div id="hai" class="form-hold">
<g:form name="company-edit-form" action="save">
<fieldset>
<div class="form-columns">
<div class="column">
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Company Name"/</content>
<content tag="label.for">Company</content>
<g:textField class="field" name="Company" id="Company" />
</g:applyLayout>
<g:applyLayout name="form/checkbox">
<content tag="label"><g:message code="Starts with"/</content>
<content tag="label.for">Starts with</content>
<g:checkBox class="cb checkbox" name="StartsWith" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Street"/></content>
<content tag="label.for">Street</content>
<g:textField class="field" name="Street" />
</g:applyLayout>
<g:applyLayout name="form/checkbox">
<content tag="label"><g:message code="Starts with"/</content>
<content tag="label.for">Starts With</content>
<g:checkBox class="cb checkbox" name="StreetStartsWith"/>
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Zip code"/></content>
<content tag="label.for">Zip</content>
<g:textField class="field" name="ZipCode"/>
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Fagrubrik"/</content>
<content tag="label.for">Fagrubrik</content>
<g:textField class="field" name="Fagrubrik" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="House no."/</content>
<content tag="label.for">HouseNo</content>
<g:textField class="field" name="HouseNo" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Tel./Fax."/</content>
<content tag="label.for">Telephone</content>
<g:textField class="field" name="Telephone" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Company No."/</content>
<content tag="label.for">CompanyNo</content>
<g:textField class="field" name="CompanyNo" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="City"/></content>
<content tag="label.for">City</content>
<g:textField class="field" name="City" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="TDC ID"/></content>
<content tag="label.for">TdcId</content>
<g:textField class="field" name="TdcId" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Industry Designation"/></content>
<content tag="label.for">Industry Designation</content>
<g:textField class="field" name="IndustryDesignation" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Decision Maker"/</content>
<content tag="label.for">DecisionMaker</content>
<g:textField class="field" name="DecisionMaker" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="Email"/></content>
<content tag="label.for">Email</content>
<g:textField class="field" name="Email" />
</g:applyLayout>
<g:applyLayout name="form/input">
<content tag="label"><g:message code="WWW"/></content>
<content tag="label.for">WWW</content>
<g:textField class="field" name="WWW"/>
</g:applyLayout>
</div>
</div>
<div>
<br/>
</div>
<div id="button" class="buttons">
<ul>
<li>
<g:submitButton class="buttons" id="button" name="Search"
value="Search" action="search"/>
</li>
</ul>
</div>
</fieldset>
</g:form>
<div class="table-box">
<g:if test="${company}"><table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th class="small"><g:message code="Company Name"/></th>
<th class="medium"><g:message code="TDCID"/></th>
<th class="tiny"><g:message code="STREET"/></th>
</tr>
</thead>
<tbody>
<g:each in="${company}" status="idx" var="PNo">
<tr>
<td>
<a href="javascript:void(0)"
onClick="javascript:asd('${PNo.tdcId}','${PNo.companyId}',
'${PNo.companyType}');">${PNo.officialName}</a>
</td>
<td>
<g:remoteLink action="show" id="${PNo}" before="register(this);"
onSuccess="render(data, next);" params="[template:'show']">
${PNo.tdcId}
</g:remoteLink>
</td>
<td>
${PNo.street}
</td>
</tr>
</g:each>
</tbody>
</table>
</g:if>
</div>
</div>
</div>
This is the called one for test purposes.named _showcontacts.gsp:
<body>
<div id="dialog" title="Contacts">
hai
</div>
</body>
回答1:
Change your a tag like this:
<a href="javascript:void(0)" onClick="javascript:asd('${PNo.tdcId}', '${PNo.companyId}', '${PNo.companyType}');">${PNo.officialName}</a>
And Script like this:
<script>
function asd(tdcId, companyId, companyType) {
var link = "${g.createLink(controller: 'test', action: 'test')}?tdc=" + tdcId + "&companyid=" + companyId + "&companytype=" + companyType;
window.open(link, '', 'width=200,height=100');
}
</script>
Enjoy..
来源:https://stackoverflow.com/questions/19652218/passing-params-from-within-geach-to-action-specified-in-createlink-within-a-j