How to uncheck header checkbox in Celltable when paging?

612 views
Skip to first unread message

atul

unread,
Jun 8, 2011, 5:57:45 AM6/8/11
to Google Web Toolkit
How to uncheck header checkbox in Celltable when paging?
The header checkbox is used to select/unselect all the rows in
Celltable by a single click on it:
Like we see in gmail and yahoo mail

The problem is that after selecting the header checkbox in the current
page,
it remains selected after paging to next page

I am using SimplePager for paging the celltable data

Below is the code:
* Copyright 2010 Google Inc.
package com.google.gwt.sample.celltable.client;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import
com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
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.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.GwtEvent.Type;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
/**
* Example of {@link CellTable}. This example shows a table containing
contact
* information.
*/
public class CelltableTrail implements EntryPoint {

/**
* A simple data type that represents a contact.
*/
private static class Contact {
private final String address;
private final Date birthday;
private final String name;

public Contact(String name, Date birthday, String address) {
this.name = name;
this.birthday = birthday;
this.address = address;
}
}

/**
* The list of data to display.
*/
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("1John", new Date(80, 4, 12), "123 Fourth Avenue"),
new Contact("2Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("3George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("4John", new Date(80, 4, 12), "123 Fourth Avenue"),
new Contact("5Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("6George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("7John", new Date(80, 4, 12), "123 Fourth Avenue"),
new Contact("8Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("9George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("10John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("11Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("12George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("13John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("14Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("15George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("16John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("17Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("18George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("19John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("20Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("21George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("22John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("23Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("24George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"),
new Contact("25John", new Date(80, 4, 12), "123 Fourth
Avenue"),
new Contact("26Joe", new Date(85, 2, 22), "22 Lance Ln"),
new Contact("27George", new Date(46, 6, 6), "1600 Pennsylvania
Avenue"));

public void onModuleLoad() {
// Create a CellTable.
final CellTable<Contact> table = new CellTable<Contact>();
Button addToListButton = new Button("Add To List");
Button selectAllButton = new Button("Select All");
Button resetButton = new Button("Reset");
table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

// Add a selection model to handle user selection.
final MultiSelectionModel selectionModel = new
MultiSelectionModel();

table.setSelectionModel(selectionModel,DefaultSelectionEventManager.<Contact>
createCheckboxManager());

//Add a CheckboxCell Header column to select all rows in current page

Column<Contact, Boolean> select = new Column<Contact,
Boolean>(new CheckboxCell()) {
@Override
public Boolean getValue(Contact object)
{
return selectionModel.isSelected(object);
}
};

select.setFieldUpdater(new FieldUpdater<Contact, Boolean>() {
public void update(int index, Contact object, Boolean value){
// Called when the user clicks on a checkbox.
selectionModel.setSelected(object, value);
}
});

final Header selectAllHeader = new Header(new CheckboxCell()) {
@Override
public Boolean getValue(){
return selectionModel.getSelectedSet().size() ==
table.getRowCount();
}
};

selectAllHeader.setUpdater(new ValueUpdater<Boolean>() {
@Override
public void update(Boolean value)
{
List<Contact> displayedItems = table.getVisibleItems();
for (Contact contact : displayedItems)
{
selectionModel.setSelected(contact, value);
}
}
});

table.addColumn(select, selectAllHeader);

// Add a text column to show the name.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact object) {
return object.name;
}
};
table.addColumn(nameColumn, "Name");

// Add a date column to show the birthday.
DateCell dateCell = new DateCell();
Column<Contact, Date> dateColumn = new Column<Contact,
Date>(dateCell) {
@Override
public Date getValue(Contact object) {
return object.birthday;
}
};
table.addColumn(dateColumn, "Birthday");

// Add a text column to show the address.
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact object) {
return object.address;
}
};
table.addColumn(addressColumn, "Address");



// Set the total row count. This isn't strictly necessary, but it
affects
// paging calculations, so its good habit to keep the row count up
to date.
//table.setRowCount(CONTACTS.size(), true);

// Create a data provider.
ListDataProvider<Contact> dataProvider = new
ListDataProvider<Contact>();

// Connect the list to the data provider.
dataProvider.addDataDisplay(table);

// Add the data to the data provider, which automatically pushes
it to the
// widget. Our data provider will have 27 values, but it will only
push
// the 5 that are in range to the list.
List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
// Push the data into the widget.
//table.setRowData(0, CONTACTS);

table.setRowCount(27);



// Create a SimplePager.
//SimplePager pager = new SimplePager();
SimplePager pager;
SimplePager.Resources pagerResources =
GWT.create(SimplePager.Resources.class);
pager = new SimplePager(TextLocation.CENTER, pagerResources,
false, 0, true);

//pager.setDisplay(table);

// Set the cellList as the display.
pager.setDisplay(table);

pager.setPageSize(5);




final List<Contact> ContactsAddedInList = new
ArrayList<CelltableTrail.Contact>();

// Listen for mouse events on the Add button.
addToListButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {

StringBuilder name = new StringBuilder();
Set<Contact> RecCONTACTS = new HashSet();
RecCONTACTS = selectionModel.getSelectedSet();
Iterator itr =RecCONTACTS.iterator();
while(itr.hasNext()){
Contact contact = (Contact) itr.next();
ContactsAddedInList.add(contact);
name = name.append(contact.name+" ");
}
Window.alert("You selected: " + name);
}
});

// Listen for mouse events on the Select All button.
selectAllButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {

for (Contact contact : CONTACTS) {
selectionModel.setSelected(contact, true);
}
}
});

// Listen for mouse events on the Reset button.
resetButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {

selectionModel.clear();
for (Contact contact : ContactsAddedInList) {
selectionModel.setSelected(contact, true);
}
}
});


// Add it to the root panel.
RootPanel.get().add(table);
RootPanel.get().add(pager);
RootPanel.get().add(addToListButton);
RootPanel.get().add(selectAllButton);
RootPanel.get().add(resetButton);


}

private Type<ClickHandler> Type() {
// TODO Auto-generated method stub
return null;
}
}
Reply all
Reply to author
Forward
0 new messages