Class DataGrid<T>
- Type Parameters:
T- the data type of each row
- All Implemented Interfaces:
HasAttachHandlers,HasHandlers,HasKeyboardPagingPolicy,HasKeyboardSelectionPolicy,EventListener,Focusable,HasVisibility,IsRenderable,IsWidget,RequiresResize,HasCellPreviewHandlers<T>,HasData<T>,HasKeyProvider<T>,HasRows
Columns
TheColumn class defines the
Cell used to render a column. Implement
Column.getValue(Object) to retrieve the field value from the row
object that will be rendered in the Cell.
Headers and Footers
AHeader can be placed at the top
(header) or bottom (footer) of the DataGrid. You can specify a header
as text using AbstractCellTable.addColumn(Column, String), or you can create a custom
Header that can change with the value of the cells, such as a column
total. The Header will be rendered every time the row data changes or
the table is redrawn. If you pass the same header instance (==) into adjacent
columns, the header will span the columns.
Examples
- Trivial example
public class CellTableExample 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("John", new Date(80, 4, 12), "123 Fourth Avenue"), new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"), new Contact("George", new Date(46, 6, 6), "1600 Pennsylvania Avenue")); public void onModuleLoad() { // Create a CellTable. CellTable<Contact> table = new CellTable<Contact>(); table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED); // 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"); // Add a selection model to handle user selection. final SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>(); table.setSelectionModel(selectionModel); selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { Contact selected = selectionModel.getSelectedObject(); if (selected != null) { Window.alert("You selected: " + selected.name); } } }); // 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); // Push the data into the widget. table.setRowData(0, CONTACTS); // Add it to the root panel. RootPanel.get().add(table); } }- FieldUpdater example
public class CellTableFieldUpdaterExample implements EntryPoint { /** * A simple data type that represents a contact with a unique ID. */ private static class Contact { private static int nextId = 0; private final int id; private String name; public Contact(String name) { nextId++; this.id = nextId; this.name = name; } } /** * The list of data to display. */ private static final List<Contact> CONTACTS = Arrays.asList(new Contact("John"), new Contact( "Joe"), new Contact("George")); /** * The key provider that allows us to identify Contacts even if a field * changes. We identify contacts by their unique ID. */ private static final ProvidesKey<Contact> KEY_PROVIDER = new ProvidesKey<CellTableFieldUpdaterExample.Contact>() { @Override public Object getKey(Contact item) { return item.id; } }; @Override public void onModuleLoad() { // Create a CellTable with a key provider. final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER); // Add a text input column to edit the name. final TextInputCell nameCell = new TextInputCell(); Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) { @Override public String getValue(Contact object) { // Return the name as the value of this column. return object.name; } }; table.addColumn(nameColumn, "Name"); // Add a field updater to be notified when the user enters a new name. nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() { @Override public void update(int index, Contact object, String value) { // Inform the user of the change. Window.alert("You changed the name of " + object.name + " to " + value); // Push the changes into the Contact. At this point, you could send an // asynchronous request to the server to update the database. object.name = value; // Redraw the table with the new data. table.redraw(); } }); // Push the data into the widget. table.setRowData(CONTACTS); // Add it to the root panel. RootPanel.get().add(table); } }- Key provider example
public class KeyProviderExample implements EntryPoint { /** * A simple data type that represents a contact. */ private static class Contact { private static int nextId = 0; private final int id; private String name; public Contact(String name) { nextId++; this.id = nextId; this.name = name; } } /** * A custom {@link Cell} used to render a {@link Contact}. */ private static class ContactCell extends AbstractCell<Contact> { @Override public void render(Context context, Contact value, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.name); } } } /** * The list of data to display. */ private static final List<Contact> CONTACTS = Arrays.asList(new Contact( "John"), new Contact("Joe"), new Contact("Michael"), new Contact("Sarah"), new Contact("George")); public void onModuleLoad() { /* * Define a key provider for a Contact. We use the unique ID as the key, * which allows to maintain selection even if the name changes. */ ProvidesKey<Contact> keyProvider = new ProvidesKey<Contact>() { public Object getKey(Contact item) { // Always do a null check. return (item == null) ? null : item.id; } }; // Create a CellList using the keyProvider. CellList<Contact> cellList = new CellList<Contact>(new ContactCell(), keyProvider); // Push data into the CellList. cellList.setRowCount(CONTACTS.size(), true); cellList.setRowData(0, CONTACTS); // Add a selection model using the same keyProvider. SelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>( keyProvider); cellList.setSelectionModel(selectionModel); /* * Select a contact. The selectionModel will select based on the ID because * we used a keyProvider. */ Contact sarah = CONTACTS.get(3); selectionModel.setSelected(sarah, true); // Modify the name of the contact. sarah.name = "Sara"; /* * Redraw the CellList. Sarah/Sara will still be selected because we * identify her by ID. If we did not use a keyProvider, Sara would not be * selected. */ cellList.redraw(); // Add the widgets to the root panel. RootPanel.get().add(cellList); } }
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA ClientBundle that provides images for this widget.static interfaceStyles used by this widget.(package private) static classA simple widget wrapper around a table element.Nested classes/interfaces inherited from class com.google.gwt.user.cellview.client.AbstractCellTable
AbstractCellTable.CellTableKeyboardSelectionHandler<T>, AbstractCellTable.TableSectionChangeHandler, AbstractCellTable.TemplateNested classes/interfaces inherited from class com.google.gwt.user.cellview.client.AbstractHasData
AbstractHasData.DefaultKeyboardSelectionHandler<T>, AbstractHasData.RedrawEventNested classes/interfaces inherited from class com.google.gwt.user.client.ui.UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabledNested classes/interfaces inherited from interface com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy
HasKeyboardPagingPolicy.KeyboardPagingPolicyNested classes/interfaces inherited from interface com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy
HasKeyboardSelectionPolicy.KeyboardSelectionPolicy -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final DataGrid.TableWidget(package private) final DataGrid.TableWidget(package private) final DataGrid.TableWidgetFields inherited from class com.google.gwt.user.cellview.client.AbstractHasData
isFocusedFields inherited from class com.google.gwt.user.client.ui.UIObject
DEBUG_ID_PREFIX -
Constructor Summary
ConstructorsConstructorDescriptionDataGrid()Constructs a table with a default page size of 50.DataGrid(int pageSize) Constructs a table with the given page size.DataGrid(int pageSize, DataGrid.Resources resources) Constructs a table with the given page size with the specifiedDataGrid.Resources.DataGrid(int pageSize, DataGrid.Resources resources, ProvidesKey<T> keyProvider) Constructs a table with the given page size, the specifiedDataGrid.Resources, and the given key provider.DataGrid(int pageSize, DataGrid.Resources resources, ProvidesKey<T> keyProvider, Widget loadingIndicator) Constructs a table with the given page size, the specifiedDataGrid.Resources, and the given key provider.DataGrid(int pageSize, ProvidesKey<T> keyProvider) Constructs a table with the given page size and the givenkey provider.DataGrid(ProvidesKey<T> keyProvider) Constructs a table with a default page size of 50, and the givenkey provider. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddColumnStyleName(int index, String styleName) Add a style name to thecolelement at the specified index, creating it if necessary.voidClear the width of the tables in this widget, which causes them to fill the available width.protected voiddoSetColumnWidth(int column, String width) Set the width of a column.protected voiddoSetHeaderVisible(boolean isFooter, boolean isVisible) Show or hide a header section.protected TableSectionElementGet the tbody element that contains the render row values.protected TableSectionElementGet the tfoot element that contains the footers.protected TableSectionElementGet the thead element that contains the headers.protected voidCalled when the loading state changes.voidonResize()This method must be called whenever the implementor's size has been modified.protected voidvoidremoveColumnStyleName(int index, String styleName) Remove a style from thecolelement at the specified index.voidsetAlwaysShowScrollBars(boolean alwaysShowScrollBars) Sets whether this grid always shows its scroll bars, or only when necessary.voidsetEmptyTableWidget(Widget widget) Set the widget to display when the table has no rows.voidsetLoadingIndicator(Widget widget) Set the widget to display when the data is loading.voidsetMinimumTableWidth(double value, Style.Unit unit) Set the minimum width of the tables in this widget.voidsetTableWidth(double value, Style.Unit unit) Set the width of the tables in this widget.Methods inherited from class com.google.gwt.user.cellview.client.AbstractCellTable
addColumn, addColumn, addColumn, addColumn, addColumn, addColumn, addColumn, addColumnSortHandler, addRowHoverHandler, clearColumnWidth, clearColumnWidth, convertToElements, dependsOnSelection, flush, getChildContainer, getChildElement, getColumn, getColumnCount, getColumnIndex, getColumnSortList, getColumnWidth, getColumnWidth, getEmptyTableWidget, getFooter, getFooterBuilder, getHeader, getHeaderBuilder, getKeyboardSelectedColumn, getKeyboardSelectedElement, getKeyboardSelectedSubRow, getLoadingIndicator, getRealColumnCount, getResources, getRowElement, getRowStyles, getSubRowElement, insertColumn, insertColumn, insertColumn, insertColumn, insertColumn, insertColumn, insertColumn, isAutoFooterRefreshDisabled, isAutoHeaderRefreshDisabled, isKeyboardNavigationSuppressed, isSkipRowHoverCheck, isSkipRowHoverFloatElementCheck, isSkipRowHoverStyleUpdate, onBlur, onBrowserEvent2, onFocus, redrawFooters, redrawHeaders, removeColumn, removeColumn, renderRowValues, renderRowValuesLegacy, replaceAllChildren, replaceChildren, resetFocusOnCell, setAutoFooterRefreshDisabled, setAutoHeaderRefreshDisabled, setColumnWidth, setColumnWidth, setColumnWidth, setColumnWidth, setFooterBuilder, setHeaderBuilder, setKeyboardSelected, setKeyboardSelectedColumn, setKeyboardSelectedColumn, setKeyboardSelectedRow, setKeyboardSelectedRow, setRowStyles, setSkipRowHoverCheck, setSkipRowHoverFloatElementCheck, setSkipRowHoverStyleUpdate, setTableBuilderMethods inherited from class com.google.gwt.user.cellview.client.AbstractHasData
addCellPreviewHandler, addLoadingStateChangeHandler, addRangeChangeHandler, addRedrawHandler, addRowCountChangeHandler, addValueChangeHandler, adopt, cellConsumesEventType, checkRowBounds, convertToElements, doAttach, doDetach, getAccessKey, getDisplayedItem, getDisplayedItems, getKeyboardPagingPolicy, getKeyboardSelectedRow, getKeyboardSelectionPolicy, getKeyProvider, getPageSize, getPageStart, getPresenter, getRowContainer, getRowCount, getSelectionModel, getTabIndex, getValueKey, getVisibleItem, getVisibleItemCount, getVisibleItems, getVisibleRange, isRowCountExact, isRowWithinBounds, onBrowserEvent, onUnload, redraw, redrawRow, replaceAllChildren, replaceChildren, setAccessKey, setFocus, setFocusable, setKeyboardPagingPolicy, setKeyboardSelectedRow, setKeyboardSelectionHandler, setKeyboardSelectionPolicy, setPageSize, setPageStart, setRowCount, setRowCount, setRowData, setRowData, setSelected, setSelectionModel, setSelectionModel, setTabIndex, setVisibleRange, setVisibleRange, setVisibleRangeAndClearData, showOrHideMethods inherited from class com.google.gwt.user.client.ui.Composite
claimElement, getWidget, initializeClaimedElement, initWidget, isAttached, onAttach, onDetach, render, render, resolvePotentialElement, setWidgetMethods inherited from class com.google.gwt.user.client.ui.Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, doAttachChildren, doDetachChildren, fireEvent, getHandlerCount, getLayoutData, getParent, isOrWasAttached, onLoad, removeFromParent, setLayoutData, sinkEvents, unsinkEventsMethods inherited from class com.google.gwt.user.client.ui.UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, onEnsureDebugId, removeStyleDependentName, removeStyleName, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.google.gwt.event.shared.HasHandlers
fireEvent
-
Field Details
-
tableData
-
tableHeader
-
-
Constructor Details
-
DataGrid
public DataGrid()Constructs a table with a default page size of 50. -
DataGrid
public DataGrid(int pageSize) Constructs a table with the given page size.- Parameters:
pageSize- the page size
-
DataGrid
Constructs a table with the given page size and the givenkey provider.- Parameters:
pageSize- the page sizekeyProvider- an instance of ProvidesKey, or null if the record object should act as its own key
-
DataGrid
Constructs a table with the given page size with the specifiedDataGrid.Resources.- Parameters:
pageSize- the page sizeresources- the resources to use for this widget
-
DataGrid
Constructs a table with the given page size, the specifiedDataGrid.Resources, and the given key provider.- Parameters:
pageSize- the page sizeresources- the resources to use for this widgetkeyProvider- an instance of ProvidesKey, or null if the record object should act as its own key
-
DataGrid
public DataGrid(int pageSize, DataGrid.Resources resources, ProvidesKey<T> keyProvider, Widget loadingIndicator) Constructs a table with the given page size, the specifiedDataGrid.Resources, and the given key provider.- Parameters:
pageSize- the page sizeresources- the resources to use for this widgetkeyProvider- an instance of ProvidesKey, or null if the record object should act as its own key loadingIndicator- the widget to use as a loading indicator, or null to disable
-
DataGrid
Constructs a table with a default page size of 50, and the givenkey provider.- Parameters:
keyProvider- an instance of ProvidesKey, or null if the record object should act as its own key
-
-
Method Details
-
addColumnStyleName
Description copied from class:AbstractCellTableAdd a style name to thecolelement at the specified index, creating it if necessary.- Specified by:
addColumnStyleNamein classAbstractCellTable<T>- Parameters:
index- the column indexstyleName- the style name to add
-
clearTableWidth
public void clearTableWidth()Clear the width of the tables in this widget, which causes them to fill the available width.The table width is not the same as the width of this widget. If the tables are narrower than this widget, there will be a gap between the table and the edge of the widget. If the tables are wider than this widget, a horizontal scrollbar will appear so the user can scroll horizontally.
-
onResize
public void onResize()Description copied from interface:RequiresResizeThis method must be called whenever the implementor's size has been modified.- Specified by:
onResizein interfaceRequiresResize
-
removeColumnStyleName
Description copied from class:AbstractCellTableRemove a style from thecolelement at the specified index.- Specified by:
removeColumnStyleNamein classAbstractCellTable<T>- Parameters:
index- the column indexstyleName- the style name to remove
-
setAlwaysShowScrollBars
public void setAlwaysShowScrollBars(boolean alwaysShowScrollBars) Sets whether this grid always shows its scroll bars, or only when necessary.- Parameters:
alwaysShowScrollBars-trueto show scroll bars at all times
-
setEmptyTableWidget
Description copied from class:AbstractCellTableSet the widget to display when the table has no rows.- Overrides:
setEmptyTableWidgetin classAbstractCellTable<T>- Parameters:
widget- the empty table widget, or null to disable
-
setLoadingIndicator
Description copied from class:AbstractCellTableSet the widget to display when the data is loading.- Overrides:
setLoadingIndicatorin classAbstractCellTable<T>- Parameters:
widget- the loading indicator, or null to disable
-
setMinimumTableWidth
Set the minimum width of the tables in this widget. If the widget become narrower than the minimum width, a horizontal scrollbar will appear so the user can scroll horizontally.Note that this method is not supported in IE6 and earlier versions of IE.
- Parameters:
value- the widthunit- the unit of the width- See Also:
-
setTableWidth
Set the width of the tables in this widget. By default, the width is not set and the tables take the available width.The table width is not the same as the width of this widget. If the tables are narrower than this widget, there will be a gap between the table and the edge of the widget. If the tables are wider than this widget, a horizontal scrollbar will appear so the user can scroll horizontally.
If your table has many columns and you want to ensure that the columns are not truncated, you probably want to use
setMinimumTableWidth(double, Unit)instead. That will ensure that the table is wide enough, but it will still allow the table to expand to 100% if the user has a wide screen.Note that setting the width in percentages will not work on older versions of IE because it does not account for scrollbars when calculating the width.
- Parameters:
value- the widthunit- the unit of the width- See Also:
-
doSetColumnWidth
Description copied from class:AbstractCellTableSet the width of a column.- Specified by:
doSetColumnWidthin classAbstractCellTable<T>- Parameters:
column- the column indexwidth- the width, or null to clear the width
-
doSetHeaderVisible
protected void doSetHeaderVisible(boolean isFooter, boolean isVisible) Description copied from class:AbstractCellTableShow or hide a header section.- Specified by:
doSetHeaderVisiblein classAbstractCellTable<T>- Parameters:
isFooter- true for the footer, false for the headerisVisible- true to show, false to hide
-
getTableBodyElement
Description copied from class:AbstractCellTableGet the tbody element that contains the render row values.- Specified by:
getTableBodyElementin classAbstractCellTable<T>
-
getTableFootElement
Description copied from class:AbstractCellTableGet the tfoot element that contains the footers.- Specified by:
getTableFootElementin classAbstractCellTable<T>
-
getTableHeadElement
Description copied from class:AbstractCellTableGet the thead element that contains the headers.- Specified by:
getTableHeadElementin classAbstractCellTable<T>
-
onLoadingStateChanged
Called when the loading state changes.- Overrides:
onLoadingStateChangedin classAbstractHasData<T>- Parameters:
state- the new loading state
-
refreshColumnWidths
protected void refreshColumnWidths()- Overrides:
refreshColumnWidthsin classAbstractCellTable<T>
-