Controlling the formatting of individual cells in dataTable

Hello,

I am looking for a way to control the formatting (for example color) of individual cells in dataTable. The current formatting option allow me to change only the formatting on a column or row basis.

Is there a way to do that with dataTable? Maybe within the servlet ?

If not, any other recommended control that display a data table ?

Thanks

Amir

[388 byte] By [AmirSa] at [2007-11-26 17:12:29]
# 1

There are several ways. Here are some examples:<h:dataTable value="#{myBean.list}" var="item">

<h:column>

<h:outputText value="#{item.value1}" styleClass="#{item.styleClass}" />

</h:column>

<h:column>

<h:outputText value="#{item.value2}" styleClass="#{item.stringValue == 'someValue' ? 'someClass' : 'anotherClass'}" />

</h:column>

<h:column>

<h:outputText value="#{item.value3}" styleClass="#{item.booleanValue ? 'someClass' : 'anotherClass'}" />

</h:column>

<h:column>

<h:outputText value="#{item.value4}" styleClass="#{item.intValue > 10 ? 'someClass' : 'anotherClass'}" />

</h:column>

...

</h:dataTable>

BalusCa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Many thanks for the answerIs there a way to do that from Java (server code) and not the Facelet/JSP (client code )
AmirSa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Yes. Just bind the component to the backing bean.

In example of a h:outputText:

JSF<h:outputText binding="#{myBean.outputText}" />

MyBeanprivate HtmlOutputText outputText; // + getter + setter

public void doSomething() {

if (booleanValue) {

outputText.setStyleClass("someClass");

} else {

outputText.setStyleClass("anotherClass");

}

}

BalusCa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks, but

From what I understand both ways (via JSP or Bean ) is controlling the whole column. If I have a table with 10 rows all the rows of a specific column will have the same style. What if I want to control individual cell - for example, in the same column, row number 2 will have style A and row number 4 will have style B?

Amir

AmirSa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You can do this the same way like I posted in 1st reply.

<h:outputText value="#{item.value1}" styleClass="#{item.styleClass}" />

Or even:

<h:outputText value="#{item.value1}" styleClass="#{myBean.itemStyleClass}" />

public String getItemStyleClass() {

Object row = htmlDataTable.getRowData();

// base on the row data which styleclass should be returned

return "someStyle";

}

BalusCa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

BalusC,

Can we do the same for each ROW of a datatable. I'm trying to highlight a row that has some incomplete information. can we do this?

Right now, i am just adding an "*" to the beginning of the row, but it would be great if we can set the background color of a particular row to something else.

Seshu_Varanasia at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Seshu_Varanasi

To change the style of a specific row you can use the rowClasses property of the dataTable. You need to define the styles of the different row

BalusC

I cant see how your solution solve my problem. For example, How do you set the cell at row number 10 column number 12 to be with red background ? According to your solution all the rows will have the same style! you define the style once for the columns !

Amir

AmirSa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Hey Amir,Isn't the rowClasses property for alternating rows?Also, how do we enable the specific style only for that row?
Seshu_Varanasia at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Hiן

In your JSP you write

rowClasses="#{BeanOne.rowStyles}"

and then in your bean (BeanOne) you set the variable rowStyles to have the right amount of styles with the given row with the right style, let say you have 4 rows and the third is highlighted

rowStyles="regStyle, regStyle, highlightedStyle, regStyle"

you need to define the styles regStyle, highlightedStyle in you CSS

AmirSa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
i actually did that. but i put the actual style in it, instead of the classes.but now i put the classes. and each row now has its own class that i saw from the view source of the jsp. but the color is not showing up.
Seshu_Varanasia at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

> I cant see how your solution solve my problem. For

> example, How do you set the cell at row number 10

> column number 12 to be with red background ?

> According to your solution all the rows will have the

> same style! you define the style once for the columns

You can define and change "someStyle" yourself based on the row object.

BalusCa at 2007-7-8 23:40:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...