Use <form:select> tag with a map - forms

Is there a way to map the data inside a map to tag?
I have a map Map<String, Integer> in my code.
Is there a way to map the option labels to the String in the map and the Integer to the option values?

The <form:options> tag supports what you want right out of the box, using the items attribute. You can do something like this:
LinkedHashMap<Integer, String> states = new LinkedHashMap<Integer, String>();
states.put(1, "Alabama");
states.put(2, "Alaska");
states.put(3, "Arizona");
states.put(4, "Arkansas");
states.put(5, "California");
And so on. Then in your form:
<form:select path="state">
<form:options items="${states}" />
</form:select>
That will be rendered to something like:
<select name="state">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
</select>

See the Spring form:select and form:options documentation. Use items, itemValue, and itemLabel as needed.
<form:select path="myFormVariable">
<form:option value="0" label="Select One" />
<form:options items="${myCollection}" itemValue="propertyToUseAsValue" itemLabel="propertyToUseAsDisplay" />
</form:select>

Related

How to add multiselect in form

I am newbie in SalesForce Commerce Cloud. So if my question is silly then I am sorry in advance.
My Target is to show the multiselect on customer registration form. By searching on internet what I have done
Step 1: Add form definition
<field formid="brands" label="label.select.interestedbrands.preferences" type="integer" mandatory="true" binding="profile.interestedBrands">
<options>
<option optionid="1" label="Brand 1" value="1"/>
<option optionid="2" label="Brand 2" value="2" />
<option optionid="3" label="Brand 3" value="3" />
<option optionid="4" label="Brand 4" value="4" />
<option optionid="5" label="Brand 5" value="5" />
<option optionid="6" label="Brand 6" value="6" />
<option optionid="7" label="Brand 7" value="7" />
</options>
</field>
Step 2: In isml template file I have rendered it like this
<select class="custom-select form-control" id="brands" <isprint value="${pdict.preferencesForm.brands.attributes}" encoding="off" /> multiple>
<isloop items=${pdict.preferencesForm.brands.options} var="brand">
<option id="${brand.id}" value="${brand.htmlValue}" <isif condition="${brand.selected}">selected</isif> >${brand.label}</option>
</isloop>
</select>
To save it in the system object profile I have added one new attribute which type is Enum of Integer and multiselect true.
Problem: Now when I try to save the form in the controller it have only one value whether I select multiple options or not.
Can you please let me know how can I add the multi-select in the SFCC and which mistake I am doing?

Cypress how to store all option texts from a <select> element?

I want to store all option texts from dropdown field. Application code is
<select id="regionSelect" onchange="setRegionalManagerInfo()" data-val="true" data-val-required="The Region field is required." name="RegionalInfo.RegionId" class="form-control input-validation-error">
<option selected="selected" value="" data-regionalmanageremail="" data-regionalmanagerphonenumber="" data-regionalmanagerphonenumberextension="">— Select —</option>
<option value="1" data-regionalmanageremail="user1#gmail.com" data-regionalmanagerphonenumber="(123) 456-7890">Region 1</option>
<option value="2" data-regionalmanageremail="user2#gmail.com" data-regionalmanagerphonenumber="(123) 456-7890">Region 2</option>
<option value="5" data-regionalmanageremail="user3#gmail.com" data-regionalmanagerphonenumber="">Region 3</option>
</select>
let array = []
cy.get('select option').each(($option, index )=> {
array.push(Cypress.$($option).text())
console.log(array[index]);
})
Is a bit simpler way to get the data. Right now this data is stored in the array, so you can access the content inside the each as well as outside.
This is more easily done with a combination of .then() and .map(),
cy.get('select option').then($options => {
return [...$options].map(option => option.innerText)
})

How to choose an item in form:select component?

