Data Table how to perform logical calculation of a column value

Hi All,

I'm new to JSF & trying to build a portlet wherein.I have a requirement where i to display a graphic ( tick or error) into one of the column's of a data table depending upon an attribute of the bean class.

The problem is that with the any other column i have to simply bind the column value to the bean property so that all the bean objects in the list will be iteratively displayed in the column.But in this column how to place the logic that depending on a bean property content of the column is to be set.

Kindly help me with it

[570 byte] By [prat_deva] at [2007-11-27 9:57:52]
# 1

2 ways:

<h:graphicImage value="#{myBean.error ? 'error.gif' : 'tick.gif'}" />

or

<h:graphicImage value="error.gif" rendered ="#{myBean.error}" />

<h:graphicImage value="tick.gif" rendered ="#{!myBean.error}" />

Where the MyBean#isError() returns a boolean value. You can also use boolean expressions in EL, for example

#{myBean.objectValue != null}

#{myBean.stringValue == 'foo'}

#{myBean.intValue > 0}

#{myBean.booleanValue && myBean.stringValue != 'bar'}

#{!empty myBean.collectionValue}

BalusCa at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

BalusC posted the straightforward cases. If you need to get fancier, create a backing bean and use the binding attribute of the dataTable to give the bean access to the UIData object. Then output the result of a method in your binding bean. Within the method call UIData.getRowData() to get the bean for the current row.

RaymondDeCampoa at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Thanks a lot everyone for guiding me with this, this has really helped me to do away a lot of development roadblocks.

I have a further requirement to highlight a particular row i.e. to assign a different CSS to a row is that possible to have in JSF.

To assign a different styling to a particular row of a datatable based on some conditions.

prat_deva at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Haven't you learnt from my former posting? This is straightforward again:<h:someComponent styleClass="#{myBean.someBooleanValue ? 'style1' : 'style2'}" />
BalusCa at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi BalusC,

Thanks a lot for the assistance once again,

well doing a <h:column styleClass="someClass based on EL calculation"> will not solve the purpose as this will assign StyleClass to a particular column & not the entire row,doing it on <h:outputText> will also not solve the purpose fully either, as it will not apply it on the whole of the row.

I have been doing some google & found an article in developerWorks.

http://www.ibm.com/developerworks/rational/library/05/1213_he/index.html

This example uses javascript to highlight the row, while i don't particularly like this way of doing thing.I'm exploring using other options to get reference to the row.

prat_deva at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Oh, this way .. Check http://balusc.xs4all.nl/srv/dev-jep-dat.html for an useful article about using datatables.
BalusCa at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Great reference of JSF dataTable at a single place, i got a lot of tips to enhance my dataTable.

Well as far as i have researched out there seems presently no straightforward way to apply a particular style to a row based on some business logic.

However there do exist some intelligent workarounds like

http://forum.java.sun.com/thread.jspa?threadID=5048900 reply by san-arul please include this as well on your dataTable reference web page.

prat_deva at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

You could use the rowClasses attribute of the dataTable component, in concert with a backing bean. Although depending on the implementation this might be cumbersome with a large table.

<h:dataTable rowClasses="#{backing_bean.rowClasses}"

public String getRowClasses()

{

int selected = getSelectedRowIndex();

int rowCount = getRowCount();

StringBuilder result = new StringBuilder();

int i = 0;

for ( ; i >< selected; i++)

{

result.append(',');

}

result.append('selectedRowStyle');

i++;

for ( ; i < rowCount; i++)

{

result.append(',');

}

return result.toString();

}

RaymondDeCampoa at 2007-7-13 0:28:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...