Grid data content not properly sync

Hi,

I’m facing a really weird behavior in using GRID component where I do modify its content.
I’ll explain the flow I follow to reach to this situation:

  1. I create a Grid component and define a BeanItemContainer associated to it.
	BeanItemContainer<Object> conNew = new BeanItemContainer(bean.getClass());
	Grid grdNew = new Grid("New data", conNew);

  1. Add some data to it deleting the previous one.
	public boolean insertNewData(ArrayList<?> data) {
		boolean res = true;
		grdNew.getContainerDataSource().removeAllItems();
		grdNew.getSelectionModel().reset();
		grdNew.refreshAllRows();

	if (data.size() > 0) {
		try {
			for (Object item : data) grdNew.getContainerDataSource().addItem(item);
			grdNew.getSelectionModel().reset();
			grdNew.recalculateColumnWidths();
			grdNew.refreshAllRows();
		} catch (Exception e) {
			res = false;
		}
	} else grdNew.setHeightByRows(1);

	return res;
}
  1. The first time I add data to the Grid, when I select any of the items, I can check that the selected item is properly obtained from
		grdNew.addSelectionListener(new SelectionListener() {
			@Override
			public void select(SelectionEvent event) {
				Set<Object> selEvent = event.getSelected();
			}
		});
  1. If I call again insertNewData(…) method and I select any row from the Grid, what I get in the selection listener are the rows previously selected!
  2. Besides, if instead of retrieve the selected rows from the Grid I get the list of items contained, I get correctly the last loaded into the Grid.

Can anyone explain what is happening?

Thanks in advance,
Martin

Sounds like maybe something is getting mixed up in the internal bookkeeping of selected items. Instead of refreshing the existing data, it might be better just to create a whole new Grid from scratch.

-Olli

Thanks a lot for your response Olli,

I’ve thought that the command on the Grid

grdNew.getSelectionModel().reset();

would precisely do that: forces a refresh on the selected items. Isn’t that so?
Is there any other way to force the refreshing?

Anyway, I’ll make a test as you propose and I’ll see what happens…

The JavaDoc of SelectionModel::reset says:

/**
         * Resets the SelectiomModel to an initial state.
         * <p>
         * Most often this means that the selection state is cleared, but
         * implementations are free to interpret the "initial state" as they
         * wish. Some, for example, may want to keep the first selected item as
         * selected.
         */

…so the actual implementation is left some amount of freedom to do what it wants.

The implementation in MultiSelectionModel just does this:

        /**
         * Resets the selection model.
         * <p>
         * Equivalent to calling {@link #deselectAll()}
         */
        @Override
        public void reset() {
            deselectAll();
        }

so I’m not sure how that will play out. You’d need to investigate how exactly deselect works in that scenario (itemids have changed / selection is not included in itemids any more).

-Olli

-Olli

I’ve found a workaround to solve this issue. It’s not very elegant nor efficient but it works:

to get the currently selected items, instead of:

Set<Object> elemSelected = new HashSet<Object>((Collection<? extends Object>) grdNew.getSelectionModel().getSelectedRows());

it can be done:

Set<Object> elemSelected = new HashSet<Object>();
Set<Object> elemTotal = new HashSet<Object>((Collection<? extends Object>) grdNew.getContainerDataSource().getItemIds());
for (Object elem : elemTotal) 
	if (grdNew.getSelectionModel().isSelected(elem)) elemSelected.add(elem);

I do not understand the logic behind this, but at least it works