Can't we give two values for one lable in JSF? - jsf

I have one label field which is license and I want give two license numbers for this..how to give two values here. I have one properties file (manageprofile) and one java class (manageprofiledatabean) which contains the license values
<h:outputLabel value="#('label.manageprofile.license')"/>
<h:outputText value="#{manageProfileDataBean.license}" />
<h:outputText escape="false" value=" "></h:outputText>

Concat both values in one string variable of manageProfileDataBean and then set it in
public class ManageProfileDataBean{
private String license = "";
private String val1 = "val1":
private String val2 = "val2";
public ManageProfileDataBean(){
license = val1+val2;
}
//setter getter
}

firstly, h:outputLabel is meant to be used for labelling input controls - it renders as the html <label> tag(e.g.)
<h:outputLabel for="myname" value="#{bean.labelValue}" />
<h:inputText id="myname" value="#{bean.name} />
If you need to specify multiple values in expression language you can do so like this:
<h:outputText value="#{bean.property1 bean.property2}"/>
so I guess in your case you would write it as
<h:outputText value="#{'label.manageprofile.license' manageProfileDataBean.license}" />
Finally, you don't need to use an h:outputText if you're not referenceing any propery so the last pair of nbsp characters don;t need to be in in outputText tag at all.

Related

<p:selectOneMenu> with bold labels for some items

I'm trying to have a Primefaces "SelectOneMenu" with items that can either have a "normal" label or a "bold" label. I tried 2 different variants, but both don't work as expected.
Variant 1:
<p:selectOneMenu id="menu" value="#{selected}">
<f:selectItems value="#{mySelectItems}"/>
</p:selectOneMenu>
public List<SelectItem> getMySelectItems() {
List<SelectItem> list = new ArrayList<SelectItem>();
for (...) {
SelectItem item = new SelectItem(value, label);
item.setEscape(false);
if (...)
item.setLabel("<b>" + item.getLabel() + "</b>");
list.add(item);
}
return list;
}
This way I can have some of the items in the menu with a bold label and some with a normal label. The problem: The field that is showing the current selection is displaying the text as "< b >...< /b >". The text doesn't seem to be escaped, but it seems that it just can't be displayed as bold, because it's inside an HTML label. I don't know how I could get rid of the "< b >" and "< /b >" in that label, though.
Variant 2:
<p:selectOneMenu id="menu" value="#{selected}" var="myClass">
<f:selectItems value="#{myClasses}"/>
<p:column>
<h:outputText value="#{myClass.name}" styleClass="bold"/>
</p:column>
</p:selectOneMenu>
public List<MyClass> getMyClasses() {
List<MyClass> list = new ArrayList<MyClass>();
...
return list;
}
With this variant I can also have bold labels for items (currently all of them) in the menu. The problem is: The text showing the current selection doesn't show "myClass.getName()", but instead "myClass.toString(). The labels in the menu are correct. Is there a way to fix this?
If I write...
<f:selectItems value="#{myClasses}" var="myVar" itemValue="#{myVar.value}" itemLabel="#{myVar.name}"/>
instead, then it doesn't display the items in the menu as bold anymore, but it's using getName() instead of toString() in the field of the current selection.
Does anyone know how to get my ideas to work the way I want them to work or maybe have a better idea on how to solve this?
Thanks in advance!
In Variant 2 you need to provide converter for MyClass by implementing javax.faces.convert.Converter. Converter needs only for selection (it generates string representation of the object (unique id of instance) and converts it back to object after processing). And for correct representation of selected value you need add this attributes to selectItems: <f:selectItems value="#{myClasses}" var="myVar" itemValue="#{myVar}" itemLabel="#{myVar.name}"/>.
<h:outputText styleClass="bold"... will effect only for list of selectItems. To change style of selected item you need to add styleClass to p:selectOneMenu. In your case:
<p:selectOneMenu id="menu" value="#{view.selection}" converter="#{view.myClassConverter}"
var="v" style="#{(view.selection.name eq 'bold-name') ? 'font-style: bold' : null}">
<f:selectItems value="#{view.myClasses}" var="mc"
itemLabel="#{mc.name}" itemValue="#{mc}"/>
<p:column>
<h:outputText value="#{v.name}" style="#{(v.name eq 'bold-name') ? 'font-style: bold' : null}"/>
</p:column>
<p:ajax event="change" update="menu" process="#this"/>
.

How to bold specific Strings in an h:column

I have a JSF data table that is displaying data based off a search successfully. However, I'm not sure how to selectively bold certain text data in a particular column.
So, for instance, I would like this text...
Here is some text that would be inside the h:column
to show up like this on the page...
Here is some text that would be inside the h:column
Here's what my data table looks like
Results:
<h:dataTable var="results"
value="#{logSearcherBean.results}"
border="1">
<h:column>#{results.logName}</h:column>
<h:column>#{results.matchLine}</h:column>
</h:dataTable>
You could either homebrew an EL function which manipulates the column value and returns the desired HTML,
<h:outputText value="#{my:highlight(results.logName, logSearcherBean.query)}" escape="false" />
(note that this is due to escape="false", which is mandatory to present HTML literally, also sensitive to XSS attacks if the logName is a value which is fully controlled by the enduser)
Or grab JavaScript/jQuery which manipulates the returned HTML, see also this related question: Highlight a word with jQuery.
Hi all,
<p:column id="lastName"
headerText="Last Name">
<h:outputText value="#{person.lastName}" style="#{myBean.getStyle(person.lastName)}"/>
</p:column>
And in the bean:
public String getStyle(String str) {
return str.equals(keyword) ? "background-color: yellow" : "";
}
All best and happy coding!

