TableColumn sorting on list size

View: New views
4 Messages — Rating Filter:   Alert me  

TableColumn sorting on list size

by Jscud :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

i'm trying to sort a tablecolumn which displays the number of elements of a java.util.List
I've done this (using Netbeans VWP 6.1 and glassfish 2ur2 btw):

<jsp:root version="2.1" ...xmlns:fn="http://java.sun.com/jsp/jstl/functions"> //Added this part to the VWP generated stuff

...
<webuijsf:tableRowGroup id="tableRowGroup1" rows="20"
                                sourceData="#{IlesControleur.iles}" sourceVar="currentRow">
                               
                                <webuijsf:tableColumn headerText="Communes" id="tableColumn7" sort="#{fn:length(currentRow.value['communes'])}" descending="true">
                                    <webuijsf:staticText id="staticText1" text="#{fn:length(currentRow.value['communes'])}"/>
                                </webuijsf:tableColumn>


#{IlesControleur.iles} returns a List of POJOs Ile which contains the property communes as a List too.
When I sort the column the first time, all is going well (getting the communes size descending), but the second time i sort I got an exception:
javax.el.ELException: Function 'fn:length' not found
        at com.sun.el.lang.ExpressionBuilder.visit(ExpressionBuilder.java:213)
        at com.sun.el.parser.SimpleNode.accept(SimpleNode.java:162)
        at com.sun.el.lang.ExpressionBuilder.prepare(ExpressionBuilder.java:178)
        at com.sun.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:189)
        at com.sun.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:232)
        at com.sun.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:92)
        at com.sun.webui.jsf.faces.ValueBindingSortCriteria.readObject(ValueBindingSortCriteria.java:220)

If it says it doesnt find the 'fn:length' jstl function, it shouldn't work at all.
Any idea on this?
Thanks in advance


Re: TableColumn sorting

by Dan Labrecque :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Dan

Jscud wrote:
Hi,

i'm trying to sort a tablecolumn which displays the number of elements of a
java.util.List
I've done this (using Netbeans VWP 6.1 and glassfish 2ur2 btw):

<jsp:root version="2.1"
...xmlns:fn="http://java.sun.com/jsp/jstl/functions"> //Added this part to
the VWP generated stuff 

...
<webuijsf:tableRowGroup id="tableRowGroup1" rows="20"
                                sourceData="#{IlesControleur.iles}"
sourceVar="currentRow">
                                
                                <webuijsf:tableColumn headerText="Communes"
id="tableColumn7" sort="#{fn:length(currentRow.value['communes'])}"
descending="true">
                                    <webuijsf:staticText id="staticText1"
text="#{fn:length(currentRow.value['communes'])}"/>
                                </webuijsf:tableColumn>


#{IlesControleur.iles} returns a List of POJOs Ile which contains the
property communes as a List too.
When I sort the column the first time, all is going well (getting the
communes size descending), but the second time i sort I got an exception: 
javax.el.ELException: Function 'fn:length' not found
        at
com.sun.el.lang.ExpressionBuilder.visit(ExpressionBuilder.java:213)
        at com.sun.el.parser.SimpleNode.accept(SimpleNode.java:162)
        at
com.sun.el.lang.ExpressionBuilder.prepare(ExpressionBuilder.java:178)
        at
com.sun.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:189)
        at
com.sun.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:232)
        at
com.sun.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:92)
        at
com.sun.webui.jsf.faces.ValueBindingSortCriteria.readObject(ValueBindingSortCriteria.java:220)

If it says it doesnt find the 'fn:length' jstl function, it shouldn't work
at all.
Any idea on this?
Thanks in advance


  


Re: TableColumn sorting

by Jscud :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.
Hi and thank you for this information, didnt know it was a bad idea to use JSTL with JSF (I'm rather new to the JSF crowd)
Then how would i sort a tablecolumn with a list size (too bad i cant use #{myList.size} in EL btw) ?
Regards
Yves.

Parent Message unknown Re: TableColumn sorting

by Dan Labrecque :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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