Reputation: 123
How to create this simplest table in GWT FlexTable?
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>
The documentation of FlexTable it's not clear about creating tables with thead
and tbody
. Anyone could help? Thank in advance.
Upvotes: 1
Views: 958
Reputation: 5599
If you want to keep the flexibility of FlexTable
and need to add a thead
element, you can achieve it by manipulating TableElement
. It is quite low-level, but id does what you need.
Here is how to get a DOM structure given in your example:
FlexTable table = new FlexTable();
table.setText(0, 0, "January");
table.setText(0, 1, "$100");
table.setText(1, 0, "February");
table.setText(1, 1, "$80");
com.google.gwt.user.client.Element oldElement = table.getElement();
com.google.gwt.dom.client.Element element = (com.google.gwt.dom.client.Element) oldElement;
TableElement tableElement = (TableElement) element;
TableSectionElement tHead = tableElement.createTHead();
TableRowElement row = tHead.insertRow(0);
row.insertCell(0).setInnerText("Month");
row.insertCell(1).setInnerText("Savings");
RootPanel.get().add(table);
Note that table.getElement();
returns deprecated com.google.gwt.user.client.Element
. It extends com.google.gwt.dom.client.Element
and so does TableElement
. Once all casts are done you can add rows and cells to thead
.
Here is the result (tested with GWT 2.7 on Chrome):
Upvotes: 1