0

Listboxes fed by list models apparently do not behave as expected if automatic sorting is used

asked 2009-02-23 14:27:31 +0800

cvarona gravatar image cvarona
554 1 6

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) {...

and it's not listitems that the comparator is provided with, but the list model inner _list instead.

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

delete flag offensive retag edit

7 Replies

Sort by » oldest newest

answered 2009-02-25 02:13:40 +0800

PeterKuo gravatar image PeterKuo
481 2

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?

link publish delete flag offensive edit

answered 2009-02-25 02:31:53 +0800

PeterKuo gravatar image PeterKuo
481 2

Look at your code the second time,
I found your code generate random label each time when render.
And that's why you can't see the sorted result.

link publish delete flag offensive edit

answered 2009-02-25 09:13:43 +0800

cvarona gravatar image cvarona
554 1 6

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>
which ensures that every value is associated to the same random number every time.

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

link publish delete flag offensive edit

answered 2009-02-25 10:13:25 +0800

PeterKuo gravatar image PeterKuo
481 2

Yes, I think you are right.
Please post it to feature request.

Thank you.

link publish delete flag offensive edit

answered 2009-02-25 10:19:49 +0800

cvarona gravatar image cvarona
554 1 6

Posted!

With kind regards

César Varona

link publish delete flag offensive edit

answered 2009-02-26 01:01:50 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

See this smalltalk

http://docs.zkoss.org/wiki/Multiple_Field_Sorting_on_Listbox

link publish delete flag offensive edit

answered 2009-02-26 09:19:16 +0800

cvarona gravatar image cvarona
554 1 6

The zk3.5.3 sorting capabilities look nice; unfortunately enough our data model is not comprised of beans, but I guess it will do for most people.

With kind regards

César Varona

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-02-23 14:27:31 +0800

Seen: 342 times

Last updated: Feb 26 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More