• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Primefaces get rows of DataTable programmatically

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a <p:dataTable> which has five columns and around 100 rows. I Need to get These rows programmatically from my view in a bean. My Problem is that I Need to get the rowKeys from my rows to get the Data but I don't know how to get the rowKeys.

Right now I figuered out how to get the DataTable in my bean and how to get the Columns but to get the rows I only see the function getRowData(String rowKey)



Is there a way to get the rowKeys or is there another solution DataTable table = (DataTable) component;
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're trying to do this the hard way. Alarm bells ring for me any time people go doing stuff with the FacesContext.

Your situation deals with 2 Model objects of differing types:

1. A JSF DataModel (View Model) that acts as the source of data for JSF to generate the View.

2. The ORM Entity Object Model that holds the actual data that you wish to work with.

People get confused a lot because many times, the ORM Model can serve directly as a View Model, but not in the case of table displays (or Select lists). The JSF DataModel contains cursor context needed by JSF to properly render the rows of the table being displayed AND to determine what row you selected if you click on an action method (or AJAX object) within the table.

The confusion only gets worse because in JSF 2, you don't have to explicitly construct a DataModel object - you can have the dataTable tag reference an array or collection of rows directly. But that's not as useful as it sounds, since a DataModel is still produced, but it's an anonymous object and there's no way to access it for important information (such as the currentRow and currentIndex methods).


The most common way to display a set of database table rows in a JSF dataTable is to have them returned from the ORM as a List of single row entity objects, then wrap that List in a ListDataModel - either by passing it as the constructor parameter for ListDataModel or using the setWrappedData() method for a previously-constructed ListDataModel. You then expose the ListDataModel as a named property of a JSF backing bean. IMPORTANT A backing bean for a JSF DataModel or SelectItem must not be Request-scoped. You need at least the View Scope or Session Scope level.

If you do all that and reference the DataModel propery in your dataTable's "value" attribute, then the table will automatically display whatever row values it wraps, using the column definitions you define on the View Template (xhtml). If application logic changes the contents of that list, then the next dataTable View Refresh will update to show the changed values.

On the other side, putting a commandButton or commandLink (or AJAX equivalent) on a row of the displayed dataTable View Template will ensure that if the user activates that command, its action method will be able to determine what row was selected when the user clicked. The action method can simply use the DataModel object's getRowData() method and be handed the entire row that was clicked on (this is not a copy, it's the actual row from the ORM Entity List object). That's useful, since if you need the row's key, it's usually a column (property) of the row object. Or, if you prefer, the getRowIndex() method returns the 0-based List index value of the selected row.

Note that at no time in the above process do you ever need to code anything referencing the FacesContext. The only JSF-specific part of it is the instance of javax.faces.DataModel that wraps the collection of POJO row objects.


Note that this technique also works for non-ORM data collections, such as manually-constructed tables or tables constructed from JDBC ResultSets. There is, in fact a JdbcDataModel class, although I've never used it.
 
Rancher
Posts: 383
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I came across this particular forum while looking for the answer to another issue I was running into, but after reading your reply Tim I had to comment. I struggled with this same issue myself some time ago, and although it was resolved, I have saved this information for future reference.

I found your reply to be one of the easiest to understand, and most concise, than many articles and books I have read on this topic. Unfortunately I can't give out cows, but I can at least commend you on your excellent (and helpful) reply. Cheers.

P.S. - My apologies to Arman for stepping in on his question.
 
reply
    Bookmark Topic Watch Topic
  • New Topic