-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello guys,
I want to create a table with a dynamic header and dynamic content. I wrote:
<window>
<listbox model="@bind(vm.usersListModel)" vflex="1" sizedByContent="true">
<listhead children="@bind(vm.headerListModel)" hflex="1">
<template name="children" var="item">
<listheader label="@load(item)" sort="auto"/>
</template>
</listhead>
<template name="model" var="line">
<listitem children="@bind(line)">
<template name="children" var="cell">
<listcell label="@load(cell)"/>
</template>
</listitem>
</template>
</listbox>
</window>
and all I need is a sort for the columns. But with a ListModelList or ListModelArray the error "cannot be cast to java.lang.Comparable" comes. Do I need to write a comparator? How?
Here's demo try it:
package com.swain;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
public class TestViewModel {
private Set<Person> selectedCode;
public class Person {
private String _name;
public Person(String name) {
_name = name;
}
public String getName() {
return _name;
}
}
private ListModelList<Person> model;
@AfterCompose
public void init() {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("aaa"));
persons.add(new Person("bbbbb"));
persons.add(new Person("zzz"));
persons.add(new Person("ZZZZZ"));
persons.add(new Person("aaa"));
model = new ListModelList<Person>(persons);
model.setMultiple(true);
}
public ListModel<Person> getModel() {
return model;
}
public Set<Person> getSelectedCode() {
return selectedCode;
}
public void setSelectedCode(Set<Person> selectedCode) {
this.selectedCode = selectedCode;
}
}
Here's zul:
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="new page title" border="normal" >
<label value="You are using: ${desktop.webApp.version}"/>
<hlayout>
<window title="Listbox Default Auto-sorting" border="normal" hflex="1">
<listbox>
<listhead>
<listheader label="Name (insensitive)" sort="auto" />
</listhead>
<listitem>
<listcell label="aaa"/>
</listitem>
<listitem>
<listcell label="bbbbb"/>
</listitem>
<listitem>
<listcell label="zzz"/>
</listitem>
<listitem>
<listcell label="ZZZZZ"/>
</listitem>
</listbox>
</window>
<window title="Listbox Field Auto-sorting" border="normal" hflex="1"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('com.swain.TestViewModel')">
<listbox model="@load(vm.model)" multiple="true" sizedByContent="true" span="true" checkmark="true" height="400px" selectedItems="@bind(vm.selectedCode)">
<listhead>
<listheader></listheader>
<listheader label="Name (insensitive)" sort="auto(LOWER(name))"/>
<listheader label="Name (sensitive)" sort="auto(name)" />
</listhead>
<template name="model" var="person">
<listitem>
<listcell />
<listcell label="@load(person.name)"/>
<listcell label="@load(person.name)"/>
</listitem>
</template>
</listbox>
</window>
</hlayout>
</window>
</zk>
Or you write a comparator or you need to do this :
edit: You don't have to know the index, you compare 2 objects with each other.(I did fill in a comparator on the Id of an object, you just need to implement on what you want to compare.)
public class Item implements Comparable<Item> {
@Override
public int compareTo(Item item){
if(item.getId() < this.getId()) {
return -1;
} else if (item.getId() == this.getId()) {
return 0;
} else {
return 1;
}
}
}
Asked: 2014-02-11 10:48:10 +0800
Seen: 85 times
Last updated: Feb 11 '14
Ah okay... but how do I get the index/column to my compareTo method? I would use a ListModelArray.
DarkFoo ( 2014-02-11 12:30:26 +0800 )editSorry guys, but I think the problem is that I don't know the actual header label. <listheader label="@load(item)" sort="auto(cell)"/> does not work, because there is no method called "cell" In my I use an ArrayList<List<String>>.
DarkFoo ( 2014-02-12 08:54:35 +0800 )edit