I have two domain classes...
First:
class Employee {
String vorname
String nachname
Department department
}
Second:
class Department {
Integer number
String description
}
...and I want to create a dropdown in Grails.
My code looks like this:
<g:select id="department" name="department.description" from="${workloadreport.Department.list()}" optionValue="bezeichnung" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one" noSelection="${['null':'Please select...']}"/>
But I want to show the number + description as one value in dropdown.
Like:
Value 1 : 30 General Services
Value 2 : 40 Product Services
Value 3 : 50 Other Services
I'm assuming you want the resulting html to look something like this for an option element ...
<option value="1">1 : 30 General Services</option>
this should produce it (there may be typo, I haven't tested it):
<g:select id="department" name="department.description"
from="${workloadreport.Department.list()}" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one"
noSelection="${['null':'Please select...']}"
optionValue="${{it.id + ' : ' + it.number + ' ' + it.description}}"
/>
** note you probably have a typo in your from property. If you just want to generate a list of options from all departments, your from should look like this
<g:select ... from="${Department.list()}" .../>
Related
I have a strong need to use custom input naming rules like:
<input type="text" value="1" name="products[123][qty]">
Can't use #HtmlTextBoxFor()
I decided to move "products" and "qty" text literals to be constants. Have a strong need for it too.
Then I tried:
<input id="qty" type="text" value="1" name="#OrderContext.PRODUCTS_KEY[123][#OrderContext.QUANTITY_KEY]">
And I get an error because "[" and "]" are used in Razor like index separators, I need to use it like string literals. I tried to escape them with "\" but then I get "\[" and "\]" in HTML instead of desired "[" and "]".
How to escape square brackets properly? What can you suggest?
The real need is to move input name keys like "products" and "qty" to be defined and later changed in one place.
Try
<input id="qty" type="text" value="1" name="#(OrderContext.PRODUCTS_KEY)[123][#OrderContext.QUANTITY_KEY]">
Parentheses right after # limits the scope of the parsing as code to just the content within the parentheses.
You could argue that what your doing is data related. So it belongs in the model. So edit your model thus:
public class OrderContext
{
public string PRODUCTS_KEY {get; set;}
public int QUANTITY_KEY {get; set;}
public string PRODUCTS_KEY_InputName
{
get
{
return string.format("{0}[123][{1}]", PRODUCTS_KEY, QUANTITY_KEY);
}
}
}
View
then you can simply print it in your view:
<input id="qty" type="text" value="1" name="#PRODUCTS_KEY_InputName">
I ran into an issue while populating domain object values on a gsp page by using a g:select tag.
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
In my controller, I have an object which gathers values from my domain. I tried writing this object in the controller both as:
def d = Demo.getAll()
and def d = Demo.list()
Both of these returned an exception while trying to use the g:select tag. The error was:
No signature of method: Demo.getAt() is applicable for argument types: (java.util.ArrayList) values: [[One, Two, Three, Four, Five]]
I was able to get my code working by simply removing the g:select tag and writing the following in my gsp:
<select name="demo" id="dm">
<option value="">Select!</option>
<g:each in="${dm}" status="i" var="dm">
<option value="${dm.id}">${dm.name}</option>
</g:each>
</select>
However, I would like to know how to make this work with the g:select tag, for my future reference.
Thanks in advance!
I see two issues in your select:
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
value shouldn't be looking at the collection for the id. I believe this is why you are getting your error. value should be whatever instance's id you've passed into your view.
optionValue doesn't required an expression.
Controller action
def someAction() {
def dm = Demo.get(params.id)
def demos = Demo.list()
[dm: dm, demos: demos]
}
view
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${demos}"
optionValue="type"/>
I have the <g:select> on my proyect who is a one-to-many relation,
but I want it to show the name (attribute) of my class instead of the id.
It shows something like this:
com.petshop.Category: 1
This is the code I use on the view:
<g:select id="subcategory" name="subcategory.id" from="${com.petshop.Category.list()}" optionKey="id"
required="" value="${animalInstance?.category?.id}"/>
What should I change/add/delete to show "Birs" for instance, instead of "com.petshop.Category: 1" that is the id of the object.
Would this work?
<g:select id="subcategory" name="subcategory.name" from="${com.petshop.Category.list()*.name}" optionKey="id" optionValue="name"
required="" value="${animalInstance?.category?.name}"/>
I'm trying to do something pretty simple, that apparently is not that simple. I have a domain class:
class Regex {
String name
String Regex
}
and another class:
class RegexRef {
int sequenceNumber
Product product
Regex regex
}
Now, in a select, I want to list the RegexRef instances, with the name of the regex as the optionValue as such:
<g:select name="regexRef.id" from="${com.mycompany.RegexRef.list()}" optionKey="id" size="5" optionValue="regex.name" value="${actionRefInstance?.regexRef?.id}" />
but this doesn't work. It throws:
Exception Message: No such property: regex.name for class: com.jetheaddev.RegexRef
I can do this mis-direction in other constructs...
<g:link controller="regexRef" action="show" id="${actionRefInstance?.regexRef?.id}">${actionRefInstance?.regexRef?.regex.name.encodeAsHTML()}</g:link>
and it works fine.
With optionValue="regex.name" the <g:select/> is trying to retrieve the property of RegexRef as:
regexRefInstance."regex.name"
To retrieve the name property on the Regex class do:
<g:select optionValue="${{it.regex.name}}"/>
which will run the optionValue Closure on each entry in the list and retrieve the related name property.
How do I get Grails data binding to correctly bind referenced objects (such as Country in the example below)?
Given the following two Grails domain classes ..
class User {
String username
Country country
}
class Country {
String name
}
.. and the following HTML form ..
<g:form>
<g:textField name="user.username" value="${user.username}" />
<g:select name="user.country" from="${Country.list()}" optionKey="id" />
</g:form>
.. and the following code in the corresponding action ..
User user = new User(params["user"])
.. I would have hoped that user.username and user.country would get bind. However, it appears as if username.username gets bind, whereas user.country is not. What is the correct syntax to bind referenced objects (user.country in this example)?
The binding of the "country" property starts working if ..
<g:select name="user.country" from="${Country.list()}" optionKey="id" />
.. is changed to ..
<g:select name="user.country.id" from="${Country.list()}" optionKey="id" />
You should also look into command objects. They can validate and bind all of your parameters at once.