HTML select box, show updated value - grails

please help me this issues
I got this select box
<select name="company">
<g:each in="${grailsApplication.config.Companies['No']}" var="no" status="index">
<option value="${no}">${no}: ${grailsApplication.config.Companies['Name'][index]}</option>
</g:each>
</select>
the select box has value like this
option1 01: abc
option2 02:def
Then, I used form update, when I choose option2, it saves value to db, but on select box, default value is 01:abc, how can i change it to 02:def after update.

Assuming that you are sending saved value in savedValue:
<select name="company">
<g:each in="${grailsApplication.config.company}" var="company" status="index">
<g:if test="${grailsApplication.config.company['no'][index] == savedValue}">
<option value="${grailsApplication.config.company['no'][index]}" selected>${grailsApplication.config.company['name'][index]}</option>
</g:if>
<g:else>
<option value="${grailsApplication.config.company['no'][index]}">${grailsApplication.config.company['name'][index]}</option>
</g:else>
</g:each>
</select>

Related

How keep value from input select after request?

I'm doing a search form in my site. And what i want is to keep on my inputs, the value selected by the user. It's ok for my checkbox, but i can't do it for select.
I show you my form first :
<select name="garde" id="garde">
<option value="">Choisir un type de garde</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
<div class="flex items-center">
<input id="chats" name="chats" value="1" id="chats" type="checkbox"
#if(request()->chats) {{ 'checked' }} #endif>
<label for="chats" class="ml-3 text-sm text-gray-600">Chat</label>
</div>
I tried to use some :
<select blablabla>
<option value="{{$garde->id}}" #if(request()->garde) {{'selected'}} #endif/>{{$garde->garde}}</option>
</select>
So it's certainly good for the value, but it doesn't display the good option name. Always sending me the last value from the database (which is 3 in this case).
My controller
$garde = request()->input("garde");
$chats = request()->input("chats");
$g = Garde::where('id', 'like', "%$garde%")->pluck('id');
Annonce::when($g, function ($s) use ($g) {
return $s->where('garde_id', $g);})
->when($chats, function ($s) use ($chats) {
return $s->where('chats', $chats);})
Edit : Here the "%$garde%" was after trying another option, using "$garde" doesn't changed anything.
As i said, it's ok for checkbox, but for select i can not keep this value without doing a "bidouille" as we say in french ^^
<select name="garde" id="garde">
<option value="{{ request()->garde ?? '' }}">
#if(request()->garde == 1) Chez le Pet-Sitter
#elseif(request()->garde == 2) Visite à domicile
#elseif(request()->garde == 3) Chez le Pet-Sitter/En visite
#endif
</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
But this is not a good practice i know it, and it's display 2 times the selected value ofc..
Have you got some ideas?
Maybe not using the pluck('id) in my controller is the key? But what is other way to do it?

strange behaviour when populating a select list in razor

I'm populating a select list using razor and its giving me some strange results. The items in the list contain spaces, the text created is ok, but the value has been split into sections for each word in the text description. Here's my razor code
<select name="fromReport[]" id="multiselectReport" class="form-control" size="15" multiple="multiple">
#foreach (var item in Model.AvailableReports)
{
<option value=#item>#item</option>
}
</select>
and the resulting HTML
<select id="multiselect_toRpt" class="form-control" name="toReport[]" size="15" multiple="multiple">
<option value="All" trades="" activity="" last="" week="">All Trades Activity Last Week</option>
<option value="All" trades="" for="" delivery="" last="" month="">All Trades For Delivery Last Month</option>
<option value="Energy" costs="" report="">Energy Costs Report</option>
</select>
so its created a section in the definition for each word in the text, why is this ?
Wrap the value attribute value in quotes. Single quotes or double quotes will work.
<option value="#item">#item</option>
figured it out
#foreach (var item in Model.AvailableReports)
{
<option value="#item">#item</option>
}

Set default value of dropdown through grails paramater

I send a user to a page with setting the parameters using JS like this:
window.location='/myPgae/MyController?terminId='+terminId;
On the view page I want to set the dafult value of one drop-down menu from the parameter if it was sent.
Drop down:
<select name="terminId" dojoType="dijit.form.Select" style="width:180px;">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3 Tage</option>
</select>
If parameter was sent I want to set option3 as the selected value. I know there is an option selected="selected" but how can I do the check?
I've got it:
<g:if test="${params.terminId}">
<option value="option3" selected="option3">option3</option>
</g:if>
<g:else>
<option value="option3">option3</option>
</g:else>

How to make a select box with constant list items with g:select

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

how to check for a condition in g select tag

I'm a newbie to Grails and GSPs.
I need to achieve the following code using g:select tag
<select name="applaiances" style="width: 200px" onchange="selectedTC(this); ">
<g:each in="${applaianceList}" var="appl">
<g:if test="${appl == "TELEVISION"}">
<option value="TELEVISION">TV</option>
</g:if>
<g:else>
<option value="${appl}">${appl}</option>
</g:else>
</g:each>
</select>
Any help would be appreciated.
Haven't tried this in an app but you could try something like:
<g:select name="applaiances" onchange="selectedTC(this);" from="${applaianceList}" optionKey="${{it=='TELEVISION'?'TELEVISION':it}}" optionValue="${{it=='TELEVISION'?'TV':it}}"></g:select>
Not sure about the optionKey but you can apply transformation via a closure on the optionValue.
Update:
This is documented here. Just search for the phrase "If you require even more control over how each"

Resources