Just to show how easy it can be with beanutils:

display_categories.do points to a struts action that preloads like:

*****
        List list = new ArrayList();
        list.addAll( DummyDao.getAllCategories() );
        String sortBy = request.getParameter("sortBy");
        if(sortBy != null) {
            BeanComparator comparator = new BeanComparator(sortBy);
            Collections.sort( list, comparator );
        }
        request.setAttribute( "categories", list );
        return mapping.findForward("success");
*****

and the jsp page that success points to contains:

****
  <table>
   <tr>
    <th><a href="?sortBy=name">name</a></th>
    <th><a href="?sortBy=description">description</a></th>
   </tr>
   <c:forEach var="category" items="${requestScope.categories}">
    <tr>
      <td><a href="load_todos_for_category.do?categoryId=${category.id}">
        <c:out value="${category.name}"/>
      </a></td>
      <td><c:out value="${category.description}"/></td>
    </tr>
   </c:forEach>
  </table>
****

where Category has 'name' and 'description' as properties. This handles
things with natural sorting. Doing the reverse sort used to be pretty
easy, but is harder now that we separate the view and model [as the model
wants to record view-state, or maybe the view wants to record model-view
state...ugh].

The trick is by having:

    if(reverse) {
        comparator = new ReverseComparator(comparator);
    }

after the BeanComparator line. The only complication is where you decide
to store reverse. You can use the session, or as you'll probably want
little arrows showing the sort direction, you can write it into the
column-header with '?sortBy=name&reverse=true'. This can be tricky and
probably involves the Action storing things in the request scope for the
jsp to discover later.

Anyway, I assume the display-tag library contains this pretty much, but
Comparators are lovely so it can't hurt to babble on about them :)

Hen

On Thu, 4 Mar 2004, as as wrote:

> Thanks in advance.
> Sam
> (I am currently truying out display tag though, as per Ashish)
>
> Henri Yandell <[EMAIL PROTECTED]> wrote:
>
> I imagine display tags does it for you mostly if you go that way, else
> I'll knock up an example tonight. Finally getting back into taglibs and
> jsp :)
>
> Hen
>
> On Thu, 4 Mar 2004, as as wrote:
>
> > thanks much.
> > sample code by any cance?
> > Thank you!
> >
> > ~~Sam
> >
> > Henri Yandell wrote:
> >
> > BeanUtils' BeanComparator is a useful way to do the sorting by the way,
> > and will be in your classpath if you're using Struts [depending on
> > version].
> >
> > Also ReverseComparator in Commons Collections. Between them you can write
> > a nice abstract table sorting system.
> >
> > Hen
> >
> > On Thu, 4 Mar 2004, as as wrote:
> >
> > > Wendy,
> > >
> > > Thanks for the quick reply.
> > > books is a session attribute (request.setParameter("books",books)
> > > I retrieve books from database - Book.java
> > > so books is a "List" object.
> > > Will try your approach and post back the results
> > > Thanks,
> > > Sam~~
> > >
> > >
> > > Wendy Smoak wrote:
> > > Sam wrote:
> > > > Any clues on best easy ways to do table column sorting
> > > > my rows in the table are generated as follows using
> > > > as below.
> > >
> > > I do it on the server side, holding the items to be sorted in a Map and
> > > creating a new Map passing a Comparator to the constructor to get the
> > > new order. Then when you're back on the JSP iterating over it, the
> > > items are in the "correct" order.
> > >
> > > Map accountMap = (Map) session.getAttribute( "accountMap" );
> > > Map newMap = new TreeMap( new CostCenterComparator() );
> > > newMap.putAll( accountMap );
> > > session.setAttribute( "accountMap", newMap );
> > >
> > > [Comments welcome, I'm not sure this is the best way to do it, but it
> > > works...]
> > >
> > > What type of Collection is 'books' in your example?
> > >
> > > --
> > > Wendy Smoak
> > > Application Systems Analyst, Sr.
> > > ASU IA Information Resources Management
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > >
> > > ---------------------------------
> > > Do you Yahoo!?
> > > Yahoo! Search - Find what you’re looking for faster.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> > ---------------------------------
> > Do you Yahoo!?
> > Yahoo! Search - Find what you’re looking for faster.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Search - Find what you’re looking for faster.


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

Reply via email to