java - vaadin 中的延迟加载表

标签 java vaadin vaadin7

我试图找到一些使用延迟加载来加载信息(即行)的表的示例。我很好看,但我似乎无法在任何地方找到任何好的示例(我不想使用任何像 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");
    }

}

我读到,要实现延迟加载功能,我必须有一个支持它的容器,而不是表,这是我的理解。

最佳答案

根据documentationIndexedContainer 符合您的需求。或者,如果您想自己实现一个支持 Table 延迟加载的容器,则实现 Container.Indexed interface 。您可以浏览 IndexedContainer 源代码以获取示例。

我制作了一个实现Container.Indexed接口(interface)的基本示例:

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 之间(含)。正如您所看到的,这需要大量工作,因此您应该尽可能使用内置容器。

关于java - vaadin 中的延迟加载表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377984/

相关文章:

java - JNI 使用。 "symbol lookup error"

java - Vaadin Combobox - 无效自定义输入时不会出现错误

java - 如何从Sql中插入和获取数据

java - 使用 eclipselink 时将 persistence.xml 放在哪里?

java - Vaadin - 如何停止等待服务器响应?

java - 在for循环中添加列表/数组元素,java12

java - Hibernate @ManyToMany 不保存到第三个表

java - 如何解决 Maven/Gradle 中的多级间接缺失依赖 hell ?

在网格 ComponentRenderer 中使用时 ComboBox 的 Vaadin 12 ItemLabelGenerator

java - 创建新的maven项目指南