2

I am trying to add an AbstractToolBar to a DefaultDataTable. The toolbar has a button on click of which the selected rows should get deleted. My table has a checkbox column, to select the rows.

AbstractToolBar implementation looks like this -

public class GridToolBar extends AbstractToolbar {

/**
 * 
 */
private static final long serialVersionUID = -2126515338632353253L;
Button btnDelete;
List<Contact> selected;

public GridToolBar(final DataTable<?> table) {
    super(table);
    // TODO Auto-generated constructor stub

    btnDelete = new Button("delete",new Model("Delete"));
    btnDelete.setOutputMarkupId(true);
    btnDelete.add(new AjaxEventBehavior("onclick") {

        private static final long serialVersionUID = 6720512493017210281L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            System.out.println(selected);
            ((UserProvider)table.getDataProvider()).remove(selected);
            target.add(table);
        }
    });

    add(btnDelete); 


}

public void setSelected(List inList){
    selected = inList;
}

}

The toolbar has been added to table as follows -

GridToolBar tb = new GridToolBar(table);
tb.setOutputMarkupId(true);
table.addTopToolbar(tb);

The code works fine, except on click of delete button it adds an additional delete button below the table. On inspecting it with firebug, the ids of both the buttons match exactly. On sorting the table though, the extra button is removed from the view.

Could someone help me how can I avoid creation of extra button on every click? Why is it being created in the first place? Any help is appreciated.

Thanks, Sonam

1 Answers1

0

You add the button directly to the table. This is incorrect, as you cannot have a button in a table. You need a <td> element. You can create one using a WebMarkupContainer. See also the source of for example NoRecordsToolbar

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121