File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Displaying 2D array using data tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Displaying 2D array using data tables" Watch "Displaying 2D array using data tables" New topic
Author

Displaying 2D array using data tables

Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
Hi,

I'm trying to display the content of 2D array using nested datatables but it didn't work.

here is my code:


can anyone help with it ?!

Thanks,
- Sara
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Welcome to the JavaRanch, Sara!

You specified the variable reference incorrectly in the inner datatable. It should have been like this:


It's always helpful to provide details when you have a problem. Unfortunately, when you're talking computers, there are so many different ways that things "don't work", that we need all the help we can get.!


Customer surveys are for companies who didn't pay proper attention to begin with.
Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
Hi Tim,

Thanks for your reply

I am quite sure that the problem is in the inner datatable because when I view the html source I get an empty table but it has number of rows equal to the size of my array rows. I changed the inner table like you said but it didn't work also.

Do you have any other suggestions?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

You also need the EL markers on your outputText element. (value="#{col}")

But if you're hoping for a table whose columns are tables, that's not how dataTable works. The core JSF dataTable control always presents a fixed number of columns, one column per "column" element. Only the number of rows can vary. To make it work otherwise, you have to add column elements as needed in backing code, binding the control to whatever backing bean method you're using to do that.

It's possible that a dataGrid control might be more useful, but I don't think that's one of the core JSF tags. I think it's a RichFaces element, but other extension tagsets may have something more or less equivalent.
Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
Actually I've tried the EL markers and it didn't work too.

All I wanted to do is to display the content of the 2D array in a table .. I think it can be done using DataModel, but I was trying to do it with simple arrays ... still can't figure out why it's not working


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

I was assuming that "col" was a simple variable, since all outputText can do is render the value of the referenced expression as a single string.

All dataTable "value" properties MUST reference a DataModel, because the DataModel provides the necessary cursor services. Because the DataModel wraps and array or collection, the actual "var" value is the wrapped value, so in the case of a 2D array (or list of lists), you'd have to create a DataModel to wrap a collection of DataModels each one of which would contain a collection of base data items. I think. Now I'm starting to confuse myself.
Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
All dataTable "value" properties MUST reference a DataModel,


Do you mean it's done by the jsf framework or I must do it?
if I must do it, then how?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Sara El-Kady wrote:
All dataTable "value" properties MUST reference a DataModel,


Do you mean it's done by the jsf framework or I must do it?
if I must do it, then how?



All DataModel objects must be created by you. JSF uses them, but does not instantiate them. You do this (usually) in the backing bean that presents the data to the datatable.

DataModel is an interface, so you have to pick a concrete implementation. In your case, that would probably be ListDataModel. It has a constructor that can wrap your actual list data.
Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
ok I understood it, Thanks a lot
Sara El-Kady
Greenhorn

Joined: Jan 11, 2010
Posts: 14
it worked without DataModel

here is how



The inner table had to be in a <h:column> too but the output was each one of the inner table is displayed as a row ... anyway seems that the DataModel is the best way to do it.
Richard Pinaroc
Greenhorn

Joined: Jun 03, 2011
Posts: 7
Hello Sara et all,

I found this solution for the same issue I am having but I cannot get my 2D arraylist to show up properly in a nested datatable. The problem I am having is that the data is being displayed in one column instead of multiple columns. If I output the 2D arraylist to console using plain old java, it shows up properly.

Here is a simple example on what I am doing in my backing bean



Here is what my jsf looks like:



This is the result being displayed:

Item Name
Quantity
Price

Chocolate bar
10
1.50

Chips
5
1.00

Instead of the desired output:

Item Name Quantity Price
Chocolate Bar 10 1.50
Chips 5 1.00

Any ideas or suggestions?

Thanks in advance



[Thumbnail for 2doutput.PNG]

Richard Pinaroc
Greenhorn

Joined: Jun 03, 2011
Posts: 7
figured it out using the following code

<p:dataTable value="#{my2DBean.my2Dlist}" id="tbl" var="row" style="width: 40%">
<p:columns columnIndexVar="index" var="columnNames" value="#my2DBean.myColumnHeadings}">
<f:facet name="header">
<hutputText style="text-align:left" value="#{columnNames}" />
</f:facet>
<hutputText value="#{row[index]}"/>
</p:columns>
</p:dataTable>
Flavio Cardoso
Greenhorn

Joined: Jun 21, 2011
Posts: 3
I came up with this very same solution to a similar problem. Only in my case, I'm getting an exception after I try to submit a form with input data on a h:selectOneMenu, like this:



The exception I get is this:

javax.el.PropertyNotFoundException: ... value="#{myInnerArrayList[colIndex].someProperty Target Unreachable, 'null' returned null

Yes, I do have getters and setters for the someProperty object... Does anyone have a clue on what am I possibly doing wrong?

Am I'm wrapping data in a wrong way? My current wrapping is:



How about yours, Richard Pinaroc?
Flavio Cardoso
Greenhorn

Joined: Jun 21, 2011
Posts: 3
Flavio Cardoso wrote:I came up with this very same solution to a similar problem. Only in my case, I'm getting an exception after I try to submit a form with input data on a h:selectOneMenu, like this:



The exception I get is this:

javax.el.PropertyNotFoundException: ... value="#{myInnerArrayList[colIndex].someProperty Target Unreachable, 'null' returned null

Yes, I do have getters and setters for the someProperty object... Does anyone have a clue on what am I possibly doing wrong?

Am I'm wrapping data in a wrong way? My current wrapping is:



How about yours, Richard Pinaroc?


Correction; I'm actually trying to access items[colIndex] instead of myInnerArrayList[colIndex]:

Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Flavio,

This might not even apply here, but is there a chance that myInnerArrayList[colIndex] is null?
Flavio Cardoso
Greenhorn

Joined: Jun 21, 2011
Posts: 3
Sridhar,

Are you asking about the actual array originally wrapped within my datamodel? If you are, then "no, it's not" would be the answer. To make sure of that, I've had my datatable doing output of items[colIndex].someOtherInitializedProperty, instead of input and the table came out fine with the expected data. If you are talking about the myInnerArrayList as a datatable var in the other hand, again, it's supposed to be items[colIndex], which is definitely not null, at least at rendertime I guess :-s. I could post this test results' screenshot if necessary...

gourav garg
Greenhorn

Joined: Mar 01, 2012
Posts: 1
Please check the following attached file .

It helps to resolve your problem

Second thing is it helps to display the data from result to datatable without
knowing number of columns and name of the columns . Even I used text alignment
for numeric column.

Please change text file to java file.

If any query please call any time
+919781379000
+919478679200






 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying 2D array using data tables