I have a richfaces dataTable that is populated by fetchDevices() which runs a sql query. I want to be able to sort the table without re-running the query.
How do I do that? Is that what client-side processing is all about? Doing stuff just on the client? I use a DeviceSorting bean to determine sort order.
Can I use a java bean on the server to calculate sort order without re-running my fetchDevices() query?
First, check to make sure that you have the right RichFaces dataTable. You may need an extendedDataTable in order to get column sorting support. I'd have to RTFM to be certain.
Getting the sort to work properly may require you to be using an explict DataModel object in your backing bean. If you've taken the easy way out and bound the xhtml directly to a collection object, you may not have what you need. Also, of course, DataModels can not be in Request scope, since they store data between requests. So you need View scope or higher.
Finally, except when using a JdbcDataModel, RichFaces would not be re-running the original query unless you told it to explicitly. If you have run the query and stored the results in an ordered collection such as a List or Array, RichFaces should consider that collection as immutable and do its magic transparently using the DataModel that provides the intermediary between your collection and the UI. You might need to add a comparison function, but that's about it.
An IDE is no substitute for an Intelligent Developer.
Joined: Jun 22, 2009
Thanks Tim. After thinking about this a bit I realized I do want to sort the ENTIRE list and not just the part that is displayed.
You are right -- I'm using ArrayList behind the dataTable. I'm not sure how to use DataModel as yet, but I'm going to look into it. I'm not sure I have to when all I want to do is sort. Plain dataTable does do sorting as well. You don't need the extended version. It's got some nifty features, but I'm not sure I want those. For now trying to keep it simple.
Once I've figured out what I need to do I'll post my results. Didn't want you to think I disappeared into the ether like posters sometimes seem to do.
Basically, you need a DataModel whenever you want 2-way communication between Model and View.
As I've said before, I think all current JSF implementations will create an anonymous DataModel if you don't create one yourself, but you cannot easily access that DataModel to see, for example, what row someone clicked in.
The DataModel is a Decorator for the underlying Model so that you can use generic non-JSF lists and tables for your actual models. This is especially useful when the actual model is itself an ORM Object model, and therefore shouldn't be carrying any JSF freight of its own for portability reasons.
You can also introduce yet another model layer if it's useful. Since a List is a set of references to objects, it's perfectly legal (and sometimes very useful) to construct a façade List in front of an object model List (or array) that re-orders the references to the base Model object. A variation on this is to provide a decorator collection, since many databases (and therefore ORM models) don't have native Boolean object support and JSF's checkbox controls won't work with non-boolean properties. You can't even make JSF Converters for them. :pissed:
I think when you said "plain dataTable" you actually meant "plain RichFaces" dataTable, though. A vanilla core JSF dataTable doesn't support sortable column UI support. Only roll-your-own stuff.
Joined: Jun 22, 2009
I really appreciate the info about DataModel. I like to do things "correctly" (ha! whatever that means) and I like transparency.
I prefer to create my own DataModel rather than have one show up.
And yes, I was refering to richfaces regular DataTable rather than richfaces extendedDataTable.
I'm working on a pilot project to bring myself up to speed on JSF2 and the other related hibernate 4 and richfaces 4. I am self-taught and there is SO MUCH I don't know it's really hard sometimes.