Delete Button and Generating CheckBox in Table

Hello,

I am having trouble with the Deleting Button and generating of Checkboxes in my table. I have been using some examples from the forum but i couldn’t manage to make it work. I would appreciate some help :slight_smile:

Thank you!
BR, Elena

The button code is:

delete = new Button("Delete");
        delete.setVisible(true);
        delete.addClickListener(new Button.ClickListener() {
                            @Override
              public void buttonClick(ClickEvent event) {
              List<Object> toDelete = new ArrayList<Object>();
              treeTable.addContainerProperty("Checkbox", CheckBox.class, null);
              treeTable.addContainerProperty("String", String.class, null);
                                  
                   for (int i = 0; i < 10; i++) {
                   Object id = treeTable.addItem();
                   treeTable.getContainerProperty(id, "Delete").setValue(new CheckBox());
                   treeTable.getContainerProperty(id, "String").setValue("row " + id);
                      }
               
               for (Object id : treeTable.getItemIds()) {
                // Get the checkbox of this item (row)
                CheckBox checkBox = (CheckBox) treeTable.getContainerProperty(id, "Delete").getValue();
                                         
                if (checkBox.booleanValue()) {
                toDelete.add(id);
                     }
                 }
                                         
                 // Perform the deletions
                 for (Object id : toDelete) {
                 treeTable.removeItem(id);
                    }
                                                     
                Notification.show("Removed row", "",Notification.Type.WARNING_MESSAGE);
                                    }
                                });

Hi Elena!

I think the problem with your solution is that you are at the same time adding the checkboxes to the table and trying to remove the rows inside the button’s clicklistener. Consequently none of the checkboxes are ever checked and no rows are removed. Also you need to be mindful with the propertyIds: in your example you are adding a property of id “Checkbox” but then accessing a property of id “Delete”. Unless you have added another property elsewhere to the table, the code will not work.

I modified your code a bit to make it work:

[code]
treeTable.addContainerProperty(“Delete”, CheckBox.class, null);
treeTable.addContainerProperty(“String”, String.class, null);

for (int i = 0; i < 10; i++) {
Object id = treeTable.addItem();
treeTable.getContainerProperty(id, “Delete”).setValue(new CheckBox());
treeTable.getContainerProperty(id, “String”).setValue("row " + id);
}

delete = new Button(“Delete”);
delete.addClickListener(new ClickListener(){

@Override
public void buttonClick(ClickEvent event) {

    List<Object> toDelete = new ArrayList<Object>();
    for (Object id : treeTable.getItemIds()){
        // Get the checkbox of this item (row)
        CheckBox checkBox = (CheckBox) treeTable.getContainerProperty(id, "Delete").getValue();                             
        if (checkBox.getValue()) {
            toDelete.add(id);
        }
    }                     
    // Perform the deletions
    for (Object id : toDelete) {
        treeTable.removeItem(id);
    }
}

});
[/code]Hopefully this helps!