setting the value of datePicker to a variable - grails

in Grails 2.3.4 i need to set the value that comes from the datePicker
<g:datePicker name="toDate" precision="day" relativeYears="[0..10]" default="${new Date()}" />
to a variable for instance :
<g:set var="fromDate" />
how can i do that ?
Thanks

You can use the <g:set /> tag to set the variable value, then assign the variable to the datePicker using the value attribute.
<g:set var="fromDate" value="${new Date()}" />
<g:datePicker name="toDate" precision="day" value="${fromDate}" />
Here is the documentation.

Related

grails Radio Button True or false checked property

I have radio group with two values. On editing a record, the radio should be set to checked or unchecked depending on the value from database.I use
<input type="radio" name="value1" value="${someValue}" ${it.id == true ?'checked="checked"' : ''}>
<input type="radio" name="value1" value="${someValue}" ${it.id == false ? 'checked="checked"' : ''}>
Using such code, gives me syntax error. Please correct my syntax.
Can't you use:
<g:radio name="value1" value="${someValue}" checked="${it.id}" />
(assuming it.id is a boolean as you seem to say)
Looks like you have to go this lengthy way. (assuming it.id yields a boolean)
<g:if test="${it.id}">
<g:radio name="value1" value="${someValue}" checked="${it.id}" />
</g:if>
<g:else>
<g:radio name="value1" value="${someValue}" />
</g:else>
You can use html input instead of g:radio in this case as well.

How to automatically populate value from action class to the JSP TextBox

I am able to print value from Java Action Class to JSP Page without textbox, But when I want to put that value in a TextBox in JSP, its not coming.
My Code is :
<S:form>
<s:textfield name="toDate" key="td" > <s:property value="fromDate"/> </s:textfield>
<s:textfield name="fromDate" key="fd"> <s:property value="toDate" /> </s:textfield>
<s:submit value="Confirm"></s:submit>
</s:form>
toDate and fromDate is variables in Java Class.
Just dive in the Struts2 UI Tag documentation:
key : Set the key (name, value, label) for this particular component
If you want them different (eg. toDate the value to set, td the value to read, and so on), specify all of them:
<s:form>
<s:textfield name="toDate" value="td" label="To date" />
<s:textfield name="fromDate" value="fd" label="From date" />
<s:submit value="Confirm" />
</s:form>
(Obviously according to your getters and setters).
You can set value in TextBox using value attribute of <s:textfield> tag to value come from action class.
For this you have your getter and setter method for that variable in action class and read that value by below code:
For Example :
<s:textfield name="some_name" value="%{variable}"/>
your code look like this:
<s:form>
<s:textfield name="toDate" value="%{fromDate}" />
<s:textfield name="fromDate" value="%{toDate}" />
<s:submit value="Confirm" />
</s:form>

How to get rid of the auto generated j_idt ID in composite component

I'm using below composite component:
<composite:interface>
<composite:attribute name="inputId" />
</composite:interface>
<composite:implementation>
<h:panelGrid id="myTableId">
<h:inputText id="#{cc.attrs.inputId}" value="..." />
...
</h:panelGrid>
</composite:implementation>
And I'm using it in my form as below:
<h:form id="myForm">
<myCompositeComp:test inputId="myInputTextBoxId" />
<h:form>
I've verified the view source for the page and this is how it is generated:
<table id="myForm:j_idt90:myTableId">
...
<input type="text" id="myForm:j_idt90:myInputTextBoxId" />
</table>
How can I get rid of j_idt90 here? Is it the id of my composite component? I read from one of BalusC post that this issue will get fixed if I declare id as static. But I'm not able to identify the place to declare it in my code. Also can I assume <h:panelGrid> is a kind of UINamingContainer?
Yes it is the id of your composite component, <h:panelGrid> is not a UINaminContainer but the composite component is (it has to be, otherwise you would have duplicate IDs if you use it several times inside the same form for example).
Why do you need to get rid of the ID? You can set it yourself if that solves your problem:
<h:form id="myForm">
<myCompositeComp:test id="myComp" attr1="" attr2="" />
<h:form>
the genereated html should look like this:
<table id="myForm:myComp:myTableId">
....
<input type="text" id="myForm:myComp:myInputTextBoxId"
</table>

Get an array value with dynamic index in struts2

I'm trying to get a value from an array into a s:textbox element:
it works if I take a hard coded index like:
<s:textarea value="%{languageHelper.myHauptuebbeschr[0]}" />
but when I try to use a dynamic index it doesn'T work:
<s:textarea value="%{languageHelper.myHauptuebbeschr[attr.number]}" />
Number is not empty, I can get the value via
<s:property value="%{#attr.number}" />
So how can I use the number variable as index for my array?
shouldn't it be:
<s:textarea value="%{languageHelper.myHauptuebbeschr[#attr.number]}" />
I found a solution:
I first save the value in an addtional variable:
<c:set var="myHauptuebbeschr" value="${languageHelper.myHauptuebbeschr[number]}" ></c:set>
and then get it via:
<s:textarea value="%{#attr.myHauptuebbeschr}" />

Problem with variable inside gsp

I have a loop inside a gsp page, and I want to do a calculation on the fly, so I set a variable:
<g:set var="total" value="0" />
And in the loop:
<g:each in="${mob}" var="m">
...
<g:set var="total" value="${total+(m.q*m.sts.uf)}"/>
...
</g:each>
The "total" value does not correspond to the expected calculation. m.q is an integer, and m.sts.uf is a float.
Any hints ?
Thanks.
What does total end up being?
It could be because total is being initialised as a String.
Try
<g:set var="total" value="${0l}" />

Resources