I got this form:select in my JSP file:
<form:select id="locale" path="locale">
<form:option value='${preferredLanguage}' />
<form:options itemValue="key" itemLabel="value" items="${locales}" />
</form:select>
After the page is being rendered, the selected item is added as a first item and as a another item.
I tried to solve it using:
defaultValue='${preferredLanguage}'
Either:
defaultLabel='${preferredLanguage}'
And it didn't succeed, any other ideas how to solve it ?
I used to fixed it using if-else, solution:
<form:select id="locale" path="locale">
<c:forEach items="${locales}" var="loc">
<c:choose>
<c:when test="${loc.key == currentLocale}">
<option class="bdi" itemValue="key" value="${loc.key}" selected><bdi><div>${loc.value}</div></bdi></option>
</c:when>
<c:otherwise>
<option class="bdi" itemValue="key" value="${loc.key}"><bdi><div>${loc.value}</div></bdi></option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>

jQuery addClass using input select

Im a bit of a jQuery novice.
I have 5 options in a drop down menu.
I want to add a class in a div depending on which option is selected.
This class will then change the layout of the content using CSS.
Here is an example if it helps!
Thanks
<form>
<select>
<option id="1">layout 1</option>
<option id="2">layout 2</option>
<option id="3" selected>layout 3</option>
<option id="4">layout 4</option>
<option id="5">layout 5</option>
</select>
<div class="3">styled content</div>
</form>
You can use the ".attr()" to set the class attribute of the div onchange.
You're best to change the option id to value first. then:
$("select").change(function() {
$("div").attr("class", $(this).val());
});
(EDIT) Change it to:
$("select#np_blog_layout").change(function() {
$("div#changebox").attr("class", $(this).val());
});
What 'rudeovski ze bear' said, but if you still want to set it to the div's class to the selected elements id, here it is.
$('select').change(function() {
$('div').attr('class', $(this).attr('id'));
});
First off, you don't have to use id to your options. Put the values in value attribute. Put ID to your div and select so you can select them using jQuery.
<form>
<select Id="selectElement">
<option value="1">layout 1</option>
<option value="2">layout 2</option>
<option value="3" selected>layout 3</option>
<option value="4">layout 4</option>
<option value="5">layout 5</option>
</select>
<div id="styledContent" class="3">styled content</div>
</form>
On JS
//Attach event handler to select element's onchange event
$('#SelectElement').bind("change",changeStyle);
//The event handler
function changeStyle()
{
//Set class to selected value
$('#styledContent').class($('#SelectElement').val());
}

ColdFusion auto select option

