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

documentationによれば、IndexedContainerはニーズに合っています。または、遅延ロードをサポートするコンテナを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(); } 

} 

これは読み取り専用です。アイテムにはアイテムのインデックスである0〜5000の1つのプロパティー"indexed"しかありません。ご覧のとおり、これは大変な作業ですので、可能であれば組み込みのコンテナを使用する必要があります。

+0

ありがとうございました。私は面白い何かに気づいた。私の独自のサンプルコードです。私はテーブルのすべての要素を表示するので、この行 'setPageLength(size());'を 'setPageLength(100);'に変更しました。怠惰な読み込みが行われています。テーブルには読み込みが遅れているという意味ですか?しかし、唯一のことは、私は自分のアプリケーション(私はテーブルの行ではなく、オブジェクトの整数を持っていない)を適用するときにレイジーローディングはOKをスクロールするときに動作しますが、 – antobbo

+0

奇妙な音、バグかもしれないが、コードなしで言うことができない。 –

+0

いいえ、あなたがそれについて考えるなら、私は意味します。基本的には 'setPageLength(size());')はすべての行を表示することを言っているので、 'setPageLength(100);では100行しか表示されていないので、他のものはレイジーローディングを介してロードされます(それが有効になっていると思われます)。コードは私がスレッドを更新したのと同じように含まれています – antobbo