GWT CellTable programmatically select CheckBoxCell

19,249

Solution 1

Assuming what you want is to bind the checkbox to "selection" and easily select a bunch of items programmatically (subtlety: select items, which will result in checkbox being checked, rather than checking boxes), you'll use a MultiSelectionModel.

You'll find sample code in http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTable, that is:

final MultiSelectionModel<ContactInfo> selectionModel = new MultiSelectionModel<ContactInfo>(
    ContactDatabase.ContactInfo.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel,
    DefaultSelectionEventManager.<ContactInfo> createCheckboxManager());
…
Column<ContactInfo, Boolean> checkColumn = new Column<ContactInfo, Boolean>(
    new CheckboxCell(true, false)) {
  @Override
  public Boolean getValue(ContactInfo object) {
    // Get the value from the selection model.
    return selectionModel.isSelected(object);
  }
};
cellTable.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
cellTable.setColumnWidth(checkColumn, 40, Unit.PX);

Then, to select an item (and have its checkbox checked automatically), you'll simply do:

selectionModel.setSelected(item, true);

and you can similarly get the set of all selected items with selectionModel.getSelectedSet().

Solution 2

I don't know how you've set up your cellTable, but I suppose you have a dataProvider linked to it. Either a ListDataProvider or AsyncDataProvider. You want to modify the data in the provider to change the status of your rows.

Here is a simple example for a select/unselect all, assuming you have a uiBinder with a cellTable and checkbox :

public class MyView extends Composite {

  private class MyTableRow {
    private boolean checked;

    public boolean isChecked() {
        return this.checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

  }

  interface MyViewUiBinder extends UiBinder<Widget, MyView> {
  }

  private static MyViewUiBinder uiBinder = GWT.create(MyViewUiBinder.class);
  private ListDataProvider<MyTableRow> provider = new ListDataProvider<MyTableRow>();
  @UiField(provided = true)
  CellTable<MyTableRow> table;

  public MyView() {
    this.table = new CellTable<MyTableRow>();
    Column<MyTableRow, Boolean> checkBoxColumn = new Column<MyTableRow, Boolean>(new CheckboxCell()) {

        @Override
        public Boolean getValue(MyTableRow object) {
            return object.isChecked();
        }

    };
    this.table.addColumn(checkBoxColumn);
    this.initWidget(uiBinder.createAndBindUi(this));
  }

  @UiHandler("selectAllBox")
  public void onSelectAllClicked(ClickEvent e) {
    for (MyTableRow row : this.provider.getList()) {
        row.setChecked(((CheckBox)e.getSource()).getValue());
    }
  }

}
Share:
19,249
Noya
Author by

Noya

Main focused on Moblie Computing (Android and iPhone), web programming, video and music production.

Updated on June 07, 2022

Comments

  • Noya
    Noya almost 2 years

    I've got a cellTable with a CheckBoxCell column. What I'm trying to do is to have a set of buttons outside the celltable which let the user to automatically check/uncheck a different set of elements (for example check all / uncheck all, but I'd like to have more complex rules).

    What I don't understand is how to access the element (I suppose via row/col values ???) and get/set the value.

    Can anyone help me to figure out how to resolve it?

  • Noya
    Noya almost 13 years
    Thanks for the answer but it's not my case: I don't want to check/uncheck via a table selection but rather using a set of buttons outside the table. The binding is between the column's element and a button outside the table
  • Noya
    Noya almost 13 years
    thanks. In my case I'm using an AsyncDataProvider... I'll try to follow your example and then give feedback
  • Thomas Broyer
    Thomas Broyer almost 13 years
    The button's click handler would just have to "select" the appropriate items in the selection model (clear() + setSelected(item, true)), and the checkboxes state would then be updated accordingly (automatically).
  • Nobody
    Nobody almost 8 years
    @ThomasBroyer How would I go about selecting all items across multiple pages with an AsyncDataProvider?