How to delete the table row with out using check box?

Hello Vaadin Experts,

I am new to vaadin and i have a doubt regarding deleting a cell from the table.

I have 2 native select’s and a delete button in each row of the table. When the user click on the delete button that particular row should be deleted.

I don’t want to use check box for selecting the row and iterate through the table whether the check box is selected for deleting the row.
Is there any way that i can get the table row ID after clicking on the delete button.

I don’t want to use set selectable for getting the row id.

Please Help me out

Thanks & Regards,

Siva

One way I can think of is saving the item id as the buttons data object (Button.setData(itemId)). Then when a user clicks that button, retrieve the item id from the button and erase that row.

Or you can set the table to allow for multi-select so when they click a delete button you removed all selected rows.

Great John It works. Thanks a lot. I am fighting with this from 2 days. Now it works.

Hello Vaadin Experts,

My problem with delete button solved.

Now I have one more problem. I had a Horizontal Layout in one cell of the table which has only textfield in it. When I click on the textfirld a pop up comes in and asks for the value to be entered into the textfield.

[b]
My problem when i had 2 rows in the table the value is the i selected from the pop up is entered into the final row text field but not the row whic i selected.

[/b]

When I set the data into the textfield or the layout the data of the final cell is reflected.

I don’t want to use set selectable for getting the row id.

Please Help me out

Thanks & Regards,

Siva

Hi ,

I have a another solution for deletion of the row from table.I have used collection to fill table and delete row from table.

How it works,
When GUI is loaded first time, table is populate and button remove is disabled. when user select one of the row from table to delete it, button remove is enable. Clicking button will remove selected row from table.


it is the table class which will populate the value in table and row deletion method will delete selected row through rowId.

package myVaadin.Table;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Vector;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Table;

public class FirstTable extends VerticalLayout {
private static final long serialVersionUID = 1L;

	private Table table;
	private Button btnRimuovi;
	private Collection< String > collectionDtoTable;
	
		public FirstTable(Button btnRimuovi) {
		
		this.btnRimuovi =btnRimuovi; // Button is in my calling class, you can make it here also
		table= new Table();			
		// size
		table.setWidth("300.0px");
		table.setHeight("300.0px");			
		addComponent(table);
		table.setSelectable(true);
		table.setImmediate(true); //react at once when something is selected
	}
	
	public void buildListOL() throws Exception
	{			
		table.addContainerProperty("FirstHeader", String.class, null);
		table.addContainerProperty("SecondHeader",String.class, null);
	}				
		public void populateTable(Collection<String> collectionDtoTable){
			this.collectionDtoTable=collectionDtoTable;
			System.out.println("collectionDtoTable"+collectionDtoTable.size());
			   int itemId=1;
			   table.removeAllItems();
				for(int i=0;i<5;i++){
					Vector v = new Vector();
					v.add(((ArrayList<String>)collectionDtoTable).get(i));
					v.add(((ArrayList<String>)collectionDtoTable).get(i));
					table.addItem(v.toArray(), new Integer(itemId));
					itemId++;						
				}
			table.addListener(new ItemClickEvent.ItemClickListener() {   // for ebaling of the button on selecting a row

				private static final long serialVersionUID = 1L;
				public void itemClick(ItemClickEvent event) {
					btnRimuovi.setEnabled(true);						
				}
			});}			
		
		public void rowDeletion()// this will remove selected row from the table.
		{
			
			Integer rowId = (Integer)table.getValue();
			((ArrayList<String>)collectionDtoTable).remove(rowId.intValue()-1);				
			
			try {		
				table.removeAllItems();		
				populateTable(collectionDtoTable);
				} catch (Exception e) {
				e.printStackTrace();
			}}}			    
		  [b]

It is class which will create main Window.
[/b]
package myVaadin;
import com.vaadin.Application;
import com.vaadin.ui.Window;

public class TestApplication extends Application
{ 

public void init()
{
Window mainWindow = new Window(“Test Application”);
ShowTable myComponent = new ShowTable();
// may be needed if you haven’t set the size in the component
myComponent.setSizeFull(); // make sure the implicit layout of the main window fills the window

		mainWindow.getContent().setSizeFull();        
		mainWindow.addComponent(myComponent);      
		setMainWindow(mainWindow);  
		}
	}


it is class which will add table and button to mainLayout(large though)

public class ShowTable extends CustomComponent{

private AbsoluteLayout mainLayout;
private AbsoluteLayout absLytComponent;
private Panel pnlMain;
private Button btnRimuovi;
FirstTable firstTable;
private Collection <String> collectionDtoTable;

public ShowTable() {
	
	// the main layout and components will be created here
	mainLayout = new AbsoluteLayout();
    mainLayout.setHeight("360.0px");
    mainLayout.setWidth("100%");
    mainLayout.setDebugId("mainLayout");
	//add component level Height and Weight
    pnlMain = mainPnlFunt();	
	mainLayout.addComponent(pnlMain);		
	setCompositionRoot(mainLayout);
}


private Panel mainPnlFunt()
{
   pnlMain = new Panel();	
   pnlMain.setHeight("100%");
   pnlMain.setWidth("100%");
   pnlMain.setDebugId("mainPnlFunt");
   absLytComponent = absLytComponent();
   pnlMain.setContent(absLytComponent); 
   return pnlMain;
}

private AbsoluteLayout absLytComponent()
{
	absLytComponent = new AbsoluteLayout();
	absLytComponent.setHeight("100%");
	absLytComponent.setWidth("100%");
	absLytComponent.setDebugId("absLytComponent");
	absLytComponent.setMargin(false);		
	
	btnRimuovi = new Button("remove");
	btnRimuovi.setDebugId("AccordianInsTipoCodec-BttnAggiungi");
	btnRimuovi.setImmediate(true);
	btnRimuovi.setEnabled(false);
	btnRimuovi.setWidth("-1.0px");
	btnRimuovi.setHeight("-1.0px");

	collectionDtoTable = new ArrayList<String>();

	collectionDtoTable.add("value1");
	collectionDtoTable.add("value2");
	collectionDtoTable.add("value3");
	collectionDtoTable.add("value4");
	collectionDtoTable.add("value5");
	collectionDtoTable.add("value6");
	collectionDtoTable.add("value7");
	collectionDtoTable.add("value8");
	
	firstTable = new FirstTable(btnRimuovi);
	try {
		firstTable.buildListOL();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println("collectionDtoTable"+collectionDtoTable.size());
	firstTable.populateTable(collectionDtoTable);
	
	btnRimuovi.addListener(new Button.ClickListener() {
		
		private static final long serialVersionUID = 1L;
		public void buttonClick(ClickEvent event) {
			
			firstTable.rowDeletion();	
	}});
	absLytComponent.addComponent(btnRimuovi,"left:450.0px");
	absLytComponent.addComponent(firstTable,"top:5.0px;left:5.0px");
	return absLytComponent;

}
}
Please write me if u need more infor on it ,
vconly4you@gmail.com