Tag "Select" in gsp - grails

How to correctly use tag select in gsp while passing to it ArrayList<String> ? I know how to use it this my own composed objects, for example:
<g:select name="tablePlacesAvailable" from="${tableInfo}" optionValue="${{it.placesInTableAmount}}" optionKey="placesInTableAmount" value=""/>
But how to use it with built in objects, like String?

If you want to use a g:select with a list of String
<g:select name="selection" from="${values}" />
Where values is a collection of Strings. My controller code is
class DemoController {
def index() {
[values: ["This", "is", "a", "test"]]
}
}

Here are some tests and its results:
Controller:
Controller:
def index() {
def myList=['hello','world']
def myMap=['h':'hello','w':'world']
render view: 'index', model: [myList:myList,myMap:myMap]
}
gsp:
<g:form name="test" >
<g:select name="s1" from="${['lastUpdated', 'id']}" value="${sortby}" />
<g:select name="s2" from="${['lastUpdated', 'id']}"
optionKey="value" optionValue="value" value="${sortby}" />
<g:select name="g" from="${['90%':'90%','100%': '100%']}"
optionKey="key" optionValue="value" value="${params.g}" />
<g:select name="g2" from="${myList}" />
<g:select name="g3" from="${myMap}" />
<g:select name="g4" from="${myMap}" optionKey="key" optionValue="value"/>
<g:select name="g5" from="${myMap}" optionKey="key" optionValue="key"/>
<g:select name="g6" from="${myMap}" optionKey="value" optionValue="value"/>
<g:submitButton name="go" value="post"/>
</g:form>
Results:
[g:100%, s2:lastUpdated, s1:id, g2:world, g6:world, g5:w, g4:w,
go:post, g3:w=world, action:index, format:null, controller:test]
As you can see a Arraylist with no optionKey optionValue works by default, in s2 I have also physically defined key value to be value and it still works

Related

Problems with form gsp

I'm trying to send parameters from my form (gsp) to my controller grails, but doesn't work.
<g:form url="[action:'searchByFilter', controller:'invoice']" method="GET">
<p>Filtro de busca</p>
<g:textField name="search" value="${invoice?.search}" params="search : search"/>
<g:submitButton name="search" class="input-search" value="${message(code: 'default.button.search.filter')}" />
</g:form>
I need to send input's value to do a result's filter. But, input's value isn't sending.
My domain class code:
static namedQueries = {
getInvoicesByStatus {
eq 'deleted', false
}
getInvoicesByFilter {
eq 'description', Invoice.get(params.search)
}
}
What's my mistake? I need to use namedQuery :)

How can I do: required="false" in grails g:select?

required="false" does not work in "g:select" tag
Example:
<g:select from="${}" name="select" required="false" />
and
<g:select from="${}" name="select" required="true" />
produces a html tag required (in html5)
How can I make the "g:select" produces the required or not, dynamically?
Just remove required, for eg:
<g:select id="select" from="${}" name="select"/>
You can use jquery to change the g:select to be required or not required. For example, lets say you have another
<g:select id="yesNo" from="[yes, no]">
In the gsp, use javascript:
$( "#yesNo" ).change(function() {
if($(this)[0].value == "yes") {
$( "#select" ).attr('required', 'required')
}
else {
$( "#select" ).removeProp( "required" )
}
});
Another approach is if you pass a variable required to gsp, you can use <g:if>:
In controller:
[required: "true"] //If dont want required, simply don't return required at all
In gsp:
<g:if test="${required}">
<g:select from="${}" name="select" required/>
</g:if>
<g:else>
<g:select from="${}" name="select"/>
</g:else>
<g:select from="${}" name="select" required="${false/true}" />
should work !

Getting a value from g:select in Grails

I'm trying to create my own 'edit' form in my grails application.
My g:select is currently populated with stuff from my database and looks like this:
<g:select name="nameList" from="${Card.list()}" value="${name} " />
And then the value :
<g:field name="amount" type="number" value="" required=""/>
My domain has only two variables - name and amount. I want to select the item from the dropdown box, type in the amount and just click 'update', my update method is a default one generated by grails so it requires ID and Version, how would I go about passing it through?
My update button ;
<g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
My domain code:
package cardstorage
class Card {
String name;
int amount;
static constraints = {
name(blank:false);
amount(blank:false);
}
String toString(){
return name;
}
}
Thank you
I have fixed it but I'm sure it is not a proper way to do so.
<g:form method="post" >
<g:select name="card" from="${Card.list()}" optionValue ="name" optionKey="id" />
<g:field name="amount" type="number" value="" required=""/>
<fieldset class="buttons">
<g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
</fieldset>
</g:form>
thats my code for the g:select. In my Controller method, passing the value of 'card' to the 'Long id' would result in 'id' being 49 (null + 1 = 49?)
def update(Long id, Long version) {
id = params.card;
id = id- 48;
...
}
Now I'm able to update my records, however I'm curious how should I have done this more properly.

How do I pass value of the variable from controller to view in grails?

I have statuscode that I need to pass from my controller to view. My view code has if/else condition.
<g:if test="${code=='something'}">
<g:link action="StartServer">
<input type="button" value="Start Server" class="stopimg" id="startServer" />
</g:link>
</g:if>
<g:else>
<g:link action="StopServer">
<input type="button" value="Stop Server" class="runimg" id="stopServer" />
</g:link>
</g:else>
I need to obtain the code from the controller, My code value is inside
def index() {}
How can I achieve this?
Return everything you need in a map.
def index {
...
return [code:variableContainingCode]
}

Grails controllers

I have a form similar to this one in Grails:
Name: _____
Age: _____
Street: _____
Email: ____
|Submit|
How can i pass all the filled in information to a controller that will add me the records to the database? Im kinda new to Grails, and my problem is i dont understand how to "pass" and get things to the controllers.
class Person {
String name
Integer age
String street
String email
}
class PersonController {
def save = {
def personInstance = new Person(params)
personInstance.save(flush:true)
}
}
<g:form controller="person" action="save">
<g:textField name="name" />
<g:textField name="age" />
<g:textField name="street" />
<g:textField name="email" />
<g:submitButton name="save" value="Save" />
</g:form>
Also, if you have a domain, you can run
grails generate-all com.foo.Person
And all the code will be generated for you. Then you can see how it is done.

Resources