2016-07-14 40 views
1

我试图找到一些使用延迟加载来加载信息的示例,即行。我有一个很好看,但我似乎无法在任何地方找到任何好的例子(我不想使用像Viritin任何添加),我只是想从头开始。 vaadin网站上的文档并没有真正帮助Table,所以我只是想知道是否有人知道有什么好的教程可以解释需要完成的工作。也许一个例子可能会更好。所以这里是一个简单的表格,显示5000以下的整数。我将尝试在这里实现延迟加载,这是一个非常简单的应用程序,然后希望我能够将简单易用的功能集成到我自己的应用程序中。这是代码。 我的UI类(MyUI.java):vaadin中的懒惰加载表

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

你有什么样的数据源? Vaadin中的一些容器支持延迟加载。 –

+0

嗨有一个表(在扩展表的类中创建) – antobbo

+0

表是从容器显示数据的方式。你如何填充表格? https://vaadin.com/docs/-/part/framework/components/components-table.html。该表根据需要从容器中检索行 –

回答

1

根据documentationIndexedContainer符合您的需求。或者,如果您想自己实现支持延迟加载的容器,请使用Table然后执行Container.Indexedinterface。例如,您可以浏览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之间。正如你所看到的那样,这是很多工作,所以如果可能的话,你应该使用一个内置的容器。

+0

感谢您的代码。我注意到一些相当有趣的事情。在我自己的示例代码中。我显示了表中的所有元素,所以我将此行更改为setPageLength(size());'setPageLength(100);'。瞧,懒加载works.Does这意味着表具有延迟加载内置?但唯一的一点是,当我将它应用于我自己的应用程序时(我在表格行中没有整数,但是有对象),当您向下滚动时,延迟加载可以正常工作,但是当您再次向上滚动时,某些表格行是空的 – antobbo

+0

听起来很奇怪,也许是一个bug,不能没有代码就说。 –

+0

呃,不,我是说如果你想一想。使用'setPageLength(size());''你实际上是说要显示所有的行,所以你不能像任何东西一样使用惰性加载来做任何事情,使用'setPageLength(100)';'你只有100行显示而其他的是通过延迟加载加载的(提供它看起来是启用的)。当我更新线程时,代码被包含在内。 – antobbo