How to get a confirmation box when value change in h:selectOneMenu - jsf

I'm having trouble trying to get a confirmation box when someone change the value in a h:selectOneMenu and if the person accept, then the value changes, otherwise it return to its original value.
I do not know how to manage to do this, I have made some research but most of the answer I found were for PrimeFaces (which I do not use).
Here's the relevant part of code with the selectOneMenu :
<ui:repeat value="#{CommandeBean.choixMetaProprietes}" var="choixMetaProprietes">
<h:selectOneMenu value="#{choixMetaProprietes.metaProprieteSelectionnee}">
<f:selectItems noSelectionValue="#{CommandeBean.retrieveDefaultMetaProprietesValue()}" value="#{choixMetaProprietes.metaProprietes}" var="metaPropriete" itemLabel="#{metaProprieteConverter.getMetaProprieteLabel(metaPropriete)}" itemValue="#{metaPropriete}"/>
<f:converter converterId="com.festo.didactic.ca.webapp.entreecommandes.converter.MetaProprieteConverter"/>
</h:selectOneMenu>
</ui:repeat>
I'm open to any idea that could help me solve this problem.

I finally manage to do it by storing the data in an input hidden and then compare the actual value of the selectOneMenu with the input in query.
i was then able to call a confirmation box via confirm('blabla bla').
the result would then tell me if i need to keep the value or put the previous one.

Related

How to change order in which the components are processed

I have a selectOneMenu and an editor. What I want is to submit the value of the editor before the value of the selectOneMenu. The code looks like this
<p:selectOneMenu value="#{myBB.selectedItem}">
<f:selectItems value="#{myBB.selectItems}"/>
<p:ajax event="change" process="itemText #this" update=":mainForm"/>
</p:selectOneMenu>
<p:editor id="itemText" value="#{myBB.selectedItem.text}"/>
It looks like the order of elements in process="itemText #this" doesn't matter, because when I change it, the values are submitted in unchanged order.
The problem is, that the selectedItem of the editor is changed by the selection before the value from the editor is submitted.
Am I right, that the order doesn't matter and it is submitted based on the order in the DOM tree? (When I change order of the input fields it's working as I would like to)
What is the best way to work around this?
You shouldn't rely on things like layout or processing order in your code. If the problem is that itemText value is reset when selectedItem is changed, then bind the text to separate String text bean variable, and update selectedItem.text in some other code, maybe <p:ajax listener="#{...}".

rich:suggestionbox how to call bean in case user aborts selection

Old SuggestionBox component for RichFaces (version 3.3... yes, I have to maintain legacy code, weep for my fate) is pretty useful, but has one downside.
If user enter some text in input component and then click somewhere else than entry on list of suggestion, partially filled input is left as is. It is ugly and can be confusing, giving impression that something is selected when it is not.
I want to have separate call to bean in this case, allowing me to remove text, if nothing got selected.
Example code:
<h:inputText id="selectorInput" value="#{backingBean.inputText}" label="Select something:" />
<rich:suggestionbox id="suggestion" for="selectorInput" suggestionAction="#{backingBean.resolveSuggestions}" var="sug">
<a4j:support event="onselect" reRender="someForm" action="#{backingBean.select(sug)}" />
<h:column>#{sug.name}</h:column>
</rich:suggestionbox>
I tried to add a4j:support for h:inputText with event="onblur", but it is called before backingBean.select(sug) and has no way to know if something was selected or not, making it almost useless. Adding other events to suggestionbox itself appear to not work at all or even break suggestionbox.
Is there any other way?
The backing JavaScript object has a getSelectedItems() method. It returns an empty array if nothing was selected.
To get the object use
document.getElementById(clientId).component

jsf inputtext doesn't show value from bean

I have the follow situation:
I have a bean that send to form some data, but only in outputlabel the data from the bean is displayed.
I tried to use primefaces, but the same problems persist.
my code:
<h:outputLabel value="#{Bean.name}" id="name2" />
<h:inputText value="#{Bean.name}" id="name" />
<p:inputText value="#{Bean.name}" id="name3" />
Any idea why?
You should have given the bean's code also, to help us better analyze the problem.
Normally you should check for the following:
Check whether you are specifying a correct bean name. Normally
bean's name is same as that of class, except that first letter
should be lowercase. In your case it should be #{bean.name} or else,
specify your custom name with #Named("Bean").
Check whether the getters and setters such as getName() are properly
provided. It may happen that you might have reset the name property in
your bean in the get method itself. Because of which first time it
shows you properly in outputLabel and then in next call to getName it may give you null or empty String. To check this, try put your inputText tag first, and check.
I solve my problem.
When I tried show the values, I was trying recover data from database by pass an ajax action. So, When I clicked at button to retrieve the datas, some of my inputText were set as a required. And because this the data is just displaying into label and not inside of inputtext with required. But because ajax, the request were not called correctly.
When I remove the required from inputtext, it works fine.

