I am trying to make my dropmenu whenever I select the number 30 to print hello,however it always shows Hello,any idea why it doesn't work
<g:select name="user.age" from="${18..65}" value="${age}"
noSelection="['':'-Choose your age-']"/>
<g:if test="${age==30} ">
Hello
</g:if>
That should be <g:if test="${user.age==30} ">. The problem here is as per your last post, a user changing the value of a select box on the front end has not been registered or sent to your grails controller. When you next do <g:if It is checking the same value as was provided by your controller in the instance of
<g:select name="user.age" from="${18..65}" value="${age}" age here.. again this is wrong since you have called name user.age
So unless your g:select on change submits it to the controller then your gsp is reloaded so <g:if test="${user.age==30} "> can now pickup the new value.
It is a static value received by your view at the time controller called it VS your user dynamically changing the content of the select box. For which the change behaviour is handled by jquery as explained in previous answer to your previous question
Related
I have a situation in my application where I need to hide a field in one editable mode, but be visible and editable in another editable mode. For instance, I have ownerEdit where it renders a file _userid.gsp and all fields in the _userid.gsp are editable. At the same time, I am trying to render _userid.gsp file in userEdit mode where I need to hide some fields completely.
Any suggestions are welcome. Here are the sample codes:
I have ownerdataEdit.gsp which is call in ownerEdit.gsp. The _ownerDataEdit.gsp indeed call _userid.gsp as
<g:render template = "userid" model="${[userEditable:!ownerView.equals('show')]}"/>
Similarly, userDataEdit.gsp also call _userid.gsp as
<g:render template = "userid" model="${[userEditable:userEdit]}"/>
And finally, the _userid.gsp has codes for the password field which I want to hide. And to do that I used if statement
<g:if test="${[userEdit:true]}"><dt input type="hidden"</dt></g:if>
<g:else>render the field </g:else>
For some reason it doesn't seem to be reading g:if condition.
You will need to pass the mode in the render model:
render(template: "userid", model: [editable: true])
Then you have some options.
You could take the trivial route and use style='display: none' on fields when editable is true. This has the added benefit of being able to easily toggle between editable and not client-side, if that has value to you.
You could use <input type='hidden' ... /> fields if you need the values to come through to any submission. Note that these can still be edited by a user who knows how to use the developer console, so you'll need to have protections in place on your server side as well to prevent malicious editing.
You could just not render the fields at all when you're not in editable mode, if you don't need them for anything.
Additional info:
Change your if conditions from:
<g:if test="${[userEdit:true]}"><dt input type="hidden"</dt></g:if>
to:
<g:if test="${userEditable}"><dt input type="hidden"</dt></g:if>
I am writing a form. If checkbox in this form is filled, some hidden fields in this form must be shown. This is a part of my form:
<div class="checkbox">
<label><g:checkBox name="isAdminCafee" value="${false}"/>Register as admin</label>
</div>
<g:if test="${isAdminCafee == true}">
some admin data
</g:if>
I've decided to use if-operator for solving this task, but when I set check-box on, hidden fields don't show. How to fix it?
It seems you are trying to show some admin data when user checks the checkbox and hide some admin data when user unchecks the checkbox.
You have used g:if tag library which is interpreted in the server and render the part inside it if the test condition is true.
Instead of using g:if tag library, you need to hide some admin data using css and use javascript to show or hide when user checks or unchecks the checkbox.
I'm doing an application where after log in, User gets some settings (in check-boxes) which are Boolean in my database.
I'm trying to make that every User have his own settings checked, apropose when User logs checked are those fields where the value in the database is 1. My check-box is:
<g:checkBox name="task" value="${setting?.message}" />
<label class="text2" for="calendar">Things to done</label>
What I have to write in my controller?
in your scenario i understand there exist a one to many relationship between user and setting, as you explain after user successfully login user is take it to a view where are listed all possible setting.
you can do this in the view
<g:each in="${settings}" var="setting">
<label class="checkbox">
<g:checkBox name="settings" value="${setting}" checked="${user?.settings?.contains(setting)}"/> ${setting}
</label>
</g:each>
i took this code from a similar scenario i solve someday ago you can see it here in this github repo
after sending the form in the controller you can handle in this way
def settings = params.list("settings")
you can see the full action controler example here
You could now apply some user.addToSettings or user.removeFromSetting in order to save selected settings
i hope it help you
I used grails generate-all on my application. The Author view has a multi-select which allows for a number of Book instances:
<g:select multiple="true" ... />
However, if I edit an existing Author who owns 5 out of total 15 books (the multi-select shows 15 books, 5 selected), unselect all books and click save, the Author still keeps their 5 books. From what I can tell, no book input from the form - books property of Author don't get changed.
Now, I can test for this in my controller (something like this):
if (params?.books.size() < 1) {
authorInstance.books = []
}
Is this the way to do it, or is there a better way?
Most examples I've seen use:
authorInstance.books.clear()
I had the same problem, that a multi select list can not be emptied by default data binding, as the params map does not contain fields with a value of NULL.
To circumvent this, you can do this in your .gsp:
<g:hiddenField name="books" value="" />
<g:select multiple="true" name="books" />
When you post this form elements, the multi select will override the hidden field. If the multi select is empty, you will fallback to the empty string.
Not pretty, but get the job done, when you can not alter the controller action.
With the <g:select> tag... sometimes it displays normally like a selection drop down box, while sometimes it displays with multiple rows, this is very annoying.... Even I put the size="1" into the <g:select>, it still displays multiple rows... is there anyone knows how to make <g:select> display correctly? with only one item visible, like a dropdown box. Thanks!!
<g:select size="1" id="s_caseID" name="s_caseID" value="${t1T2InstanceListTotal?.Accession_No}"
noSelection="${['null':'Select One...']}"
from='${t1T2InstanceListTotal}'
optionKey="Accession_No" optionValue="Accession_No" onclick="this.form.submit();" >
</g:select>
Here's the taglib code that cause the multiple="multiple" attribute to be rendered (if not explicitly declared on the tag):
def value = attrs.remove('value')
if (value instanceof Collection && attrs.multiple == null) {
attrs.multiple = 'multiple'
}
Therefore, it looks like you're passing a Collection as the <g:select>'s value attribute instead of a single value. Is that what you're intending to do?
Set the multiple attribute to false
<g:select name="cars"
from="${Car.list()}"
value="${person?.cars*.id}"
optionKey="id"
multiple="false" />
If the "value" is a list, g:select always considers it as a multiple select. To avoid this and have a single select drop-down just ignore the value attribute and use keys option instead!
This works fine for me!
`<g:select id="s_caseID" name="s_caseID" from='${t1T2InstanceListTotal}'
noSelection="${['null':'Select One...']}"
keys="${t1T2InstanceListTotal?.Accession_No}" onclick="this.form.submit();">
</g:select>`