Search code examples
jakarta-eevaadinvaadin7

Creating textfields inside a vaadin table and get their value in a container on button click


I am new to vaadin. I want to create a table which has 4 columns and N rows, and the table cells contain textfields, there would be two buttons outside of the table, on button click of 1st button more rows would be added to the table, and on the click of 2nd button the values in all the textfields should be added to some container. I tried this myself using Table.addGeneratedColumn("columnName", new ColumnGenerator() {}); and making a TextField inside it but it didn't show up in the table. Plus I also don't know how to assign unique IDs to each of the table rows. I already have idea of populating data to vaadin table using BeanItemContainer, but I don't know how to create a table which has empty TextFields and on button click I load the container with values that user have entered in the fields. Thanks in advance.


Solution

  • With the help of a friend and some searching I have done it in the following way :-

    public class MyTableClass extends VerticalLayout implements View, ValueChangeListener, ClickListener{
    private Table myTable = new Table();
    private  Button btnAddRow = null;
    private  Button btnSave = null;
    private  int rowIdsCount = 0;
    private TextField txtf1 = new TextField();  
    private TextField txtf2 = new TextField();  
    private TextField txtf3 = new TextField();  
    private TextField txtf4 = new TextField();
    
    private Table createTable()
    {
        myTable.addContainerProperty("TextField1", TextField.class, null);
        myTable.addContainerProperty("TextField2",  TextField.class, null);
        myTable.addContainerProperty("TextField3",  TextField.class, null);
        myTable.addContainerProperty("TextField4",  TextField.class, null);
    
        MyTableClass obj = new MyTableClass();
        myTable.addItem(new Object[] { obj.txtf1, obj.txtf2, obj.txtf3, obj.txtf4 },
                        ++rowIdsCount);
    
        return myTable;
    
    }                   
        private Table addRow() {
        MyTableClass obj = new MyTableClass();
        myTable.addItem(new Object[]{obj.txtf1,obj.txtf2,obj.txtf3,obj.txtf4}, ++rowIdsCount);
        return myTable;
    }
    
    public void buttonClick(ClickEvent event) {
            if (btnAddRow.equals(event.getButton())) {
                    this.addRow();
            } 
    
            else if(btnSave.equals(event.getButton())){
    
            for (int i = 1; i <= this.rowIdsCount; i++) {
                String txtf1Val = myTable.getItem(i).getItemProperty("TextField1").getValue().toString().trim();
                String txtf2Val = myTable.getItem(i).getItemProperty("TextField2").getValue().toString().trim();
                String txtf3Val = myTable.getItem(i).getItemProperty("TextField3").getValue().toString().trim();
                String txtf4Val = myTable.getItem(i).getItemProperty("TextField4").getValue().toString().trim();
                }
            }
    }