The code below is an example of having a checkbox at the header of a cell table in GTW that allows you to select/deselect all items in list (all pages).
The source file is at the bottom of the page.
package headerCheckboxCellTableExample.client;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class HeaderCheckboxCellTableExampleWebApp implements EntryPoint
{
/**
* A simple class to demonstrate data to be shown
*/
public class Person
{
public Person(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
/**
* The first name
*/
public String firstName;
/**
* the last name
*/
public String lastName;
}
/**
* An array to store the list of persons
*/
private java.util.List<Person> persons;
/**
* the cell table
*/
private CellTable<Person> table;
/**
* the selection model for the table
*/
private MultiSelectionModel<Person> selectionModel;
/**
* Generate persons to be displayed
*/
private void generatePersons()
{
persons = new java.util.ArrayList<Person>();
persons.add(new Person("Glenna","Snipes"));
persons.add(new Person("Karen","Nusbaum"));
persons.add(new Person("Lorena","Delzell"));
persons.add(new Person("Elnora","Guice"));
persons.add(new Person("Beulah","Wiley"));
persons.add(new Person("Larhonda","Corchado"));
persons.add(new Person("Terisa","Mckim"));
persons.add(new Person("Adela","Lion"));
persons.add(new Person("Harvey","Scianna"));
persons.add(new Person("Susy","Moses"));
persons.add(new Person("Brandee","Wiggins"));
persons.add(new Person("Zachariah","Gittens"));
persons.add(new Person("Dedra","Littler"));
persons.add(new Person("Leontine","Jezierski"));
persons.add(new Person("Roman","Otani"));
persons.add(new Person("Venita","Reich"));
persons.add(new Person("Cecelia","Torrain"));
persons.add(new Person("Carlyn","Guerrette"));
persons.add(new Person("Windy","Almodovar"));
persons.add(new Person("China","Witten"));
}
/**
* This is the entry point method.
*/
public void onModuleLoad()
{
generatePersons();
table = new CellTable<Person>(10);
// Add a selection model to handle user selection.
//allows mutltiple selection
selectionModel = new MultiSelectionModel<Person>();
table.setSelectionModel(selectionModel);
//set the first columns as checkboxes
//a checkbox cell at the start of row to indicate whether a person had been selected
CheckboxCell selectedCell = new CheckboxCell();
Column<Person,Boolean> selectedCol = new Column<Person,Boolean>(selectedCell)
{
@Override
public Boolean getValue(Person person)
{
//return true only the person at that row is selected
return selectionModel.isSelected(person);
}
};
//a checkbox at the header row to select/deselect all persons
CheckboxCell selectAllHeaderCB = new CheckboxCell(true,true);//the checkbox is true true for dependsOnSelection and handlesSelection for it to work
Header<Boolean> selectAllHeader = new Header<Boolean>(selectAllHeaderCB)
{
@Override
public Boolean getValue()
{
//return true only when all items are selected
boolean value = selectionModel.getSelectedSet().size() == persons.size();
return value;
}
};
selectAllHeader.setUpdater(new ValueUpdater<Boolean>()
{
@Override
public void update(Boolean value)
{
//select/deselect all persons
for (Person person : persons)
{
selectionModel.setSelected(person, value);
}
}
});
table.addColumn(selectedCol,selectAllHeader);
//end set up first column
//set up second column
TextColumn<Person> firstNameColumn = new TextColumn<Person>()
{
@Override
public String getValue(Person object)
{
return object.firstName;
}
};
table.addColumn(firstNameColumn, "First Name");
//end set up second column
//set up third column
TextColumn<Person> lastNameColumn = new TextColumn<Person>()
{
@Override
public String getValue(Person object) {
return object.lastName;
}
};
table.addColumn(lastNameColumn, "Last Name");
//end set up third column
table.setPageSize(10);//set the page size
//Push the data into the widget.
table.setRowData(0, persons);
//set up a pager to move between pages
SimplePager pager = new SimplePager();
pager.setDisplay(table);
// Associate an async data provider to the table so that data can be updated
AsyncDataProvider<Person> provider = new AsyncDataProvider<Person>()
{
@Override
protected void onRangeChanged(HasData<Person> display)
{
int start = display.getVisibleRange().getStart();
int end = start + display.getVisibleRange().getLength();
end = end >= persons.size() ? persons.size() : end;
java.util.List<Person> sub = persons.subList(start, end);
updateRowData(start, sub);
}
};
provider.addDataDisplay(table);
provider.updateRowCount(persons.size(), true);
RootPanel.get().add(table);
RootPanel.get().add(pager);
}
}