I want to hide the minus value from the print during formatting in JSF. How can I achieve that.
For Example,
The Value : -340
Need to Display as : 340.00
Not like -340.00 or (340.00)
Is that possible? The current code is like that, but can not hide minus value.
<h:outputText value="#{paymentBill.amount}" >
<f:convertNumber pattern="#0.00" />
</h:outputText>
If it's <h:outputText> then you can use arbitrary EL expression as its value, if you're so dissatisfied with getting absolute value while preparing data in your managed bean, as it's rightly proposed by perissf and fareed, like:
value="#{(paymentBill.amount lt 0) ? -paymentBill.amount : paymentBill.amount}"
You can return the absolute value of the amount In the backing bean.
Return Math.abs(amount) instead of amount.
Related
I have a p:dataTable with a pe:toolTip appended for all cells. The tooltip text is displaying ok, but it doesnt apply the f:convertNumber format that I have for my table cells:
My columns look like this:
<h:outputText value="#{informeAux.vagonesKm}" title="#{informeAux.vagonesKm}">
<f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
</h:outputText>
And the number in the table looks like this: 11.335,40 but the Tooltip displays the value like this: 11335.4 (Number is not formatted)
Haven't found a way to properly display it, any help will be appreciated.
I tried to use the converter attribute for the <pe:tooltip> but couldn't get it working.
In the end I made a separate property 'vagonesKmTitle' and applied the decimal format in my bean.
<h:outputText value="#{informeAux.vagonesKm}" title="#{informeAux.vagonesKmTitle}">
<f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
</h:outputText>
What is the best way of using special characters and formatting in a dynamically generated h:commandLink?
I am rendering an equation (e.g. A=π*r2) as a list of h:commandLink items, so that each symbol in the equation can be separately clicked:
JSF:
<ui:repeat value="#{eqBean.eqSymbolDisplays}" var="eqSym">
<h:commandLink value="#{eqSym.text}" styleClass="#{eqSym.styleClass}" action="#{eqBean.eqSymbolClick(eqSym)}" />
</ui:repeat>
Bean:
public String getText(){
// Return the text for a given symbol
}
The question is what the getText method should do when the symbol needs a special character, special mathematical symbol, and/or needs to be a super-script or sub-script?
Here are some specific problems/questions:
(1) How do I use the greek letter π symbol in a CommandLink? If I return π then that is what gets displayed, not the greek symbol
(2) What is the best way to do a superscript in a CommandLink? I could use a CSS style but some people say that is a bad idea, especially when the superscript implies meaning, rather than just presentation, as it does for a number raised to a power.
See :
Beware CSS for Superscript/Subcript
The answer was obvious in the end: Just replace the value attribute of the h:commandLink with a child h:outputText element that has escape="false" :
JSF:
<ui:repeat value="#{eqBean.eqSymbolDisplays}" var="eqSym">
<h:commandLink styleClass="#{eqSym.styleClass}" action="#{eqBean.eqSymbolClick(eqSym)}" >
<h:outputText value="#{eqSym.htmlText}" escape="false"/>
</h:commandLink>
</ui:repeat>
Is there a way to specify the order in which the inputs should be set after a submit?
Here is my case:
<h:inputText id="fieldA" value=#{myBean.myObject.fieldA}" />
<h:inputText id="fieldB" value=#{myBean.myObject.fieldB}" />
<p:autoComplete id="myObject" value=#{myBean.myObject" converter="myObjectConverter" />
<h:inputText id="fieldC" value=#{myBean.myObject.fieldD}" />
<h:inputText id="fieldD" value=#{myBean.myObject.fieldC}" />
The issue I am encountering is that, as the inputs are processed in the ordered they are declared, fieldA and fieldB are set in the initial instance of myObject, then myObject is set (with a new instance thus filedA and fieldB values are lost), and finally fieldC and fieldD are set with no problem.
If I could manage to start by setting myObject first, that would solve my problem.
I will temporarily set the fields and myObject into two different attributes of my bean, and populate myObject after clicking a save button. But it looks more like a hack than a real solution.
Needless to say that declaring the autocomplete before the inputtexts is not an option.
Thanks in advance.
In shortcut:
You can use <p:inputText> tag from primefaces. Then, you can disable all inputs. Add ajax to your autoComplete, and update other inputs after processing autoComplete. Inputs disable attribute can be set to depend on whether the autoComplete is not null. This way you will make the user to enter the autoComplet first.
you can try to set immediate="true" to p:autocomplete, so that it will be processed in the APPLY_REQUEST_VALUES phase, before all other components.
The simple solution is to update h:inputTexts when p:autocomplete item is selected to reflect its values:
<p:autoComplete id="myObject" value="#{myBean.myObject}" ...>
<p:ajax event="itemSelect" process="#this" update="fieldA fieldB fieldC fieldD" />
</p:autoComplete>
but this reverts user inputs on h:inputTexts. And since you can't move p:autocomplete on top, probably this is not acceptable too.
In case you can't/don't want to use ajax, you can force an early model update:
<p:autoComplete id="myObject" value="#{myBean.myObject}" immediate="true"
valueChangeListener="#{component.updateModel(facesContext)}" ... />
but, in my opinion, this is not very user friendly...
P.S. this time it's tested :)
There's no pretty way to get around this; your situation is already less than ideal and is hacky (re: not being able to simply reorder the fields). One workaround is for you to set fieldA and fieldB as attributes of myObject. In the converter, you could then pull the values off the components. Observe
Set attributes thus
<h:inputText id="fieldA" binding=#{fieldA}" />
<h:inputText id="fieldB" binding=#{fieldB}" />
<p:autoComplete id="myObject" value=#{myBean.myObject" converter="myObjectConverter">
<f:attribute name="fieldA" value="#{fieldA}"/>
<f:attribute name="fieldB" value="#{fieldB}"/>
</p:autoComplete>
The binding attribute effectively turns those components into page-scoped variables, allowing you to then pass them as attributes on your p:autocomplete
Get the values of those variables in your converter
//Retrieve the fields and cast to UIInput, necessary
//to retrieve the submitted values
UIInput fieldA = (UIInput) component.getAttributes().get("fieldA");
UIInput fieldB = (UIInput) component.getAttributes().get("fieldB");
//Retrieve the submitted values and do whatever you need to do
String valueA = fieldA.getSubmittedValue().toString();
String valueB = fieldB.getSubmittedValue().toString();
More importantly, why can't you just reorder the fields/logical flow of your form? You can avoid all this nasty business if you did
I'm using the repeat tag of JSF 2.0 to loop through a list of objects and display some of their properties. I want to use the varStatus attribute of repeat so that I can access the loop index, the number of the last list item, and to tell whether the end of the list has been reached (so the spacer won't be displayed). I thought this would work:
<ui:repeat var="anObject" varStatus="repeatStatus" value="#{objectList}">
<h:panelGroup>
<h:outputText value="Item #{repeatStatus.index + 1} of #{repeatStatus.end}" />
<h:outputText value="#{anObject.text}" />
</h:panelGroup>
<h:outputText value=" " rendered="#{false == repeatStatus.last}" />
</ui:repeat>
However, it never displays anything for repeatStatus.end. The index and last properties work well.
Instead of repeatStatus.end, I tried using objectList.size(), but that worked for only the first item in the list.
How can I display the number of items in the list as part of the "Item x of y" text?
The end is only used when you set the size attribute.
<ui:repeat ... size="#{fn:length(objectList)}">
Alternatively, you can also just use it directly.
Item #{repeatStatus.index + 1} of #{fn:length(objectList)}
By the way, the boolean comparison in #{false == repeatStatus.last} is ugly. It returns a boolean already; if you want to negate it, rather use #{not repeatStatus.last}.
Is there any inbuilt number validator tag in JSF that checks whether an input entered in h:inputext field is a number?
The first question was answered. Edited to explain the next problem:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" required="false" maxlength="4" >
<f:convertNumber longOnly="true"/>
</h:inputText>
Backing Bean
private long numRecords = null;
If I use String or Integer object in the backing bean , value is not being set. Now when I use primitive int, 0 is being printed on the screen. I would like the screen to be empty.
You can use f:convertNumber (use the integerOnly attribute).
You can get more information here.
You can use:
<f:validateLongRange minimum="20" maximum="1000" />
Where minimum is the smallest number allowed and maximum is the largest.
Look here for more details
JSF Number validation for inputtext
mention f:converterNumber component in between h inputText component and mention the attributes integerOnly and type.
<h:inputText id="textMobileId" label="Mobile" styleClass="controlfont" value="#{UserRegistrationBean.textMobile}">
<f:convertNumber integerOnly="true" type="number" />
</h:inputText>
If you enter abcd in Mobile textbox at the time when you click on commandbutton it automatically shows an error like
Mobile: 'abcd' is not a number.
i8taken solution converts number into long without validation message (at least in my case: JSF2 / global messages per page). For proper validation message you can
1. check value in action method in bean;
or
2. use converter attribute for inputText:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" maxlength="4" converter="javax.faces.Integer" />
You can simply use the passthrough, so first add this library
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
and after use this
<h:inputText id="numberId" pt:type="number" />