How to choose an item in form:select component? - forms

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>

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?

Primefaces v.5.3.5 confirm dialog render issue outside main h:form and inside main form is rendered but it is not working properly

I am using PF v.5.3.5 and JSF v.2.2.8. It is a frequent topic in SO. I noticed that there is a bug in PF v.5.3.5 documentation related to the appendTo attribute.
1st approach
The p:confirmDialog is rendered and commandbuttons are rendered but actions does not work and message is not rendered if it is placed inside of nested h:form.
2st approach
If I place this dialog outside of main h:form it is not rendered at all also if I add the global="true" attribute.
3st approach
The p:confirmDialog is rendered and commandbuttons are rendered but actions does not work and message is rendered if the nested h:form is removed.
<h:form>
...
<p:confirmDialog id="askSessionDialog1" widgetVar="askSessionDialog1" severity="alert"
appendTo="#(body)" rendered="#{treeData.askSessionDialogRendered}" visible="#{treeData.askSessionDialogRendered}">
<h:form>
<f:facet name="message">
<h:outputText value="#{msg.WEB_ADMIN_PAGES_TREESEGMENT_NOSESSION}" escape="false"/>
</f:facet>
<p:commandButton value="#{msg.WEB_BUTTONS_OK}" action="#{treeData.save(false, true)}" icon="fa fa-check"
update="#(form)" type="button" />
<p:commandButton value="#{msg.WEB_BUTTONS_CANCEL}" action="#{treeData.setAskSessionDialogRendered(false)}"
icon="fa fa-close" onclick="PF('askSessionDialog1.hide()')" update="#(form)" type="reset" />
</h:form>
</p:confirmDialog>
...
</h:form>
BECKEND PART
setAskSessionDialogRendered(true);
RequestContext.getCurrentInstance().update("treeSegmentForm askSessionDialog askTurnOffDialog askSessionDialog1 askTurnOffDialog1");
Thanks in advance for constructive posts and comments.
SOLVED
This is the best approach that works for me now.
<h:form>
...
<p:confirmDialog id="askSessionDialog" widgetVar="askSessionDialog" severity="alert"
appendTo="#(body)" rendered="#{treeData.askSessionDialogRendered}" visible="#{treeData.askSessionDialogRendered}">
<f:facet name="message">
<h:outputText value="#{msg.WEB_ADMIN_PAGES_TREESEGMENT_NOSESSION}" escape="false"/>
</f:facet>
<h:form>
<p:commandButton value="#{msg.WEB_BUTTONS_OK}" icon="fa fa-check" type="button" accesskey="o">
<p:ajax event="click" listener="#{treeData.save(false, true)}" oncomplete="PF('askSessionDialog').hide()"
update="#(form)" />
</p:commandButton>
<p:commandButton value="#{msg.WEB_BUTTONS_CANCEL}" icon="fa fa-close" type="reset" accesskey="c">
<p:ajax event="click" listener="#{treeData.setAskSessionDialogRendered(false)}"
onsuccess="PF('askSessionDialog').hide()" update="#(form)" />
</p:commandButton>
</h:form>
</p:confirmDialog>
...
</h:form>
BACKEND
setAskSessionDialogRendered(true);
RequestContext.getCurrentInstance().update("treeSegmentForm");
SPACIAL THANKS TO: #YagamiLight
He helped me to kick off my solution.

ZK framework getting value of a component on zul page

I am trying to get the value of a textbox on a zul page by using some kind of getValue method. I should handle this on the zul page, not on a controller. I need to assign a listbox cell (which is the first cell of the list box below) with a value coming from the textbox.
<listcell>
<label value="" />
</listcell>
<listcell>
<toolbarbutton visible="true"
image="/resources/images/icons/1616/page_text.gif" />
</listcell>
<listcell>
<label value="#{file.name}" />
</listcell>
<listcell>
<toolbarbutton forward="onClick=onRemoveMultipleFiles"
visible="true" id="newFileAndCommentRemove" image="/resources/images/icons/1616/delete.png" />
</listcell>
</listitem>
If what you want is that after the textbox is filled then the first cell will fill with its value you can do it like this:
put an id into the label in the cell
put an onChange operation in the textbox so when the textbox change you can put its value into the cell
like this:
<textbox id="textbox" onChange="label.setValue(self.getValue())"/>
<listbox id="newFileAndComment">
<listhead>
<listheader label="1" width="30px" />
</listhead>
<listitem self="#{each=file}">
<listcell>
<label id="label"/>
</listcell>
</listitem>

Use <form:select> tag with a map

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>

Adding form:checbox inside forEach

I have a table in a jsp and i add the value to it as follows
<c:forEach items="${features}" var="feature">
<c:choose>
<c:when test="${feature.present == true}">
<c:set var="featurePresent" value="REMOVE"/>
</c:when>
<c:otherwise>
<c:set var="featurePresent" value="ADD"/>
</c:otherwise>
</c:choose>
<tr>
<td>${feature.name}</td>
<td>${featurePresent}</td>
<td><form:checkbox path="" value=""/></td>
</tr>
</c:forEach>
I am trying to add the checkbox ..
My Feature class is
class Feature{
private String name;
private boolen present;
private boolean checkbox
}
How can i set the checkbox in the jsp page
I am assuming your command object has a List<Feature> features property and you want the checkbox state for each Feature object in the list when you submit the form. You may try:
<c:forEach items="${features}" var="feature" varStatus="i">
<c:choose>
<c:when test="${feature.present == true}">
<c:set var="featurePresent" value="REMOVE"/>
</c:when>
<c:otherwise>
<c:set var="featurePresent" value="ADD"/>
</c:otherwise>
</c:choose>
<tr>
<td>${feature.name}</td>
<td>${featurePresent}</td>
<td><form:checkbox path="features[${i.index}].checkbox" value=""/></td>
</tr>
</c:forEach>