我有一个从BeanItemContainer扩展的Container类。我想将ItemSorter添加到DATE_CREATED属性,以按降序对值进行排序。
容器类。
public class NoteContainer extends BeanItemContainer<CaseNote> implements Serializable
{
private static final long serialVersionUID = -5926608449530066014L;
public static final String DATE_CREATED = "dateCreated";
public static final String CREATED_BY = "createdBy";
public static final String TEXT = "text";
public static final String ACTION = "Action";
public static final Object[] NATURAL_COL_ORDER = new Object[] {
ACTION, DATE_CREATED, CREATED_BY, TEXT
};
public static final String[] COL_HEADERS_ENGLISH = new String[] {
"ACTION", "Date Created/Updated", "Created/Updated By", "Note"
};
/**
* Default Constructor.
*
*/
public NoteContainer()
{
super(CaseNote.class);
}
}
CaseNote是一个实体类,DATE_CREATED
内的格式为java.util.Date
。
请提供适当的解决方案......
相关提前完成。
答案 0 :(得分:4)
BeanItemContainer已经准备好了轻松设置ItemSorter的所有内容。 您可以使用此功能:
public void sort(Object[] propertyId, boolean[] ascending)
例如,您可以在容器中添加sortByDate()函数来执行此操作。这里有三个示例类。
The Bean:
public class CaseNote {
private static final Random R = new Random();
private static final long Y_IN_MILLIES = 1000l * 60l * 60l * 24l * 365l;
private Date dateCreated = new Date(System.currentTimeMillis() - Math.round(R.nextDouble() * Y_IN_MILLIES));
private String text = UUID.randomUUID().toString();
public String getText() {
return text;
}
public Date getDateCreated() {
return dateCreated;
}
}
容器:
public class NoteContainer extends BeanItemContainer<CaseNote> {
public NoteContainer() {
super(CaseNote.class);
}
public void sortByDate() {
sort(new String[] { "dateCreated" }, new boolean[] { false });
}
}
测试用户界面:
public class TestUI extends UI {
@Override
protected void init(VaadinRequest request) {
NoteContainer nc = new NoteContainer();
for (int i = 0; i < 10; i++) {
nc.addItem(new CaseNote());
}
nc.sortByDate();
for (int i = 0; i < 10; i++) {
nc.addItem(new CaseNote());
}
Table t = new Table("MyTable", nc);
setContent(t);
}
}
注意:调用sortByDate()函数后添加的项目未排序。如果要在每次插入后对项目进行排序,可以覆盖addItem()函数,以便在添加项目后调用sort()。