0

Live data Listbox sorting

asked 2007-05-15 02:18:58 +0800

admin gravatar image admin
18691 1 10 129
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4312721

By: sdswamy

Following the Developers Guide, I have implemented the following. The question I have is what should I write inside the sort() method in the ProductListModel class that implements ListModelExt??
------------------------------------------------------------------
StringListComparator.java
------------------------------------------------------------------

public class StringListComparator implements java.util.Comparator{

private boolean ascending;
private int _idx;

public StringListComparator(boolean asc, int idx){
ascending = asc;
_idx = idx;
}

public int compare(Object o1, Object o2) {
String s1 = ((Listcell)((Listitem) o1).getChildren().get(_idx)).getLabel();
String s2 = ((Listcell)((Listitem) o1).getChildren().get(_idx)).getLabel();

int v = s1.trim().toUpperCase().compareTo(s2.trim().toUpperCase());
return ascending ? v: -v;
}
}

--------------------------------------------------------------
This is the zul file
-------------------------------------------------------------
<window id="ProductWindow" border="normal">
<zscript>
import com.xxx.yyy.util.comparators.StringListComparator;

StringListComparator name_asc = new StringListComparator(true, 1);
StringListComparator name_dsc = new StringListComparator(false, 1);
StringListComparator number_asc = new StringListComparator(true, 2);
StringListComparator number_dsc = new StringListComparator(false, 2);
</zscript>

<listbox use="com.xxx.yyy.webapp.ProductList" id="ProductListFacadeDelegate"
rows="10" onSelect="showEditWindow()">
<listhead>
<listheader label="Name" maxlength="45" width="150px" sortAscending="${name_asc}"
sortDescending="${name_dsc}" />
<listheader label="Number" width="100px" sortAscending="${number_asc}"
sortDescending="${number_dsc}" />
</listhead>
</listbox>
</window>
-------------------------------------------------------------
ProductList.java
-------------------------------------------------------------
public class ProductList extends Listbox implements AfterCompose{

private List products;


public void afterCompose(){
ListModel model = new ProductListModel(this.getProducts());
//System.out.println("Model size is: " + model.getSize());
ListitemRenderer itemRenderer = new ProductListItemRenderer();
this.setModel(model);
this.setItemRenderer(itemRenderer);
}
}
--------------------------------------------------------------
ProductListModel.java
--------------------------------------------------------------
public class ProductListModel extends ListModelList implements ListModelExt{

private List products;
public ProductListModel(List _products){
products = _products;
}

public void sort(Comparator cmpr, boolean ascending) {

//HOW TO DO THE REAL SORTING??
//WHAT SHOULD I DO HERE?? I WANT TO USE THE SAME COMPARATOR CLASS FOR MANY COLUMNS
//List list = Arrays.asList(products);
// Collections.sort(list, new StringListComparator())
new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1);
}
}

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2007-05-18 15:11:59 +0800

admin gravatar image admin
18691 1 10 129
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4319442

By: jumperchen

In your case, I don't know what you will realize in the sort method.
But I have an example to show how to implement the sort() method.
For example,

<zk>
<zscript>
import java.util.Comparator;

public class MyListModel extends SimpleListModel implements ListModelExt{ Object[] _data;
public MyListModel(Object[] data) {
super(data);
_data = data;
}

public void sort(Comparator cmpr, boolean ascending) { if(ascending){ Arrays.sort(_data, new Comparator() {
public int compare(Object o1, Object o2) {
return ((String) o1).compareTo((String) o2);
}
});
}else{
Arrays.sort(_data, new Comparator() {
public int compare(Object o1, Object o2) {
return ((String) o2).compareTo((String) o1);
}
});
}
fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1); } } </zscript> <window title="Livedata Demo" border="normal">
<zscript>
String[] data = new String[30];
for(int j=0; j < data.length; ++j) {
data[j] = "option "+j;
}
ListModel strset = new MyListModel(data);
</zscript>
<listbox id="list" width="200px" rows="10" model="${strset}">
<listhead>
<listheader label="Load on Demend" sort="auto"/>
</listhead>
</listbox>
</window>
</zk>

Regards,
Jumper

link publish delete flag offensive edit

answered 2007-07-09 04:04:40 +0800

admin gravatar image admin
18691 1 10 129
ZK Team
Hi, I have got this to work using code very similar to yours but I am a bit concerned that I could not get it to work following the instructions of the Developer Guide (p135 "sorting with live data"). This seems a simple thing many people would wa
link publish delete flag offensive edit

answered 2007-07-09 06:22:30 +0800

admin gravatar image admin
18691 1 10 129
ZK Team
Hi, I cannot duplicate your issue, but you can refer to the following example. <zk> <zscript> import java.util.Comparator; public class MyModel extends ListModelList { public MyModel(List list) { super(list, true); } public
link publish delete flag offensive edit

answered 2007-07-10 23:29:42 +0800

admin gravatar image admin
18691 1 10 129
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4406741

By: dinglemouse

Hi Jumper,
Yes it did help. It's all good. Thanks! For future reference I put a Listbox sorting/rendering sample on the wiki.
Cheers,
DM.

link publish delete flag offensive edit

answered 2011-03-09 05:43:00 +0800

yaryan997 gravatar image yaryan997
210 2

@admin

Can you plz help me to solve my sorting problem. I had posted my problem to
http://www.zkoss.org/forum/listComment/15485

actually when I run my application i got java.util.hashmap cannot be cast to java.lang.Comparable

Best Regard
Yogendra

link publish delete flag offensive edit

answered 2011-03-09 07:35:33 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Search in this thread for BranchComparator to see how you can implemented such a thing.

link publish delete flag offensive edit

answered 2011-03-10 01:01:27 +0800

yaryan997 gravatar image yaryan997
210 2

@terrytornado

hey terry thanx for you help but one more problem occured. you can see that post at
http://www.zkoss.org/forum/listComment/15485/1/20#reply

my all problems i had listed there

Best Regards
Yogendra

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: 2007-05-15 02:18:58 +0800

Seen: 988 times

Last updated: Mar 10 '11

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