pass parameter from f:selectItems in h:selectOneListbox

I have a selectOneListbox that, when clicked, should pass an additional parameter (id) to the server. As it is now, the user sees a list of names and when they select one I can get the name. But, each name also has a unique id associated with it that I don't want the user to see - how can I pass the unique id of the selected name to the backing bean without the user ever seeing it? Is it possible? I was trying to figure out how to use the f:param but I don't see how that will work here.
<h:selectOneListbox id="listBox" value="#{ScheduleMB.clients}" size="5"
rendered="#{ScheduleMB.showClients}" >
<f:selectItems value="#{ScheduleMB.clientList}" var="c"
itemLabel="#{c.lastName} #{', '} #{c.firstName}" itemValue="#{c.lastName}" />
<f:ajax event="click" listener="#{ScheduleMB.clickListener}"
render="group" />
</h:selectOneListbox>
The <f:param> serves a different purpose. Even if the <f:param> was possible, it would still end up being visible in the generated HTML output. The enduser would just do rightclick and View Source and then see the IDs being definied as option values.
Your best bet is to retrieve the ID from the DB based on a different unique identifier, perhaps the unique combination of firstname+lastname.
It does by the way not make any sense to me why you would like to hide the ID from the output. It'd be so much easier if you used that as option value, even more if you used a converter so that you can just pass the whole #{c} as option value. The enduser can't spoof/change it in any way. JSF will revalidate the submitted value against the list of available options (which are definied in server side anyway).

<h:selectOneMenu> with temporary disabled SelectItems

I have a <h:selectOneMenu> that renders the value of the cardStatus from my object model.
A CardStatus has an boolean attribute 'temporaryDisabled' that means that the value is still valid but should not be used by the user.
Now, if my model has cardStatus set to a temporary disabled value, how can I show this value in the dropdown combobox and still prevent the user from changing the value to another temporary disabled status?
If I just delete the disabled card statuses from the list of SelectItems that I feed to <h:selectOneMenu> then when the select gets rendered it will automatically select the first item in the list an submit it next time consequently wrongly changing my value in the model.
If I include the disabled card statuses in the list of SelectItems but set the value of the disabled attribute to true for their corresponding items, they are rendered in HTML disabled and not submitted so I get a null value in my model which is also wrong.
I am stuck. Any advice is kindly appreciated.
Best regards,
Dan.
Finally what I did was to use a piece of jQuery code that gets executed after the page is loaded.
<h:selectOneMenu
id="cardStatus"
value="#{someBean.cardStatus}"
converter="selectItemConverter">
<f:selectItem itemValue="E|A" itemLabel="Active" />
<f:selectItem itemValue="E|S" itemLabel="Stolen" />
<f:selectItem itemValue="D|B" itemLabel="Blocked" />
<f:selectItem itemValue="E|L" itemLabel="Lost" />
<f:selectItem itemValue="D|C" itemLabel="Counterfeit" />
</h:selectOneMenu>
What the javascript code does is to scan all the items and for each item with a value starting with the prefix D| hide the item using jQuery's hide() function. This way the combobox is acting as all values would be valid/enabled, but the user will not be able to select inactive values because they are not visible. Furthermore, if the default selected value is one of the values starting with D|, the value will still be shown as default value but the user is not able to see it in the list of options he/she can choose from.
"that means that the value is still valid but should not be used by the user"
Help me understand this piece. You want to display these choices in an h:selectOneMenu but not allow the user to select them? I guess I don't understand why you would present them to the user if they are invalid options for them?
You could always create a validator that validates against "temporaryDisabled" if that's what you're trying to accomplish ... let me know more about what you want the end-user to see and I can probably help.
<h:selectOneMenu
id="cardStatus"
value="#{someBean.cardStatus}"
converter="selectItemConverter">
<f:selectItem itemValue="#{null}" itemLabel="" />
<f:selectItems
value="#{cardStatusBean.cardStatuses}"
var="cardStatus"
itemValue="#{cardStatus}"
itemLabel="#{cardStatus.name}"
itemDisabled="#{cardStatus.temporaryDisabled}"/>
</h:selectOneMenu>
This would give you a default choice with a value of null and an empty label. Would that accomplish what you are looking for?
For the record, if using JSF 2.0, f:selectItem and f:selectItems have an "itemDisabled" attribute that produces the desired behavior.

Resources