Refreshing data in Grid

Hello,

I’m using the new Grid component (using the release Vaadin 7.4.0) and need to refresh the data in it. My idea was to remove all existing items and add the new items again. This lead to a reproducible crash of the browser if more than one item exists. Sample source code:

[code]
package com.desoft._0000_000.vaadin;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.data.Item;
import com.vaadin.event.SelectionEvent;
import com.vaadin.event.SelectionEvent.SelectionListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/**
/
@Theme(“mytheme”)
@Widgetset(“com.desoft._0000_000.vaadin.MyAppWidgetset”)
public class MyUI extends UI
{
/
* First name /
private static final String FIRST_NAME = “firstName”;
/
* Last name */
private static final String NAME = “name”;

/** Table */
private Grid m_Grid;
/** Add */
private Button m_Add;
/** Remove All */
private Button m_RemoveAll;
/** Remove All and Add */
private Button m_RemoveAllAndAdd;

/**
 * @see com.vaadin.ui.UI#init(com.vaadin.server.VaadinRequest)
 */
@Override
protected void init(final VaadinRequest vaadinRequest)
{
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

    m_Grid = new Grid();
    m_Grid.setImmediate(true);
    m_Grid.setSizeFull();
    m_Grid.addSelectionListener(new SelectionListener()
    {
        @Override
        public void select(final SelectionEvent arg0)
        {
            updateActions();
        }
    });
    m_Grid.setSelectionMode(SelectionMode.SINGLE);
    m_Grid.setHeightByRows(5);
    layout.addComponent(m_Grid);

    m_Grid.addColumn(NAME, String.class).setHeaderCaption("Nachname");
    m_Grid.addColumn(FIRST_NAME, String.class).setHeaderCaption("Vorname");

    addPerson("Zierhut", "Andreas");
    addPerson("Mustermann", "Max");

    HorizontalLayout hl = new HorizontalLayout();
    hl.setSpacing(true);
    layout.addComponent(hl);

    m_Add = new Button("Add");
    m_Add.addClickListener(new Button.ClickListener()
    {
        @Override
        public void buttonClick(final ClickEvent event)
        {
            addPerson("Zierhut", "Andreas");
        }
    });
    hl.addComponent(m_Add);

    m_RemoveAll = new Button("Remove All");
    m_RemoveAll.addClickListener(new Button.ClickListener()
    {
        @Override
        public void buttonClick(final ClickEvent event)
        {
            m_Grid.getContainerDataSource().removeAllItems();
        }
    });
    hl.addComponent(m_RemoveAll);

    m_RemoveAllAndAdd = new Button("Remove All And Add");
    m_RemoveAllAndAdd.addClickListener(new Button.ClickListener()
    {
        @Override
        public void buttonClick(final ClickEvent event)
        {
            m_Grid.getContainerDataSource().removeAllItems();

            addPerson("Zierhut", "Andreas");
        }
    });
    hl.addComponent(m_RemoveAllAndAdd);
}

/**
 * Adds a new item to the table.
 *
 * @param name the last name
 * @param firstName the first name
 */
@SuppressWarnings("unchecked")
private void addPerson(final String name, final String firstName)
{
    Object itemId = m_Grid.getContainerDataSource().addItem();
    Item item = m_Grid.getContainerDataSource().getItem(itemId);
    item.getItemProperty(NAME).setValue(name);
    item.getItemProperty(FIRST_NAME).setValue(firstName);
}

/**
 * Refreshes all actions.
 */
protected void updateActions()
{
    m_RemoveAllAndAdd.setEnabled(true);
}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet
{
    // not necessary
}

}
[/code]If you click the “Remove All And Add” button the browser (both Firefox and Internet Explorer) memory increases infinitely and doesn’t react anymore. It works if you click the “Remove All” and the “Add” afterwards, but there still is a problem with the column widths which get lost.

Am I doing something wrong?

It works if I remove the items one by one:

                Indexed indexed = m_Grid.getContainerDataSource();
                List<?> list = new ArrayList<Object>(indexed.getItemIds());
                for(Object itemId : list)
                {
                    indexed.removeItem(itemId);
                }

Instead of:

                m_Grid.getContainerDataSource().removeAllItems();

But the column widths are still lost…

Hi Andreas,

I have encountered the same issue when trying to repopulate a grid based upon a selection from a combo box. I was just about to start a thread about it when I noticed this thread. I don’t think my example would add anything on top of what you have said already. Hopefully some one can point us in the right direction.

If someone encounters the same problem: it is already fixed for 7.4.1: http://dev.vaadin.com/ticket/16747

7.4.1 contain a solution for column width in this case too?

With the same bug, I have tested to add

for(Column col : mainGrid.getColumns()) { col.setExpandRatio(1); } But it’s refresh the column size only the first time, and don’t find any other solution.

For information :

        mainGrid.recalculateColumnWidths(

10:34:09,084 SEVERE [com.vaadin.server.DefaultErrorHandler]
(http-localhost/127.0.0.1:8080-2) : java.lang.NoSuchMethodError: com.vaadin.ui.Grid.recalculateColumnWidths()V

I have tried to clean project/maven update project in eclipse. Delete manually the target directory and launch this command :mvn -U clean vaadin:update-widgetset vaadin:compile package

I’m sure to have the 7.4.1 grid’s class version. The eclipse auto-completion give me the recalculateColumnWidths().

An idea for issue?