users@woodstock.java.net

Re: TableColumn sorting

From: Dan Labrecque <Dan.Labrecque_at_Sun.COM>
Date: Tue, 13 May 2008 21:11:59 -0400

Jscud wrote:
> Dan Labrecque wrote:
>
>> The JSP Standard Tag Library (JSTL) |length| tag might be useful, but
>> there are interoperability issues with JSTL and JavaServer Faces, so use
>> with caution. The JSF property resolver may not be able to evaluate the
>> EL expression after the JSP page has been parsed.
>>
>>
>
> Thank you for this information.
> then, how could i sort my tablecolumn on list size (too bad i cant use
> #{myList.size} in EL btw) ?
>

How would the JSF property resolver know what "myList" is? If "size"
were a getter method in a mapped backing bean, named "myList", you could
return an appropriate value.

Technically, sorting does not have to be performed on the DataProvider.
In fact, the Woodstock example app demonstrates a table that sorts on
checkbox values stored in a HashMap (i.e., using
TableSelectPhaseListener as a helper). The key is knowing the current
row of the table when your getter method is called. For example, you
could borrow the following code:

    // Get current table row.
    //
    // Note: To obtain a RowKey for the current table row, the use the same
    // sourceVar property given to the TableRowGroup component. For
example, if
    // sourceVar="name", use "#{name.tableRow}" as the expression string.
    private RowKey getTableRow() {
        FacesContext context = FacesContext.getCurrentInstance();
        ValueExpression ve = JSFUtilities.createValueExpression(context,
            "#{name.tableRow}", Object.class);
        return (RowKey) ve.getValue(context.getELContext());
    }

You can find this same code by clicking the "table/util/Select.java"
source link for the table example below.

    http://webdev2.sun.com/example/faces/index.jsp

In the example, the selected attribute of each checkbox tag uses an EL
expression which ultimately points to these getter and setter methods.
Ultimately, these methods get/set checkbox values in a HashMap (i.e.,
the TableSelectPhaseListener).

    // Get selected property.
    public Object getSelected() {
        // tspl is a variable for TableSelectPhaseListener
        return tspl.getSelected(getTableRow());
    }

    // Set selected property.
    public void setSelected(Object object) {
        RowKey rowKey = getTableRow();
        if (rowKey != null) {
            tspl.setSelected(rowKey, object);
        }
    }

Note that the sort attribute of the tableColumn tag also points to this
code as well -- using another EL expression. When the user applies a
sort, the table sorts checkboxes based on the selected state, but you
could easily change this approach and return the length of your lists.

Dan