You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Vangel V. Ajanovski" <aj...@ii.edu.mk> on 2010/02/04 19:06:08 UTC

Re: Sortable computed Grid column

On 26.01.2010 20:00, Michael Prescott wrote:
> How do you go about making a computed column in a Grid sortable?  The docs
> on Grid are great, but in the example the computed column is non-sortable.
> Can I do it by providing a PropertyConduit through the grid's BeanModel
> model?
>
> Michael
>
>   
I have quite a bit of trouble with this. On almost every page we have a
grid which has at least half of the columns computed or with cell values
lazy loaded via foreign keys.

For example in 100% of cases we have source of the grid as
List<Employee>, where Employee is an Entity (via Hibernate).
And in 90% the cells we have something like this
${employee.person.firstName}
${employee.person.lastName}

The backing database model:    Employee (n) : (1) Person

Of course I have to sort based on firstName value, and not based on the
foreign key id value in the main entity.

So, this approach worked for me, but I am not sure that it's the right
way, or that it's a proper way.

    @Inject
    private PropertyConduitSource pcSource;
    @Inject
    private BeanModelSource beanModelSource;
    @Inject
    private Messages messages;
    @Property
    private BeanModel<CourseEnrollment> model;

    void pageLoaded() {
        this.model = beanModelSource.createDisplayModel(Employee.class,
messages);
        this.model.add("firstName",
pcSource.create(Employee.class,"person.firstName")).sortable(true);
        this.model.add("lastName",
pcSource.create(Employee.class,"person.lastName")).sortable(true);
        this.model.add("staffType", null).sortable(false);
        this.model.reorder("firstName, lastName, staffType");
    }

I don't even know that this code is OK, but it worked.

The grid component only references the "model" model.

I am bothered with this solution becase:
- This is too much code for something that should be much simpler, and I
am thinking about making a generic version, but I guess I still haven't
learned enough of Tapestry to succeed in that.
- I have code duplication. The names of the properties that should be
displayed and sorted are written twice, once when creating the
PropertyConduit and the other time is in the template.

On the other hand I am encouraged that even some point-and-click
non-programmer-friendly web frameworks like Oracle Application Express
do not have an obvious solution for such ordinary everyday use-cases.


Re: Sortable computed Grid column

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Thu, 04 Feb 2010 16:06:08 -0200, Vangel V. Ajanovski <aj...@ii.edu.mk>  
wrote:

> For example in 100% of cases we have source of the grid as
> List<Employee>, where Employee is an Entity (via Hibernate).
> And in 90% the cells we have something like this
> ${employee.person.firstName}
> ${employee.person.lastName}

You could, for example, add a @Transient String getFirstName() { return  
getPerson.getFirstName(); } method. Grid and DisplayModel would recognize  
it automatically. Even in non-Tapestry contexts, this can be very handy.

> So, this approach worked for me, but I am not sure that it's the right
> way, or that it's a proper way.
>
>     @Inject
>     private PropertyConduitSource pcSource;
>     @Inject
>     private BeanModelSource beanModelSource;
>     @Inject
>     private Messages messages;
>     @Property
>     private BeanModel<CourseEnrollment> model;
>
>     void pageLoaded() {
>         this.model = beanModelSource.createDisplayModel(Employee.class,
> messages);
>         this.model.add("firstName",
> pcSource.create(Employee.class,"person.firstName")).sortable(true);
>         this.model.add("lastName",
> pcSource.create(Employee.class,"person.lastName")).sortable(true);
>         this.model.add("staffType", null).sortable(false);
>         this.model.reorder("firstName, lastName, staffType");
>     }
>
> I don't even know that this code is OK, but it worked.

IMHO, it's absolutely correct. A textbook example, even. :)

> I am bothered with this solution becase:
> - This is too much code for something that should be much simpler, and I
> am thinking about making a generic version, but I guess I still haven't
> learned enough of Tapestry to succeed in that.

To do what you want, BeanModelSource would need to look for properties  
recursively, and I don't think it would be a good idea. A potentially  
large number of properties would be found, most of then useless.

> - I have code duplication. The names of the properties that should be
> displayed and sorted are written twice, once when creating the
> PropertyConduit and the other time is in the template.

Why again in the template?

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, software architect and developer, Ars Machina Tecnologia da  
Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org