JSF datatable hide column border if column is not displayed(rendered)

I have a JSF data table
<h:dataTable id="memberTable" value="#{bean.pList}" border="0"
rowClasses="rowEven rowOdd" var="item">
<h:column rendered="#{item.isDisplay == Y}">
<h:outputText value="#{item.visitDate}" >
</h:outputText>
</h:column>
</datatable>
I have more such columns that are rendred based on a condition, when the condition is false and if I have the border=1; I see that blank cells are displayed. How can i display only the columns i want and have other ie. rendered=false not display blank cells?
Thanks,
Sai.
You can't render an entire column based on the condition of a particular row. This makes technically no sense. You need to evaluate the condition based on a property of the parent bean.
<h:dataTable value="#{bean.pList}" var="item">
<h:column rendered="#{bean.display == 'Y'}">
<h:outputText value="#{item.visitDate}" />
</h:column>
</datatable>
Move the public String getDisplay() method to the class behind #{bean}.
Note that I also fixed some other mistakes in your EL expression. You shouldn't prefix the property name with is or get and you should quote string values. You can also better make it a boolean property.
private boolean display;
public boolean isDisplay() {
return display;
}
with
rendered="#{bean.display}"

JSF: h:outputText; how to show a dash when the value is empty string?

I'm using h:outputText tags to display readonly data. Ex:
<h:outputText value="Phone Number:" />
<h:outputText value="#{userHandler.user.phoneNumber}" />
When "phoneNumber" is an empty string or a null, I want to display a dash "-" as the value.
Is there any easy way to do this maybe with expression language or something?
BTW, I thought about adding methods to the User class like getPhoneNumberDisplayText() that could do the check internally, but I since it's a view issue, I'd rather keep the code in the JSF page.
<h:outputText value="#{userHandler.user.phoneNumber != null
? userHandler.user.phoneNumber : '-'}" />
Or, you could make a new outputText:
<h:outputText rendered="#{userHandler.user.phoneNumber == null}" value="-" />

jsf primefaces datatable filtering issues

I am using primefaces and its datatable. A few columns are dates or currencies. If I try to filter those, there are awkward behaviours. When I start typing the filter works until the first delimiter (dot for date for example, so it only filters for 11. the next character let the table to display no entry).
Is it possible to apply a dateconverter?
Here is my code for now:
<p:column filterBy="#{cou.startDate}"
headerText="#{text['date']}"
filterMatchMode="contains"
sortBy="#{cou.startDate}" >
<h:outputText value="#{cou.startDate}" >
<f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</p:column>
Instead of directly using the cou.startDate from the model, you can instead do the following:
Create a new transient property in the model class.
#Transient
private String dateForFilter;
public String getDateForFilter() {
return dateForFilter;
}
public void setDateForFilter(String dateForFilter) {
this.dateForFilter = dateForFilter;
}
Create the logic below before returning the data model.
public List<Item> getDataModel() {
List<Item> lstItem = serviceClass.loadItem(userid);
for (Item item : lstItem) {
DateFormat dateFormat = null;
Date date = item.getDate;
dateFormat = new SimpleDateFormat("MM/dd/yyyy kk:mm");
item.setDateForFilter(dateFormat.format(date));
}
return lstItem;
}
Update your XHTML to use the dateForFilter property.
<p:column filterBy="#{item.dateForFilter}">
<f:facet name="header">
Transaction Date
</f:facet>
<h:outputText value="#{item.dateForFilter}" />
</p:column>
Note: You can only use this if you're not using the date to update the content of the model class.
HTH.
As far as I know, you can't use a converter for the filter value. You can, however, deal with that in your bean/service/dao logic.
You could hardcode your logic and use SimpleDateFormat to parse the value if the filter column matches certain name like startDate or endDate. A more generic approach would be to use reflection to get the Class associated with the column and use SimpleDateFormat if it's Date, DecimalFormat if it's a number and so on.
Naturally if you're propagating that query to a database, you won't be able to use the like operator. If you're using a number you'll need to compare for equality (same applies to dates). If you're looking for stuff that's in memory you'll have to change you logic a little bit. But it shouldn't be too bad. If you could post some of your backing bean/service code, I guess I could be a little more helpful ;)
There is no ready-made date filter mechanism in primefaces yet, but there is a possibility to filter by date using a custom filter. You will have to define a header facet for your column and use ajax calls for "manual" filtering, but it does work:
<f:facet name="header">DateRange
<div>
<p:calendar id="from" value="#{bean.from}" styleClass="calendarFilter">
<p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
</p:calendar>
<p:calendar id="to" value="#{bean.to}" styleClass="calendarFilter">
<p:ajax event="dateSelect" listener="#{ctrlr.filterDates()}" update="dataTableId"/>
</p:calendar>
</div>
</f:facet>

Resources