Tapestry: How to make Grid “add”ed columns sortable

In Tapestry Grids, “add”ed columns are not sortable by default. The following is one way to enable sorting for grid columns that are “add”ed:

  1. In the @Component annnotation of your grid add this parameter:
    “model=model”

  2. Add a method as follows to your class which contains the grid:
    public BeanModel getModel()

  3. Inject the BeanModelSource to your class which contains the grid, as follows:
    @Inject
    private BeanModelSource beanModelSource;
    
  4. In your getModel() method create a BeanModel as follows:
    final BeanModel model =
    beanModelSource.createDisplayModel(YourRowClass.class,
    messages);
    

    Where “YourRowClass” is the class you’ve specified for your row parameter of your Grid component.

  5. In the getModel() method you also need to do your “include”, “add” and “reorder” (in that order). Note: This can’t be done as component parameters because you need to modify the “add” PropertyModels (see next step).
  6. For each of your “add”s create a PropertyModel and call sortable(true). Note that for each of your “add”s you need to include a property conduit to return the appropriate value (by overriding the get(Object instance) method). Example:
    final PropertyModel appointmentModel = model.add("appointment",
        createAppointmentPropertyConduit());
    appointmentModel.sortable(true);
    

Here is some example source code for the full getModel() method:

/**
 * @return model with sortable columns
 */
public BeanModel getModel()
{
    final BeanModel model =
        beanModelSource.createDisplayModel(YourRowClass.class,
            messages);

    model.include("propertyName1", "propertyName2", "propertyName3");

    final PropertyModel pm1 = model.add("propertyName4", 
        createPropertyName4PropertyConduit());
    pm1.sortable(true);

    final PropertyModel pm2 = model.add("propertyName5", 
        createPropertyName5PropertyConduit());
    pm2.sortable(true);

    model.reorder("propertyName1", "propertyName2", 
        "propertyName3", "propertyName4", "propertyName5");

    return model;
}

I’d hazard a guess that this would be a candidate for an improvement in a future tapestry version i.e. an easier/less verbose way to make “add”ed columns sortable.

P.S. If you’re aware of a better way to achieve this feel free to add a comment below.

Vorheriger Beitrag
willhaben.at bedankt sich bei Catalysts mit dem JOB NAVI³ Award
Nächster Beitrag
Adieu, Ottensheimer Straße!

Related Posts

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Bitte füllen Sie dieses Feld aus
Bitte füllen Sie dieses Feld aus
Bitte gib eine gültige E-Mail-Adresse ein.
Sie müssen den Bedingungen zustimmen, um fortzufahren

Menü