2016-07-14 41 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