我试图找到一些使用延迟加载来加载信息的表的例子,即行。我看起来很好看,但我似乎无法在任何地方找到任何好的例子(我不想像Viritin那样使用任何添加),我只是想从头开始。 vaadin网站上的文档并没有真正帮助Table所以我只是想知道是否有人知道任何有助于解释需要做什么的好教程。也许一个例子可能会更好。所以这是一个显示高达5000的整数的简单表。我将尝试在这里实现延迟加载,这是一个非常简单的应用程序,然后希望我能够在我自己的应用程序中轻松集成功能。这是代码。 我的UI类(MyUI.java):
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
numberTable theTable = new numberTable();
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener()
{
@Override
public void buttonClick(ClickEvent event)
{
System.out.println("test!");
}
});
layout.addComponents(button, theTable);
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);
}
表类(numberTable.java):
package my.vaadin.project.tableTest;
import com.vaadin.ui.Table;
public class numberTable extends Table
{
public numberTable(){
/*addContainerProperty("Name", String.class, null);
addContainerProperty("Mag", Float.class, null);
addItem(new Object[]{"Canopus", -0.72f}, 1);
addItem(new Object[]{"Arcturus", -0.04f}, 2);
addItem(new Object[]{"Alpha Centauri", -0.01f}, 3);*/
addContainerProperty("Number", Integer.class, null);
for(int i=1; i<=5000; i++){
Integer itemID = new Integer(i);
addItem(new Object[]{i},itemID);
}
setCaption("Rendering table");
addStyleName("testTable");
setPageLength(size());
System.out.println("table created");
}
}
我已经读过要实现延迟加载功能,我必须有一个支持它的容器,而不是表,这是我的理解。
答案 0 :(得分:1)
根据documentation,IndexedContainer
符合您的需求。或者,如果您想自己实现一个支持使用Table
延迟加载的容器,请实现Container.Indexed
interface。您可以浏览IndexedContainer
源代码以获取示例。
我已经为实现Container.Indexed
接口做了一个基本的例子:
public class MyContainer implements Container.Indexed {
public Object nextItemId(Object itemId) { return ((Integer) itemId) + 1; }
public Object prevItemId(Object itemId) { return ((Integer) itemId) - 1; }
public Object firstItemId() { return 0; }
public Object lastItemId() { return 5000; }
public boolean isFirstId(Object itemId) { return Integer.valueOf(0).equals(itemId); }
public boolean isLastId(Object itemId) { return Integer.valueOf(5000).equals(itemId); }
public Item getItem(Object itemId) {
PropertysetItem item = new PropertysetItem();
item.addItemProperty("index", new ObjectProperty<Integer>((Integer) itemId));
return item;
}
public Collection<?> getContainerPropertyIds() { return Arrays.asList("index"); }
public Collection<?> getItemIds() { return Arrays.asList(IntStream.range(0, 5001).boxed().toArray(Integer[]::new)); }
public Property getContainerProperty(Object itemId, Object propertyId) { return new ObjectProperty<Integer>((Integer) itemId); }
public Class<?> getType(Object propertyId) { return Integer.class; }
public int size() { return 5001; }
public boolean containsId(Object itemId) {
Integer item = (Integer) itemId;
return item >= 0 && item <= 5000;
}
public int indexOfId(Object itemId) { return (Integer) itemId; }
public Object getIdByIndex(int index) { return index; }
public List<?> getItemIds(int startIndex, int numberOfItems) { return Arrays.asList(IntStream.range(0, 5001).boxed().toArray(Integer[]::new)).subList(startIndex, startIndex + numberOfItems); }
public Item addItem(Object itemId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public Object addItem() throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public boolean removeItem(Object itemId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public boolean removeAllItems() throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public Item addItemAfter(Object previousItemId, Object newItemId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public Object addItemAt(int index) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
public Item addItemAt(int index, Object newItemId) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
}
它是只读的。项目只有一个属性"indexed"
,它是项目的索引,介于0和5000之间。如您所见,这是一项很多工作,所以如果可能的话,您应该使用内置容器。