-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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);
}
}
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
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.
@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
Search in this thread for BranchComparator to see how you can implemented such a thing.
@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
Asked: 2007-05-15 02:18:58 +0800
Seen: 989 times
Last updated: Mar 10 '11