Generating a JSF DataTable

Hello together,if you can see, i'm from germany, so my english is not the best one.I want to generate an datatable with dynamic columns und rows ... I know who to generate the rows but have someone an answer or a code listing who i can generate dynamic columns?!
[284 byte] By [deka-onlinea] at [2007-11-26 13:08:37]
# 1
UIColumn column = new UIColumn();htmlDataTable.getChildren().add(column);Clarifies a lot, I guess? Also check http://balusc.xs4all.nl/srv/dev-jep-dat.html
BalusCa at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

first: thanks for this nice link.

But now I have one more question.

I have a dynamic table with 3 cols and 4 rows (only for tests) and now i want to mark empty cells.

But I have no idea how I can do this!?

public HtmlDataTable getDataTable()

{

// Set headers (optional).

String []headers = new String[] {"header1", "header2", "header3"};

// Set rows. This is a stub example, just do your dynamic thing.

String[] row1 = {"ID1", "Name1", "Value1"};

String[] row2 = {"ID2", "Name2", "Value2"};

String[] row3 = {"ID3", "", "Value3"};

String[] row4 = {"ID4", "Name4", ""};

// Convert rows to List and set the List.

myList = new ArrayList();

myList.add(Arrays.asList(row1));

myList.add(Arrays.asList(row2));

myList.add(Arrays.asList(row3));

myList.add(Arrays.asList(row4));

HtmlDataTable dataTable = new HtmlDataTable();

if (myList != null && myList.size() > 0)

{

int columns = ((List) myList.get(0)).size();

for (int i = 0; i < columns; i++)

{

UIOutput header = new UIOutput();

header.setValue(headers[i]);

HtmlSimpleColumn column = new HtmlSimpleColumn();

column.setHeader(header);

HtmlOutputText output = new HtmlOutputText();

ValueBinding myItem = FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myItem[" + i + "]}");

output.setValueBinding("value", myItem);

//output.setStyle("#{(!empty myItem["+i+"]) ? 'jrbe_content' : 'jrbe_content_empty'}");

output.setStyleClass("jrbe_content_empty"); <<<<<<<HEEEEEREEEE

column.getChildren().add(output);

dataTable.getChildren().add(column);

}

}

return dataTable;

}

do someone know how i can check if the cell is ampty and who i can switch the styleclass dynamic?!

I have 2 classes : jrbe_content and jrbe_content_empty

thanks

Message was edited by:

deka-onlin>

deka-onlina at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Create a value binding with your EL for the styleClass and use setValueBinding( "styleClass", vb ). Just like you did with value.
BrantKnudsona at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

if (myList != null && myList.size() > 0)

{

int columns = ((List) myList.get(0)).size();

for (int i = 0; i < columns; i++)

{

UIOutput header = new UIOutput();

header.setValue(headers[i]);

HtmlSimpleColumn column = new HtmlSimpleColumn();

column.setHeader(header);

HtmlOutputText output = new HtmlOutputText();

ValueBinding myItem = FacesContext.getCurrentInstance().getApplication().createValueBinding("#{myItem[" + i + "]}");

output.setValueBinding("value", myItem);

/*

ValueBinding styleClass = FacesContext.getCurrentInstance().getApplication().createValueBinding("...");

output.setValueBinding("styleClass", styleClass);

*/

column.getChildren().add(output);

dataTable.getChildren().add(column);

}

}

so one more question. I created a new value binding f黵 the styleClass, and set it. It's what u find as comment. Can it work like this? and i know that i forgot the creation of the binding .. I want only to know if it can work or if I forgot something

deka-106a at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You can use setStyleClass for static values.

For EL values, you have to use createValueBinding (or createValueExpression since JSF 1.2), so do:

...createValueBinding("#{(!empty myItem["+i+"]) ? 'jrbe_content' : 'jrbe_content_empty'}");

BalusCa at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

<tr>

<td>

<span class="jrbe_content">ID3</span>

</td>

<td>

<span class="jrbe_content_empty"></span>

