I have this form and when the user types something this is submitted as finalAnswer, as you can see below:
<g:form controller="animais" params="['rootNode': rootNode, 'finalAnswer': finalAnswer]">
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
The problem is that finalAnswer come to params as a 2-sized array of strings, even if the content is a simple string.
When I printed params:
[finalAnswer:[, leao marinho], rootNode:2, _action_submitFinalAnswer:OK, action:index, format:null, controller:animais]
Value of params.finalAnswer: [, leao marinho]
ClassType of params.finalAnswer: class [Ljava.lang.String;
How can I retrieve params.finalAnswer as a simple string?
Try this
<g:form controller="animais" params="['rootNode': rootNode]">
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${params.finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${params.tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
Answer by #quindimildev not fully right. Better will be use hidden field:
<g:form controller="animais">
<g:hiddenField name="rootNode" value="${rootNode}"/>
<g:hiddenField name="finalAnswer" value="${finalAnswer}"/>
<h3>${curQuestion} </h3>
<g:if test="${finished}">
<g:actionSubmit class="btn btn-primary" action="backToStart" value="Voltar" />
</g:if>
<g:if test="${!finished}">
<g:if test="${!showDivTip}">
<g:textField name="finalAnswer" value="${finalAnswer}"/>
<g:actionSubmit class="btn btn-primary" action="submitFinalAnswer" value="OK" />
</g:if>
<g:if test="${showDivTip}">
<g:textField name="tipToFinalAnswer" value="${tipText}"/>
<g:actionSubmit class="btn btn-primary" action="submitTipForAnswer" value="Finalizar" />
</g:if>
</g:if>
</g:form>
So you can remove it from params. Really, it more clear.
Related
I know there are some questions like mine, but I'm hoping to get an explanation of how to do it in Grails.
What I want to do is pass values from a gsp datepicker and textfield to the controller when upon Submit button press.
My thanks in advance
GSP:
<g:form action="generateReport">
<ol>
<li class="fieldcontain">
<div class="fieldcontain required">
<label for="date" style="padding-left: 210px;">
<g:message code="Data" default="Data"/>
<span class="required-indicator">*</span>
<g:datePicker id="data" name="datapicker" precision="day"></g:datePicker>
</label>
</div>
</li>
<li class="fieldcontain">
<div class="fieldcontain required">
<label for="date" style="padding-left: 195px;">
<g:message code="Time" default="Time"/>
<g:textField name="minutes"/>
</label>
</div>
</li>
</ol>
<div class="content scaffold-show" style="padding-left: 50px;">
<g:submitButton value="search" name="button"/>
</div>
</g:form>
Controller:
def generateReport(){
log.info("Button pressed")
redirect(action: "list")
}
I just found the answer:
the view:
<g:textField name="name" />
the controller:
Controller:
class SomeController {
def someAction() {
def name = params.name
// do something with name
}
}
how can I update the data other than form. ? I created a button but do not update my data.
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'product.label', default: 'product')}" />
<title><g:message code="default.edit.label" args="[entityName]" /></title>
</head>
<body>
<div id="wyszukaj">
<g:form method="post">
<div class="fieldcontain" ${hasErrors(bean: productInstance, field: 'symbolIndeksu', 'error')}">
<label for="symbolIndeksu" style="width: 152px;">
<g:message code="product.wyszukajSymbolIndeksu.label" default="Symbol Indeksu" />
</label>
<g:select id="id" name="id" from="${com.app.product.findAll([sort:"symbolIndeksu"])}" optionKey="id" value="${productInstance?.id}" class="many-to-one inptSearch chzn-select" />
<g:actionSubmit class="save" action="edytuj" value="Zmień" />
<g:actionSubmit class="save" action="aktualizuj" value="${message(code: 'default.button.update.label', default: 'Update')}" />
/* Button at this point should update the data */
</div>
</g:form>
</div>
<div id="view-tab">
<g:render template="tabs"/>
<div id="content-tab">
<div id="edytuj-product" class="content scaffold-edit" role="main">
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<g:hasErrors bean="${productInstance}">
<ul class="errors" role="alert">
<g:eachError bean="${productInstance}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
<g:form method="post" >
<g:hiddenField name="id" value="${productInstance?.id}" />
<g:hiddenField name="version" value="${productInstance?.version}" />
<fieldset class="form">
<g:render template="formularz"/>
</fieldset>
</div>
</div>
</g:form>
</div>
</body>
</html>
In summary. I would like to use the update button outside of the form g: form. Previously, I had the buttons at the bottom
my form looks like this :
<form class="form-validate" id="booking-form" name="form-booking" method="post" action="/component/booking/?view=booknig">
<div class="width-60 fltlft">
<fieldset class="form-booking">
<legend></legend>
<ul>
<li><input type="text" readonly="readonly" class="readonly" value="0" id="jform_id" name="jform[id]"></li>
<li><input type="hidden" value="2013-06-17 10:15" name="jform[date_from]"></li>
<li><input type="hidden" value="1" name="jform[service_id]"></li>
<li><label class="" for="jform_phone" id="jform_phone-lbl">Telefon</label><input type="text" class="inputbox" value="" id="jform_phone" name="jform[phone]"></li>
<li><label class="" for="jform_email" id="jform_email-lbl">Email</label><input type="text" class="inputbox" value="" id="jform_email" name="jform[email]"></li>
<li><input type="hidden" value="" id="jform_checked_out" name="jform[checked_out]"></li>
<li><input type="hidden" value="" id="jform_checked_out_time" name="jform[checked_out_time]"></li>
</ul>
</fieldset>
</div>
<input type="hidden" value="booking.save" name="task">
<input type="submit" class="button-submit" value="Send" name="Send">
<input type="hidden" value="1" name="e391ee49d5291fa9d566236f5c0a435e"> <div class="clr"></div>
</form>
and to retrieve POST data i'm using :
$jinput = JFactory::getApplication()->input;
$form_values = $jinput->post->get('jform');
but when i'm tring to print_r $form_values i get only Array
where i'm making mistake
Below is the HTML code for a Telerik Tree View Control used in a ASP.NET MVC application. I need to get the value (input tags values are in GUID format) of the ChildNode when a child node checkbox is selected.
Illlustrated below:
*Main Region*
**North VAncouver
testlocation2
Testlocation123**
*Africa*
**Tanzania
Headquarters**
*India*
**Items Gobetter
Mash
YouThinkSo**
*Canada*
**WestVancouver**
AS given above I was wondering if you could give me the Jquery code to iterate through the HTML markup and select checked child node values given in GUID's.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
<title></title>
</head>
<body>
<ul>
<li>
<span id="spnloc">Locations</span>
<div style=
"position: absolute; padding-top: 5px; background-color: rgb(234, 234, 234); color: rgb(63, 138, 196); z-index: 2000; display: none;"
id="TreeView1" class="t-widget t-treeview t-reset">
<ul class="t-group t-treeview-lines">
<li class="t-item t-first">
<div class="t-top">
<span class="t-checkbox"><input type="hidden" value="0" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="True" name="TreeView1_checkedNodes[0].Checked" class="t-input"
checked="checked" /><input type="hidden" value="Main Region" name=
"TreeView1_checkedNodes[0].Text" class="t-input" /><input type="hidden"
value="Main Region" name="TreeView1_checkedNodes[0].Value" class=
"t-input" /></span><span class="t-in">Main Region</span><input type=
"hidden" value="Main Region" name="itemValue" class="t-input" />
</div>
<ul class="t-group">
<li class="t-item">
<div class="t-top">
<span class="t-checkbox"><input type="hidden" value="0:0" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[0:0].Checked" class=
"t-input" /></span><span class="t-in">North
Vancouver</span><input type="hidden" value=
"5761a893-9ef0-48d3-847a-2638ec081f5f" name="itemValue" class=
"t-input" />
</div>
</li>
<li class="t-item">
<div class="t-mid">
<span class="t-checkbox"><input type="hidden" value="0:1" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[0:1].Checked" class=
"t-input" /></span><span class="t-in">testLocation2</span><input type=
"hidden" value="d63d6ff6-07dc-4021-a4bb-7b1b7c781119" name="itemValue"
class="t-input" />
</div>
</li>
<li class="t-item t-last">
<div class="t-bot">
<span class="t-checkbox"><input type="hidden" value="0:2" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[0:2].Checked" class=
"t-input" /></span><span class=
"t-in">TestLocations123</span><input type="hidden" value=
"eadfd0f5-9181-4094-b01a-9d7ee383b7f2" name="itemValue" class=
"t-input" />
</div>
</li>
</ul>
</li>
<li class="t-item">
<div class="t-mid">
<span class="t-checkbox"><input type="hidden" value="1" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="True" name="TreeView1_checkedNodes[1].Checked" class="t-input"
checked="checked" /><input type="hidden" value="Africa" name=
"TreeView1_checkedNodes[1].Text" class="t-input" /><input type="hidden"
value="Africa" name="TreeView1_checkedNodes[1].Value" class=
"t-input" /></span><span class="t-in">Africa</span><input type="hidden"
value="Africa" name="itemValue" class="t-input" />
</div>
<ul class="t-group">
<li class="t-item">
<div class="t-top">
<span class="t-checkbox"><input type="hidden" value="1:0" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[1:0].Checked" class=
"t-input" /></span><span class="t-in">Tanzania</span><input type="hidden"
value="5c2389eb-365e-42bb-9b1a-8db36f87b4af" name="itemValue" class=
"t-input" />
</div>
</li>
<li class="t-item t-last">
<div class="t-bot">
<span class="t-checkbox"><input type="hidden" value="1:1" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="True" name="TreeView1_checkedNodes[1:1].Checked" class="t-input"
checked="checked" /><input type="hidden" value="Headquarters" name=
"TreeView1_checkedNodes[1:1].Text" class="t-input" /><input type=
"hidden" value="ac171662-3eca-411f-a623-a3cb1a69238e" name=
"TreeView1_checkedNodes[1:1].Value" class=
"t-input" /></span><span class="t-in">Headquarters</span><input type=
"hidden" value="ac171662-3eca-411f-a623-a3cb1a69238e" name="itemValue"
class="t-input" />
</div>
</li>
</ul>
</li>
<li class="t-item">
<div class="t-mid">
<span class="t-checkbox"><input type="hidden" value="2" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="True" name="TreeView1_checkedNodes[2].Checked" class="t-input"
checked="checked" /><input type="hidden" value="India" name=
"TreeView1_checkedNodes[2].Text" class="t-input" /><input type="hidden"
value="India" name="TreeView1_checkedNodes[2].Value" class=
"t-input" /></span><span class="t-in">India</span><input type="hidden"
value="India" name="itemValue" class="t-input" />
</div>
<ul class="t-group">
<li class="t-item">
<div class="t-top">
<span class="t-checkbox"><input type="hidden" value="2:0" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[2:0].Checked" class=
"t-input" /></span><span class="t-in">Item's Go Here</span><input type=
"hidden" value="d8266db4-e846-4338-906f-e0b84fd9044a" name="itemValue"
class="t-input" />
</div>
</li>
<li class="t-item">
<div class="t-mid">
<span class="t-checkbox"><input type="hidden" value="2:1" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[2:1].Checked" class=
"t-input" /></span><span class="t-in">MASH</span><input type=
"hidden" value="869c083a-a17b-4a7a-a2a9-ef88f972de1b" name="itemValue"
class="t-input" />
</div>
</li>
<li class="t-item t-last">
<div class="t-bot">
<span class="t-checkbox"><input type="hidden" value="2:2" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[2:2].Checked" class=
"t-input" /></span><span class="t-in">YouThinkSo</span><input type=
"hidden" value="4e33e582-8a54-450a-ad73-70698a578162" name="itemValue"
class="t-input" />
</div>
</li>
</ul>
</li>
<li class="t-item t-last">
<div class="t-bot">
<span class="t-checkbox"><input type="hidden" value="3" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="True" name="TreeView1_checkedNodes[3].Checked" class="t-input"
checked="checked" /><input type="hidden" value="Canada" name=
"TreeView1_checkedNodes[3].Text" class="t-input" /><input type="hidden"
value="Canada" name="TreeView1_checkedNodes[3].Value" class=
"t-input" /></span><span class="t-in">Canada</span><input type="hidden"
value="Canada" name="itemValue" class="t-input" />
</div>
<ul class="t-group">
<li class="t-item t-last">
<div class="t-top t-bot">
<span class="t-checkbox"><input type="hidden" value="3:0" name=
"TreeView1_checkedNodes.Index" class="t-input" /><input type="checkbox"
value="False" name="TreeView1_checkedNodes[3:0].Checked" class=
"t-input" /></span><span class="t-in">West Vancouver</span><input type=
"hidden" value="b8fd4a6f-bfc7-4c8a-b2ff-cb3c278c7434" name="itemValue"
class="t-input" />
</div>
</li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
</body>
</html>
In italics are the Regions (Parent nodes) and in Bold are the child nodes. Both Region and child nodes(which are locations) are Checkboxes.
The values of the Child Nodes are in GUID (Unique Indentiifer) value.
ON selecting the the parent and child nodes I need to be able to only get the value of the child nodes after I select the nodes and maybe click a button to run the JQuery code. Can you help me with the Jquery code that will be able to give me the selected values of the checkboxes(child nodes only) ignoring the parent checkboxes even if they are selected. The HML markup for the TreeView is given ABOVE.
Not sure if this is what you want
Demo: http://jsfiddle.net/qHJYJ/
by clicking the button will return GUID of all children nodes that have
Parent Node selected
Children Node itself selected
$('input[type="checkbox"]').on('change', function() {
var e = $(this);
if (e.parents('li:eq(2)').length) {
console.log('sub-checkbox found, value: ' + e.val());
} else {
console.log('parent checkbox has been changed, ignoring...');
}
console.log(e.attr('name'));
});
jsFiddle example code here
how can I jump to another action in controller?
I have form and several submit buttons. Every submmit button has name.
<g:form action="save" method="post">
<g:input name="title" value="${letter.title}" />
<g:input name="comments[0].text" value="${letter.comments[0].text}" />
<g:submitButton name="save" value="save" />
<g:submitButton name="addComment" value="add" />
</g:form>
def save = {
if (params.addComment){
letter.addToComents( new Comment() )
render(view:'form', model:["letter": letter])
return
}
...
if ( letter.save() )
...
}
def addComment = {
...
}
It works, but it is not good. I want move code from block "addComment" to action addComment:
def save = {
if (params.addComment){
// it don´t work
redirect ( action:"addComment" )
}
...
if ( letter.save() )
...
}
def addComment = {
letter.addToComents( new Comment() )
render(view:'form', model:["letter": letter])
return
}
Or it exists better solution?
It would be nice:
<g:submitButton name="save" value="save" action="save" />
<g:submitButton name="addComment" value="add" action="addComment" />
Thanks a lot
Tom
Use the g:actionSubmit tag instead.
<g:form method="post">
<g:input name="title" value="${letter.title}" />
<g:input name="comments[0].text" value="${letter.comments[0].text}" />
<g:actionSubmit action="save" value="Save" />
<g:actionSubmit action="addComment" value="Add Comment" />
</g:form>
For those who are using Twitter Bootstrap plugin (or need something besides text in your button) and want to add a glyphicon to the button, you will need to use the button tag. So you need to do something like
SNIPPET 1.
<g:form role="form" method="post">
...your inputs
<button type="submit" name="_action_save">
<span class="glyphicon glyphicon-ok"></span>
Save
</button>
<button type="submit" name="_action_saveAndNew">
<span class="glyphicon glyphicon-ok"></span>
Save and New
</button>
</g:form>
where in your button you will need to specify the name of your action with the prefix
_action_
to get something like this
name="_action_yourActionName"
just a little reminder, since I am using twitter Bottstrap plugin 3.0 this is how you add a glyphicon
<span class="glyphicon glyphicon-ok"></span>
SNIPPET 1. has a similar behaviour to:
<g:form role="form" method="post">
...your inputs
<g:actionSubmit action="save" value="Save" />
<g:actionSubmit action="saveAndNew" value="Save and New" />
</g:form>
In the end this example help you have a similar behaviour to actionSubmit in cases where you don't want or can't use it. This is only an alternative and it'd be better to use actionSubmit whenever possible.