How would I go about setting up a I18N message properties file with all the US states. I want to be able to add these to a g:select tag in my gsp file.
I'm spinning my head seeing different ways online to do this. And being new to grails isn't helping. Any help appreciated.
Example:
<select>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
.......
</select>
I see a couple ways to do this....
In Config.groovy:
com.app.states=[[abbr: 'AL', name: 'Alabama'],[abbr: 'AK', name: 'Alaska']..........]
Then in your gsp:
<g:select name="state" from="${grailsApplication.config.com.app.states}" optionKey="abbr" optionValue="name" noSelection="${['null':'Select One...']}" value="${obj.state}"/>
Another way
Make an enum for your abbreviations:
package com.app.enum
public enum State {
AL, AK, .........., WY
}
Add messages in messages.properties:
com.app.enum.AL=Alabama
com.app.enum.AK=Alaska
...
...
...
Then in your GSP:
<g:select name="state" from="${com.app.enum.State.values()}" optionKey="key" valueMessagePrefix="com.app.enum" noSelection="${['null':'Select One...']}" value="${obj.state.key}"/>
I kinda preffer the second way. I would also type the state field on my model as the State enum type instead of just a String. But either of these should work for you...
Related
I having a grails element
<g:select name="name" from="${list}" optionKey='code' optionValue='name' ></g:select>
where the optionValue contains some HTML elements like this,
I want to show only the country name, already I tried using encodeAsHTML(), but no idea how to use. Please suggest.
Thanks
you can not do this with the out-of-box g.select tag. you need to iterate through your list manually:
<select name="someName">
<option value="">- no select -</option>
<g:each in="${list}" var="c">
<option value="${c.code}">${c.name.replaceFirst( /<span class='countryName'>([\w\s]+)</span>/, '$1' )}</option>
</g:each>
</select>
This is happening because of XSS. Instead of encodeAsHTML use raw. Try this:
<g:select name="name" from="${list.collect { raw(it) }}" optionKey='code' optionValue='name'/>
I am trying to send a listbox back to my controller from my view. I dynamically add items to it with a text box and button, and I want to be able to send all of these items back to my view in some kind of array. How would I go about doing this?
I had the following modelcode:
[HttpPost]
public ActionResult BasicIdentificationIndex(MyObject returndata, List<int> ints)
And then some input boxes:
<input type="text" name="ints" value="1" />
<input type="text" name="ints" value="4" />
<input type="text" name="ints" value="2" />
<input type="text" name="ints" value="8" />
This code works and is returned to my controller(not null).
EDIT:
My issue is that I cannot get a select list to post back to my controller. I would like to send the following back to my controller:
<select name="selectfrom" id="select-from" multiple size="5">
<option value="String1">Item 1</option>
<option value="String2">Item 2</option>
<option value="String3">Item 3</option>
<option value="String4">Item 4</option>
</select>
How would I go about doing this so that I can send a list of all the options( String1,String2,etc.) back to my controller? I have tried the following:
Controller:
public ActionResult BasicIdentificationIndex(BasicIdentificationModel returndata,ICollection<String> AerialItems)
Model:
public String AerialItems { get; set; }
View:
<select name="AerialItems" id="select-to" multiple size="5">
<option value="5">Item 5</option>
<option value="6">Item 6</option>
<option value="7">Item 7</option>
</select>
But The item returned back to the controller is always null.
You should be able to just model bind back to a collection of ints...
I'm somewhat confused because this seemed to be copied from Haack's blog post on the subject... What you have listed should work, but if it's not could you include the rest of your code?
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Figured it out. I need to use Javascript to select all the items in the list. This will post them all back in the collection.
I would like to make a select box using <g:select/> that translates to this html:
<select id="myselect" name="myselect">
<option value="r">RED</option>
<option value="g">GREEN</option>
<option value="b">BLUE</option>
</select>
I would also like the value to be preselected from a bean when the page reloads.
I'm doing this inside a so I have a table with each row having a separate option box.
I'm currently accomplishing this in the below html:
<g:each in=${mylist} status="i" var="myInst">
<select id="status${myInst}" name="status${myInst}" data-id="${myInst.id}">
<option value="r" <g:if test="${myInst.color == "r"}">selected</g:if>>RED</option>
<option value="g" <g:if test="${myInst.color == "g"}">selected</g:if>>Green</option>
<option value="b" <g:if test="${myInst.color == "b"}">selected</g:if>>BLUE</option>
</select>
</g:each>
This all works fine but I'd like to change that ugly <select> into <g:select>
<g:select id="myselect" name="myselect" value="${myInst.color}"
from="${['r': 'RED', 'g': 'GREEN', 'b': 'BLUE']}"
optionKey="key" optionValue="value" />
you have to declare the "myselect" inside your domain class. I have been having trouble with this too, but I'm about 2 weeks ahead of you. see how do I write a set for g:select tag
I am currently working on a Grails application and I am looking place a select list on the page and have its Key set to one value and the Value set to another. The code of all related elements is below:
Domain Object:
class Cars {
String carType
String car
}
Controller:
def create() {
List cars = Cars.list()
[dealerInstance: new Dealer(params), cars: cars]
render(text:"This site in curently under Maintainence!")
}
View:
<g:select name="cars.id" from="${cars}" optionKey="${carType}" optionValue="${car}"/>
Now when I run this code the list does get populated however data looks like below:
<select id="cars.id" name="cars.id">
<option value="com.app.Cars : 1">com.app.Cars : 1</option>
<option value="com.app.Cars : 2">com.app.Cars : 2</option>
<option value="com.app.Cars : 3">com.app.Cars : 3</option>
</select>
and then when I try this:
View:
<g:select name="cars.id" from="${cars.car}" optionKey="${carType}" optionValue="${car}"/>
I get the following:
<select id="cars.id" name="cars.id">
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Scoda">Scoda</option>
</select>
This is not what I want at all I want to be able to set a different Key Value pair on this select list so that I can use that data within my application, I would want the following to be generated:
<select id="cars.id" name="cars.id">
<option value="Saloon">Audi</option>
<option value="Hatch Back">BMW</option>
<option value="Estate">Scoda</option>
</select>
Can someone please tell me how I achieve this within Grails???
Try this:
<g:select id="cars.id" name="cars.id" from="${cars}" optionKey="carType" optionValue="car"/>
What basically happens:
When you use the from attribute, the tag iterates over the list you passed as an argument. optionKey and optionValue should be strings representing the attributes you want to access in the current object (being accessed by the iterator).
I have a form which allows the user to create extra "rows" using JQuery (using .clone) so that they can decide how many of the same information they need to submit. My issue is that I cannot work out how to access these form items within my controller.
the form that is being submitted may look like this
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="text" name="Amount" id="Amount">
<select name="Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Basically, the block between input and the select could be repeated an infinite number of times. When I submit to the controller I am then using FormCollection form to access the form elements. from there I am unsure how I can access the items that have been submitted. I thought of using a for loop and then accessing them via something like form["Amount"][i] but obviously that is not going to work.
Am I going about this the right way and if so, does anyone have any suggestions about how this might work?
Thanks in advance.
Old question, but still...
You can get the posted values as an array by calling Request.Form.GetValues, or Request.QueryString.GetValues. For example:
string[] amounts = Request.Form.GetValues("Amount");
And the amounts array will contain the correct values, so you can post values containing comas, dots, whatever, and don't worry about splitting/parsing it.
Of course if you are running MVC, use the modelbinder to do it. But you can use this if you are using webforms, a generic handler, etc...
Check out Model Binding To A List. Your Action method should be:
public ActionResult MyAction(string[] Amount, int[] Item){
// ...
}
However this will make you need to "link" the items. Alternatively create a "Item" class:
public class Item {
public string Amount { get; set; }
public int Item { get; set; }
}
And
public ActionResult MyAction(IList<Item> items){
// ...
}
And your markup should be:
<input type="hidden" name="items.Index" value="0" />
<input type="text" name="items[0].Amount" id="items[0].Amount">
<select name="items[0].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
<input type="hidden" name="items.Index" value="1" />
<input type="text" name="items[1].Amount" id="items[1].Amount">
<select name="items[1].Item">
<option value="1">Item 1"</option>
<option value="2">Item 2"</option>
<option value="3">Item 3"</option>
</select>
Etc...
I believe if you have multiple fields named Amount the values will be comma delimited.
To access each one just try:
string[] amounts = Request.Form["Amount"].Split(new char[] { ',' });
Keep in mind though, the inputs are not cleaned on submission so if someone enters a comma into the text box it's going to cause issues.
Hence I'd recommend numbering them.
I ended up realising that (blush) the mechanism which JQuery uses to find the string within the cloned row (to replace) is basically regex. Thus I just needed to escape the square brackets and period. Once I did this I was able use JQuery to create form as Phil Haack's blog suggested.
Onto my next issue...!
I would number them Amount1, Amount2, Amount3 etc.
You can change the id and name attribute of the input to something like this "Amount[1]","Amount[2]","Amount[3]" (yes, the id and name attribute can contain the special chars "[" or "]"). Then in the controller, write a http request parameter parser to get back the Amounts as collections.