Empty a table (including header)

Hi,

I need to display a table inside my panel which is dynamically field by a click on a button and content of a textfield. The dynamic includes content of table but header columns too, which could change completly.

I’ve coded a class called TView which is instanciated on my main application. This component adds itself on the main container (as shown on book of vaadin).

The method fillTable(int viewId) will fill the table based on the viewId got from the textField.


public void fillTable(int viewId) {
		APIRestService wars = APIRestService();
		ViewDTO view = null;
		//table.setColumnHeader(null, null);
		 
		
		table.removeAllItems();
		try {
			view = new ViewDTO(wars.get("http://10.225.7.25:6060/j2ee-api/View/" + viewId, "test1", "test"));	
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		for (ViewColumnDTO viewColumn : view.getViewHeader().getViewColumns()) {
			table.addContainerProperty(viewColumn.getName(), String.class, null);		
		}
		
		for (ViewRowDTO viewRow : view.getViewBody().getViewRows()) {
			Object[] aRow = new Object[viewRow.getViewCells().size()]
;
			int i = 0;
			for (ViewCellDTO viewCell : viewRow.getViewCells()) {
				aRow[i]
 = viewCell.getValue();
				i++;
			}
			table.addItem(aRow, viewRow.getDocId());
		}
	}

The fact is that as I’m trying to modify an existing table, it doesn’t clean the existing headers and the new ones are automatically added to the existing ones (which seems logical). However, I did’nt find the possibility to clean the headers. I will try to use table.setColumnHeader(…,…) method but I need to conserve a list of the last header name to destroy them. Perhaps there is another simplier method ?

Regards,
Denis.

I’ve found a way to do it. Indeed, instead of creating the underlying table component inside the constructor, I create it in fillTable(…) method which firstly remove the eventual existing table from the underlying container :


		container.removeComponent(table);
		table = new Table();	
		table.setWidth("100%");
		table.setSelectable(true);
		table.setImmediate(true);
		table.setColumnReorderingAllowed(true);
		table.setColumnCollapsingAllowed(true);
		container.addComponent(table);

If there is a cleaner method don’t hesitate :slight_smile: