On Jan 23, 2008 10:35 PM, Beyonder Unknown <[EMAIL PROTECTED]> wrote:
> The use case is that, I have a page that renders a listview, and I need to 
> add a link that sorts the list when clicked. My listView uses 
> LoadableDetachableModel that retrieves data using hibernate backend.

In my current app, I have a ListView with three different ways of
sorting, each using several fields (columns of the ListView).  The
user can choose the ordering from a DropDownChoice.  I implemented it
with databinder's HibernateListModel.

The sorting is encapsulated in ICriteriaBuilders, with one abstract
one containing the query itself, which is the always the same.

        private abstract class FicheSort implements ICriteriaBuilder {
                HibernateListModel list = new HibernateListModel(Fiche.class, 
this);
                public void build(Criteria criteria) {
                        criteria.add( fichesForCurrentLemma() );   // the query 
itself
                        // ...
                }
                protected abstract String getDisplayName();
        }
        private class SortRenderer implements IChoiceRenderer {
                public Object getDisplayValue(Object object) {
                        return ((FicheSort)object).getDisplayName();
                }
                public String getIdValue(Object object, int index) {
                        return String.valueOf(index);
                }
        }
        private class AlphabeticalSort extends FicheSort {
                @Override
                public void build(Criteria criteria) {
                        super.build(criteria);
                        criteria.addOrder( Order.asc("entry") );
                        criteria.addOrder( Order.asc("siglumDate") );
                        criteria.addOrder( Order.asc("siglum") );
                }
                @Override
                protected String getDisplayName() {
                        return "Alphabetisch";
                }
        }
       // Two more like AlphabeticalSort
       // ...

With one of the sorting methods, I instantiate a databinder
HibernateListModel, which is the model for the ListView:

                final HibernateListModel alphabeticalFicheList =
                                new HibernateListModel(Fiche.class, new 
AlphabeticalSort());

                final FicheListView sortableFicheView = new
FicheListView("sortableFicheView",
                                alphabeticalFicheList);         

Changing the sorting method when the user selects another choice in
the DDC, whose model is a List of all the concrete ICriteriaBuilders,
is simply:

                        protected void onUpdate(AjaxRequestTarget target) {
                                FicheSort sort = (FicheSort) 
orderChoice.getModelObject();
                                sortableFicheView.setModel(sort.list);
                                target.addComponent(sortableFicheViewContainer);
                        }

The downside is that the data is retrieved from the DB each time,
although I'm not sure whether Hibernate or the DB itself recognize
that and cache accordingly.

Cheers,
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to