• 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

To get the row number of the selected primefaces datatable row

 
Ranch Hand
Posts: 40
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a primefaces datatable i need to display (selected row number) of (total number of rows) in the JSF page.I could get the row numbers displayed in one of the columns using rowIndexVar attribute but i am not getting any idea to display the same numbers separately in the input text on row select.

What should i need to do in JSF page or managed bean to get selected row number.

Please help me in this regard.

Below is my JSF page

 
Saloon Keeper
Posts: 27762
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
This is a classic example of why you usually do not want to feed a raw collection as a model to a dataTable.

If you wrap your list in a ListDataModel object, expose that object as a backing bean property references as the "value=" of the dataTable (instead of IpInfoList), the selected row number can easily be obtained in the action method by invoking the ListDataModel's "getCurrentRow()" method.

 
Raghu Sundar
Ranch Hand
Posts: 40
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for the reply.IpInfoList is indeed wrapped by ListDataModel,i have given bad naming convention.Can you please guide me where i need to invoke getCurrentRow method to get the row number of selected record,since i couldnot find that.
 
Tim Holloway
Saloon Keeper
Posts: 27762
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
You can't find it probably because (once again ) I tried to give the name from memory and failed.

The actual DataModel method name is "getRowIndex()", which will return the (zero-origin) index of the dataModel row that was selected.

To get the actual dataModel row, use the "getRowData()" method. I usually use this, since I don't care as much about the display row number as I do about the database key that's in the row I selected.
 
Raghu Sundar
Ranch Hand
Posts: 40
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for your reply.I tried using getRowIndex() by catching both check box and row selection events but each time i am getting -1 as the index.With getRowData i am getting exception "javax.faces.model.NoRowAvailableException at javax.faces.model.ListDataModel.getRowData(ListDataModel.java:150)" .Below is my code snippet .

Ajax events:

in managed bean

When i try to retrieve lpInfoList.getRowIndex() any where in the bean it always gives 0.lpinfoList is the DataModel object wrapped by ListDataModel.
Please suggest how to get this functionality.
 
Tim Holloway
Saloon Keeper
Posts: 27762
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 prefer to use the POJO (no-argument) form of the AJAX listener method myself. Since I'm not attempting to share the listener between multiple components, I aready know what DataModel instance I want to query.

Note that I said "DataModel" and NOT "DataTable". That's because the requisite information is in the model, not the UI component.

You may also experience problems if your backing bean is in Request scope. You must be in View scope or higher for DataModels to work properly. Very few things work properly in Request scope under JSF. I hardly ever use it.
 
Raghu Sundar
Ranch Hand
Posts: 40
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using managed bean which is of Request scoped,the reason why i used this scope was,i was getting java.io.NotSerializableException: com.microsoft.sqlserver.jdbc.SQLServerConnection.Now when i change to Viewscoped by declaring connection variable as transient (i have declared Datamodel object lpInfoList as also transient since i was getting NotSerializableException for DataModel object after i made connection variable as transient). Now for any selection operation i am getting exception as DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled or you need to define rowKey attribute and the row selection and check box selection events are not getting recognized.

Is it because i am putting all JDBC code inside managed bean,the viewscoped is not working?What should i need to do now in order to make this work?
 
Tim Holloway
Saloon Keeper
Posts: 27762
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
Session-scoped objects are required by the J2EE standard to implement java.io.Serializable. That is because a server may persist them to long-term storage if it goes dormant or needs extra RAM and because serialization is required if a session is transferred to a different host in a cluster. View Scope is not an actual J2EE scope, just a self-deleting version of Session scope, so the same constraints apply.

A dirty little secret. I make my session/view scope beans serializable, but since some of the things that get injected into them are not serializable (transient), they're "serializable" in name only. If the bean actually is serialized and then restored those properties end up as null. I've been hoping that someone in the standards group will wise up and start providing mechanisms that alert a listener to when a bean of this nature is being restored so that it can reconstruct itself properly. It was a feature in the original EJBs, I see no reason why it should not also be available for vanilla objects.

I'm not sure why you're being told you need a RichFaces DataModel. A standard javax.faces.DataModel should be all that the action routines need. Although if you are supporting a multi-row selection, there are some additional concerns that you have to address, since there's no single selected row/row index to return - there's a collection of them.
 
Tim Holloway
Saloon Keeper
Posts: 27762
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
Oops. Forgot.

No, there's no problem in using JDBC in a JSF backing/managed bean. I usually prefer to abstract the persistence logic into a separate non-JSF bean (such as a DAO) that I inject into the JSF bean, but it's not required.
reply
    Bookmark Topic Watch Topic
  • New Topic