What is the best way to automatically set a selected item for a select/option element on post-back? Here's the way we're currently doing it:
<select id="grade" name="grade">
<option value="A"<cfif form.grade = 'A'> selected="selected"</cfif>>A</option>
<option value="B"<cfif form.grade = 'B'> selected="selected"</cfif>>B</option>
<option value="C"<cfif form.grade = 'C'> selected="selected"</cfif>>C</option>
<option value="D"<cfif form.grade = 'D'> selected="selected"</cfif>>D</option>
<option value="F"<cfif form.grade = 'F'> selected="selected"</cfif>>F</option>
</select>
Is there a cleaner or easier way to do this with ColdFusion? Thanks in advance!
In my opinion, one of the best ways to go is to use a CFSelect:
<cfquery name="getGrades" datasource="#application.dsn#">
select gradeLetter from Grades
</cfquery>
<cfselect
name="grade"
query="getGrades"
display="gradeLetter"
value="gradeLetter"
selected="#form.grade#" />
Like this:
<cfsavecontent variable="GradeOptions">
A:A
B:B
C:C
D:D
F:F
</cfsavecontent>
<select id="grade" name="grade">
<cfloop index="CurOpt" list="#trim(GradeOptions)#" delimiters="#Chr(10)#">
<option value="#ListFirst(CurOpt,':')#"<cfif form.grade EQ ListFirst(CurOpt,':')> selected="selected"</cfif>>#ListRest(CurOpt,':')#</option>
</cfloop>
</select>
That assumes you always have separate value:label information - if your value and label are always the same, you can do this:
<cfsavecontent variable="GradeOptions">
A
B
C
D
F
</cfsavecontent>
<select id="grade" name="grade">
<cfloop index="CurOpt" list="#trim(GradeOptions)#" delimiters="#Chr(10)#">
<option<cfif form.grade EQ CurOpt> selected="selected"</cfif>>#CurOpt#</option>
</cfloop>
</select>
You could also do this with an array of structs.
<cfparam name="form.grade" default="C">
<cfset mydata = [{grade="A",value="A"},{grade="B",value="B"},{grade="C",value="C"},{grade="D",value="D"},{grade="F",value="F"}]>
<cfoutput>
<select id="grade" name="grade">
<cfloop array="#mydata#" index="i">
<option value="#i['value']#"<cfif form.grade EQ i['grade']> selected="selected"</cfif>>#i['value']#</option>
</cfloop>
</select>
</cfoutput>
using cfscript with functions
<cfscript>
Function setSelected(val1, val2){
if (val1 EQ val2)
{
Return 'selected="selected"';
}
else
{
Return '';
}
}
</cfscript>
<select id="grade" name="grade">
<option value="A" #setSelected('A', form.grade)#>A</option>
<option value="B" #setSelected('B', form.grade)#>B</option>
<option value="C" #setSelected('C', form.grade)#>C</option>
<option value="D" #setSelected('D', form.grade)#>D</option>
<option value="F" #setSelected('F', form.grade)#>F</option>
</select>
What about this?
<cfparam name="form.grade" default="A">
<cfoutput>
<select id="grade" name="grade">
<cfloop index="code" from="65" to="90">
<option value="#Chr(code)#"<cfif form.grade EQ Chr(code)> selected="selected"</cfif>>#Chr(code)#</option>
</cfloop>
</select>
</cfoutput>
A bit tricky, yeah :)
You know, it may be unpopular to say so, but the solution that you originally outlined in the question is the best.
It's simple, it's easy to see what is being done, and it's not trying to be tricky just to be tricky.
Sometimes you just need to wear gloves
http://thedailywtf.com/Articles/The_Complicator_0x27_s_Gloves.aspx
To expand on my other answer, what you should be doing is properly separating your data from your interface, by storing the grades in your database, and using code similar to:
<cfset GradeOptions = Grades.readAvailable() />
<select id="grade" name="grade">
<cfloop query="GradeOptions">
<option value="#GradeCode#"
<cfif Form.Grade EQ GradeCode>selected="selected</cfif>
>#GradeCode# - #GradeDesc#</option>
</cfloop>
</select>
(Again, if grades are only to be treated as a single letter, the values can be provided in a simple list/array rather than query.)
The key thing is, you can change your grade structure without having to change your interface, and likewise updating the interface doesn't require you to know what the grades are.
To be honest I can't see how any of these are better than a corrected version of your initial pass (below)
<select id="grade" name="grade">
<option value="A"<cfif form.grade EQ "A"> selected </cfif> >A</option>
<option value="B"<cfif form.grade EQ "B"> selected </cfif> >B</option>
<option value="C"<cfif form.grade EQ "C"> selected </cfif> >C</option>
<option value="D"<cfif form.grade EQ "D"> selected </cfif> >D</option>
<option value="F"<cfif form.grade EQ "F"> selected </cfif> >F</option>
</select>
It is simple, clean and understandable.
If you just feel a need to be slicker and are going to be doing a lot of UI manipulation invest some time in jQuery. Study Ray Camden's jQuery and CF Posts and Ben Nadel's Javascript and CF Posts and soon this will be second nature...
<script type="text/javascript">
jQuery(document).ready(function() {
$("#grade option[value='<CFOUTPUT>#FORM.Grade#</CFOUTPUT>']")
.attr('selected', 'selected');
});
</script>
<select id="grade" name="grade">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="F">F</option>
</select>
Sure, it's wackier looking than some of the other options here but amazingly powerful at solving problems that CF just isn't good at once you learn it (trust me, it will quickly make sense and you will wonder how you ever did client UI code without it).
Learn any of the popular JavaScript libraries and your ColdFusion client side code will become dramatically more elegant and powerful.