</td>

<td>

<span class="jrbe_content">Value3</span>

</td>

</tr>

OK first Thank You it works!!! but i have one new but little question. ok I have a little bit more questions.

1. why build he automatically this <span> tags ?

2. can I switch off that he's creating this ags?

3. I test it with a css-file and an class that sets the width:100px, but he didn't size the span's up to 100px, why?

i hope u know what i mean ... i think i have to learn english^^

deka-106a at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

1) Spans come from h:outputText. They are placed only if you apply some styles.

2) Create your own component. But how else would you apply styles? I don't see what's wrong with spans? Do you want the styleclass in the parent <td> element? If so, then use the columnClasses attribute of the h:dataTable.

3) According to W3C HTML spec the span is an inline element, so add display: block; to the style.

BalusCa at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

ok now i know why he create this spans. And Yes i want the styClass in the Parent element. I don't know if the columnClasses attribute works whit my idea.

I want an table with some cols and rows. and if some cells are empty he should set the background of the cell in another color then the rest.

In the code I postet 2 posts before u see that he did what i want but he set the class "jrbe_content_empty" in the span tag and he should set it on the td tag ... has someone an idea how i can do this.

He should only "delete" the spans and set the classes on the tds!

-

OK I see there are big differences between the IE and the Firefox, if I use the firefox, he doesn't show the empty span tags ... in the IE he marks the empty Cells, and it looks like a <td> with another backgroundcolor then the other

Message was edited by:

deka-106

deka-106a at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

> ok now i know why he create this spans. And Yes i

> want the styClass in the Parent element. I don't know

> if the columnClasses attribute works whit my idea.

>

> I want an table with some cols and rows. and if some

> cells are empty he should set the background of the

> cell in another color then the rest.

>

> In the code I postet 2 posts before u see that he did

> what i want but he set the class "jrbe_content_empty"

> in the span tag and he should set it on the td tag

> ... has someone an idea how i can do this.

>

> He should only "delete" the spans and set the classes

> on the tds!

This is not possible with h:dataTable. You can create your own component or just life with it ;) Or if wou want to do it die-hard, then play somewhat with DOM + JavaScript to detect empty td's and then change it's style.

> OK I see there are big differences between the IE and

> the Firefox, if I use the firefox, he doesn't show

> the empty span tags ... in the IE he marks the empty

> Cells, and it looks like a <td> with another

> backgroundcolor then the other

Like I said, add display: block; to the style as span is just an ordinary inline element.

BalusCa at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

while(it.hasNext())

{

String languageCode = (String) it.next();

header = new UIOutput();

header.setValue(languageCode);

column = new HtmlSimpleColumn();

column.setHeader(header);

StringBuffer bindingString = new StringBuffer("#{jrbeMainPageManager.valueMap['");

bindingString.append(languageCode);

bindingString.append("'][myItem]}");

HtmlInputText input = new HtmlInputText();

myItem = FacesContext.getCurrentInstance().getApplication().createValueBinding(bindingString.toString());

input.setValueBinding("value", myItem);

ValueBinding styleClass = FacesContext.getCurrentInstance().getApplication().createValueBinding("(!empty " + bindingString.toString() + ") ? 'jrbe_content' : 'jrbe_content_empty'}");

input.setValueBinding("styleClass", styleClass);

column.getChildren().add(input);

dataTable.getChildren().add(column);

}

so, everything works ... but now i chanced the output tag into an input tag ... and now he's writing the whole string in the class attribute...

I only chanced the tag and now he's writing the whole VB..

IT's look like this:

<td><input id="showContent:_idJsp0:8:_idJsp0_12" name="showContent:_idJsp0:8:_idJsp0_12" type="text" value="this is a test8" class="(!empty this is a test8) ? 'jrbe_content' : 'jrbe_content_empty'}" /></td>

u can see that the class ist the same like the VB ... have I forgot somethink?

Message was edited by:

deka-106

deka-106a at 2007-7-7 17:20:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...