0

Comparator

asked 2008-12-24 15:26:47 +0800

erickmelo gravatar image erickmelo
87 2

Hi guys... I'm having problem with this code:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Categoria" border="normal" apply="${categoriaController}">
<listbox id="list" multiple="false" rows="10" model="@{model.categorias}" selectedItem="@{model.selectedCategoria}">
<listhead>
<listheader label="Codigo" width="50px" sort="auto"/>
<listheader label="Nome" sort="auto"/>
</listhead>
<listitem self="@{each=categoria}">
<listcell label="@{categoria.id}"/>
<listcell label="@{categoria.nome}"/>
</listitem>
</listbox>
</windows>

The databinding is working fine, but I'm having problems with "Sorting"...
SEVERE: >>java.lang.ClassCastException:
at org.zkoss.zul.ListitemComparator.compare(ListitemComparator.java:252)

I implemented the Comparable<Categoria> interface and it worked, but I don't have control, in my POJO about what colum is pressed... So I get order only by name or by id... How does ZK work with sort and databinding? Have I do implement other interface?? I supose if ZK get the values from my POJO, it should use the column type to sort... In my case the field "name" is a String, and String implements the Comparable interface... Shouldn't ZK call the String.compareTo() when I try sorting? I'm not sorting Categoria objects directy, but sorting these objects by its fields....

Please help me to solve this problems!

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2008-12-25 08:52:03 +0800

robertlee gravatar image robertlee
561

Hi erick,
To be able to allow the onSort() method to be called by each Listheader, you will need to implement a controller which extends Composer (GenericForwardComposer) interface. In other words, you have control to which column pressed. For example in my small test:

ListBOx1Composer.java:

public class ListBOx1Composer extends GenericForwardComposer {

        ...other methods...
	
        public void onSort$lh_name() {
		System.out.println("ONSORT_CALLED on listheader lh_name.");
	}
	public void onSort$lh_id() {
		System.out.println("ONSORT_CALLED on listheader lh_id.");
	}
}

sort.zul:

<window title="Mah Window" border="normal" width="200px">
<listbox apply="ListBOx1Composer">
	<listhead>
		<listheader id="lh_name" label="name" sort="auto" />
		<listheader id="lh_id" label="id" sort="auto" />
	</listhead>
</listbox>
</window>

Then in your each onSort method you can then use different Comparators to your list model.

Hope this helps.

link publish delete flag offensive edit

answered 2008-12-25 17:00:35 +0800

erickmelo gravatar image erickmelo
87 2

Hi robertlee,

Thank you for the help... I will try do it.... But I think it should be a interesting feature....

When I put:

<listitem self="@{each=categoria}">
<listcell label="@{categoria.id}"/>
<listcell label="@{categoria.nome}"/>
</listitem>

ZK knows the type of categoria.nome, for example... And it's a String... For sorting ZK should have a default comparator for basic types, like String.... I think isn't a good idea have to implements a ListBoxComposer only to sort strings.... To more complex types I agree it's very interesting.... But for basic types is a work that could be made by ZK

Can I post it as a feature request??

[]s

link publish delete flag offensive edit

answered 2008-12-26 03:05:12 +0800

PeterKuo gravatar image PeterKuo
481 2

Of course you can post it to feature request.
We'll evaluate it, and schedule it by priority and difficulty.

link publish delete flag offensive edit

answered 2008-12-30 13:09:36 +0800

erickmelo gravatar image erickmelo
87 2

Hi,
I'm opening this track again.... The option : implements a Generic Forward Composer doesn't work:
I implemented the onSort method and it's working.... But as exception is trowed always:
"cannot cast do java.lang.Comparable"

I supose that:
If I'm ordering the objects in my ListBOx1Composer, my bean doesn't need implements Comparable, because the method compareTo() isn't called...

But ZK is calling this method.... So, the list is ordered, but I get this exception....

Is it a bug?? How can I, using listModel, have my list ordered, for each field?

[]s

link publish delete flag offensive edit

answered 2009-01-07 08:22:35 +0800

robertlee gravatar image robertlee
561

updated 2009-01-07 08:22:52 +0800

hi Erick,

if you look at the developer guide, it actually gave a better way to assign the comparator.(using sortAscending and sortDescending properties) Also if you remove the sort="auto", your exception will go away.

http://docs.zkoss.org/wiki/Grids,_Trees_and_Listbox#The_sortAscending_and_sortDescending_Properties

Hope this helps.

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: 2008-12-24 15:26:47 +0800

Seen: 459 times

Last updated: Jan 07 '09

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