-
FEATURED COMPONENTS
First time here? Check out the FAQ!
The development guide says
List boxes support sorting of list items directly. There are a few ways to enable the sorting of a particular column. The simplest way is to set the sort property of the list header to auto as follows. Then, the column that the list header is associated with is sortable based on the label of each list cell of the specified column.
This is absolutely true of directly populated listboxes, but it fails to describe the reality when it comes to describe the behaviour of 'live' lists.
<zk> <zscript> List data = new ArrayList(); for(int j=0; j < 20; ++j) { data.add( new Integer( j ) ); } ListModel strset = new SimpleListModel(data); renderer = new ListitemRenderer() { public void render( Listitem li, Object value ) { li.setValue( value ); new Listcell( "" + value ).setParent( li ); new Listcell( "" + Math.random() ).setParent( li ); } }; </zscript> <listbox id="list" width="300px" rows="20" model="${strset}" itemRenderer='${renderer}'> <listhead> <listheader width='150px' label="Same as model" sort="auto"/> <listheader width='150px' label="Random" sort="auto"/> </listhead> </listbox> </zk>
When trying up this zul it becomes clear that it's only the first column that gets ordered correctly. This is so because the default listitem comparator reads like
public int compare(Object o1, Object o2) { final int index = _index < 0 && _header != null ? _header.getColumnIndex(): _index; Object v1, v2; if (o1 instanceof Listitem) {...
Whether this is done this way by mistake or on purpose it would be nice if the default automatic sorting behaviour was available on live lists as well. Is there any way to "force" my listmodel-fed list to behave like a simple one when it comes to sorting?
With kind regards
César Varona
But I trace the code, in ListitemComparator.java,
public int compare(Object o1, Object o2) {
...
if (o1 instanceof Listitem) { //not live data
...
}
else { //live data
...
}
}
Maybe it's already handled by fresh code?
Of course it does; I don't think it invalidates the test, for I guess the items get just ordered, not re-rendered, but just try
<zk> <zscript><![CDATA[ Map mappings = new HashMap(); List data = new ArrayList(); for(int j=0; j < 20; ++j) { data.add( new Integer( j ) ); mappings.put( data.get( j ), Math.random() ); } ListModel strset = new SimpleListModel(data); renderer = new ListitemRenderer() { public void render( Listitem li, Object value ) { li.setValue( value ); new Listcell( "" + value ).setParent( li ); new Listcell( "" + mappings.get( value ) ).setParent( li ); } }; ]]></zscript> <listbox id="list" width="300px" rows="20" model="${strset}" itemRenderer='${renderer}'> <listhead> <listheader width='150px' label="Same as model" sort="auto"/> <listheader width='150px' label="Random" sort="auto"/> </listhead> </listbox> </zk>
The point is: when resorting to automatic sorting live lists get sorted on a listitem value basis, no matter which label appears in the listcells of the "sort by" column. In order to sort by listcell label you cannot use 'auto', you have to provide some comparator with the ability to tell which label matches a given listitem value for a certain column and use it to perform the comparison. However I think it would be very useful to have the ability of letting "automatic" (i.e., on a listecell basis) sorting also work on live lists.
With kind regards
César Varona
Asked: 2009-02-23 14:27:31 +0800
Seen: 342 times
Last updated: Feb 26 '09