How to add table headers to GWT's tables

260 views
Skip to first unread message

Madox

unread,
Jul 7, 2006, 9:55:34 AM7/7/06
to Google Web Toolkit
You can use GWT's DOM class to add table headers to GWT tables.
Here is how I do:

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Grid;

public class MyGrid extends Grid {
public MyGrid(int i, int j) {
super(i,j);

// use DOM to create thead element....
Element thead = DOM.createElement("thead");
Element tr = DOM.createTR();

// add columns
DOM.appendChild(thead,tr);
for (int k = 0;k<j;k++) {
Element th = DOM.createTH();
DOM.appendChild(tr,th);
// add some text to the header...
DOM.setInnerText(th,"Col#"+k);
}

// get the table element
Element table = this.getElement();

// and add the thead before the tbody
DOM.insertChild(table,thead,0);

}
}

wylp...@gmail.com

unread,
Jul 13, 2006, 9:59:27 PM7/13/06
to Google Web Toolkit
“header” can clicklistener
envent?(“header”的单击事件怎么得到呢?)

swecha

unread,
Jul 14, 2006, 1:45:49 AM7/14/06
to Google Web Toolkit
Another way to add headers in a Grid..

class MyGrid extends Grid {

MyGrid(int row, int col) {
super(row, col);
}

void clearRows() {
resizeRows(1);
}

void insertHeader(String[] fields, String[] styles) {
for (int col = 0; col < fields.length; ++col) {
setText(0, col, fields[col]);
getCellFormatter().setStyleName(0, col, styles[col]);
}
}

void insertData(String[] fields) {
resizeRows(getRowCount()+1);
for (int col = 0; col < fields.length; ++col) {
setText(getRowCount()-1, col, fields[col]);
}
}

//The following method is to fix the Grid Bug.
public void resizeRows(int rows) {
if (numRows == rows)
return;

if (rows <= 0) {
throw new IndexOutOfBoundsException("Cannot set number of rows to"
+ rows);
}

while (numRows < rows) {
// More rows. Add them where necessary.
insertRow(numRows);
insertCells(numRows, 0, numColumns);
++numRows;
}

while (numRows > rows) {
// Fewer rows. Remove extraneous ones.
removeRow(--numRows);
.}
.}
.}.

djsch...@mitre.org

unread,
Jul 14, 2006, 8:15:53 AM7/14/06
to Google Web Toolkit
I don't now about the first implementation, but a problem with the
second implementation is that if the grid becomes scrollable, row 0
does not stay locked while you scroll your data. So as soon as you
scroll 1 row down, the column headers are no longer visible.

Madox

unread,
Jul 14, 2006, 8:22:43 AM7/14/06
to Google Web Toolkit
My implementation mantains header visibility if grid body becomes
scrollable (that's why I used 'thead' element)
Reply all
Reply to author
Forward
0 new messages