Class Table
- All Implemented Interfaces:
AttachNotifier,ClickNotifier<Table>,DetachNotifier,HasAriaLabel,HasElement,HasSize,HasStyle,Serializable
<table> element.
Unlike the deprecated NativeTable, this component does not extend
HtmlContainer, so it has no generic
add(Component). A <table> may only contain a
<caption>, <colgroup>, <thead>, <tbody> and
<tfoot>, and those are reached through the accessors below, which
also keep them in the order the WHATWG HTML specification requires.
Most code never has to name a section. addRow(),
addRowWithHeader(String, String...), addHeaderRow() and
addFooterRow() create the enclosing <tbody>, <thead>
or <tfoot> on demand, and getHeaderRows(),
getBodyRows(), getFooterRows(), getAllRows() and
removeRow(TableRow) read and remove rows without one either. This
mirrors the DOM, where inserting a row into a table materialises the missing
row group by itself. Reach for getHead() and friends only when you
need to address a section as a whole ? to style it, or to hand it to
bindChildren.
When InitParameters.THEMED_HTML is enabled,
the Aura and Lumo themes apply their styles to the native HTML elements of
the application, and so to a table. A single element is left to the browser
with addClassName(Constants.UNTHEMED_HTML_CLASS_NAME), or styled on
its own, without the property, with
addClassName(Constants.THEMED_HTML_CLASS_NAME); see
Constants.
- Since:
- 25.3
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBody()Appends a new<tbody>to this table, after any bodies it already has and before the<tfoot>.Appends the given<tbody>to this table, after any bodies it already has and before the<tfoot>.addCaption(Component... components) Appends the given components to this table's caption, creating it if none exists yet.addCaption(List<? extends Component> components) List equivalent ofaddCaption(Component...).Appends a new empty<colgroup>to this table, after the caption and any column groups it already has, and before the<thead>.addColumnGroup(TableColumn... columns) Appends a new<colgroup>holding the given columns to this table.addColumnGroup(TableColumnGroup group) Appends the given<colgroup>to this table.addColumnGroup(List<? extends TableColumn> columns) List equivalent ofaddColumnGroup(TableColumn...).Appends a new empty row to this table's<tfoot>, creating it if the table has none.addFooterRow(String... cellTexts) Appends a row of data cells with the given texts to this table's<tfoot>.addFooterRow(List<String> cellTexts) List equivalent ofaddFooterRow(String...).voidaddFooterRows(TableRow... rows) Appends the given rows to this table's<tfoot>.voidaddFooterRows(List<? extends TableRow> rows) List equivalent ofaddFooterRows(TableRow...).Appends a new empty row to this table's<thead>, creating it if the table has none.addHeaderRow(String... cellTexts) Appends a row of header cells with the given texts to this table's<thead>.addHeaderRow(List<String> cellTexts) List equivalent ofaddHeaderRow(String...).voidaddHeaderRows(TableRow... rows) Appends the given rows to this table's<thead>.voidaddHeaderRows(List<? extends TableRow> rows) List equivalent ofaddHeaderRows(TableRow...).addRow()Appends a new empty row to this table's<tbody>, creating the body if the table has none.Appends a row of data cells with the given texts to this table's<tbody>.List equivalent ofaddRow(String...).voidAppends the given rows to this table's first<tbody>, creating the body if the table has none.voidList equivalent ofaddRows(TableRow...).addRowWithHeader(String header, String... cellTexts) Appends a row led by a header cell labelling it, followed by data cells with the given texts.addRowWithHeader(String header, List<String> cellTexts) List equivalent ofaddRowWithHeader(String, String...).Returns every row of this table, walking its sections in document order: the<thead>rows, then the rows of each<tbody>, then the<tfoot>rows.Returns the<tbody>elements of this table, in document order.getBody()Returns the first<tbody>of this table, creating one if the table has none.Returns the rows of this table's<tbody>elements, in document order, or an empty list if it has none.Returns this table's caption, creating one if the table has none.@Nullable StringReturns the text content of this table's caption, ornullif the table has no caption or the caption holds something other than text.Returns the column groups of this table, in document order.getFoot()Returns this table's<tfoot>, creating one if the table has none.Returns the rows of this table's<tfoot>, or an empty list if it has none.getHead()Returns this table's<thead>, creating one if the table has none.Returns the rows of this table's<thead>, or an empty list if it has none.booleanReports whether this table already has a<caption>.booleanhasFoot()Reports whether this table already has a<tfoot>.booleanhasHead()Reports whether this table already has a<thead>.voidRemoves every row from this table, leaving its sections in place.voidremoveBody(TableBody body) Removes the given<tbody>from this table.voidRemoves this table's caption, if it has one.voidRemoves the given column group from this table.voidRemoves this table's<tfoot>, if it has one.voidRemoves this table's<thead>, if it has one.voidRemoves the given row from whichever section of this table holds it, so that a caller who added the row throughaddRow()or one of its siblings does not have to know which section that was.voidsetCaption(@Nullable TableCaption caption) Puts the given caption on this table, replacing the one it already has, if any.voidsetCaptionText(@Nullable String text) Sets the text content of this table's caption, creating the caption if the table has none.voidPuts the given<tfoot>on this table, replacing the one it already has, if any.voidPuts the given<thead>on this table, replacing the one it already has, if any.Methods inherited from class com.vaadin.flow.component.HtmlComponent
getTitle, setTitleMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttachedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.ClickNotifier
addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy, setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
Table
public Table()Creates a new empty table.
-
-
Method Details
-
getCaption
Returns this table's caption, creating one if the table has none. Reading through this accessor therefore has a side effect; usehasCaption()orgetCaptionText()to inspect the table without adding a caption to it.- Returns:
- the table's
<caption>.
-
hasCaption
public boolean hasCaption()Reports whether this table already has a<caption>. UnlikegetCaption(), this does not create one.- Returns:
trueif the table has a caption.
-
getCaptionText
Returns the text content of this table's caption, ornullif the table has no caption or the caption holds something other than text. UnlikegetCaption(), this does not create one.A caption assembled from components has no faithful text representation, and reporting whichever text nodes happen to sit around those components would be misleading, so
nullis returned instead. UsehasCaption()to tell a table with no caption from one whose caption holds markup.- Returns:
- the caption text, or
nullif there is none to report.
-
setCaptionText
Sets the text content of this table's caption, creating the caption if the table has none.Passing
nullremoves the caption, assetCaption(TableCaption)does, so that whatevergetCaptionText()reports can be handed straight back.- Parameters:
text- the caption text, ornullto remove the caption.
-
setCaption
Puts the given caption on this table, replacing the one it already has, if any.- Parameters:
caption- the caption to use, ornullto remove the one the table has.
-
addCaption
Appends the given components to this table's caption, creating it if none exists yet. Useful for richer captions containing inline markup.- Parameters:
components- the components to append.- Returns:
- the caption.
-
addCaption
List equivalent ofaddCaption(Component...).- Parameters:
components- the components to append.- Returns:
- the caption.
-
removeCaption
public void removeCaption()Removes this table's caption, if it has one. -
addColumnGroup
Appends a new empty<colgroup>to this table, after the caption and any column groups it already has, and before the<thead>.- Returns:
- the new column group.
-
addColumnGroup
Appends the given<colgroup>to this table.- Parameters:
group- the column group to add.- Returns:
- the given group, for chaining.
-
addColumnGroup
Appends a new<colgroup>holding the given columns to this table.- Parameters:
columns- the columns the new group should hold.- Returns:
- the new column group.
-
addColumnGroup
List equivalent ofaddColumnGroup(TableColumn...).- Parameters:
columns- the columns the new group should hold.- Returns:
- the new column group.
-
getColumnGroups
Returns the column groups of this table, in document order.- Returns:
- the table's
<colgroup>elements.
-
removeColumnGroup
Removes the given column group from this table.- Parameters:
group- the column group to remove.
-
getHead
Returns this table's<thead>, creating one if the table has none. Reading through this accessor therefore has a side effect; usehasHead()orgetHeaderRows()to inspect the table without adding a section to it.- Returns:
- the table's
<thead>.
-
hasHead
public boolean hasHead()Reports whether this table already has a<thead>. UnlikegetHead(), this does not create one.- Returns:
trueif the table has a head.
-
setHead
Puts the given<thead>on this table, replacing the one it already has, if any.- Parameters:
head- the head to use, ornullto remove the one the table has.
-
removeHead
public void removeHead()Removes this table's<thead>, if it has one. -
getFoot
Returns this table's<tfoot>, creating one if the table has none. Reading through this accessor therefore has a side effect; usehasFoot()orgetFooterRows()to inspect the table without adding a section to it.- Returns:
- the table's
<tfoot>.
-
hasFoot
public boolean hasFoot()Reports whether this table already has a<tfoot>. UnlikegetFoot(), this does not create one.- Returns:
trueif the table has a foot.
-
setFoot
Puts the given<tfoot>on this table, replacing the one it already has, if any.- Parameters:
foot- the foot to use, ornullto remove the one the table has.
-
removeFoot
public void removeFoot()Removes this table's<tfoot>, if it has one. -
getBodies
Returns the<tbody>elements of this table, in document order. A table may have several.- Returns:
- the table's bodies.
-
getBody
Returns the first<tbody>of this table, creating one if the table has none. Reading through this accessor therefore has a side effect; usegetBodies()orgetBodyRows()to inspect the table without adding a body to it.- Returns:
- the table's first body.
-
addBody
Appends a new<tbody>to this table, after any bodies it already has and before the<tfoot>.- Returns:
- the new body.
-
addBody
Appends the given<tbody>to this table, after any bodies it already has and before the<tfoot>.- Parameters:
body- the body to add.- Returns:
- the given body, for chaining.
-
removeBody
Removes the given<tbody>from this table.- Parameters:
body- the body to remove.
-
getAllRows
Returns every row of this table, walking its sections in document order: the<thead>rows, then the rows of each<tbody>, then the<tfoot>rows.The name says
Allbecause this is the only row accessor that crosses section kinds.getHeaderRows(),getBodyRows()andgetFooterRows()each stay within one kind, thoughgetBodyRowsdoes flatten several<tbody>elements when the table has them, andTableRowContainer.getRows(),TableRowContainer.getRows()andTableRowContainer.getRows()each return the rows of one container.addRow()appends to the body.- Returns:
- all the rows of this table.
-
getHeaderRows
Returns the rows of this table's<thead>, or an empty list if it has none. UnlikegetHead(), this does not create the section, so it is safe to call while only reading the table ? from a debugger watch, for instance.- Returns:
- the head's rows, in document order.
-
getBodyRows
Returns the rows of this table's<tbody>elements, in document order, or an empty list if it has none. UnlikegetBody(), this does not create a body.- Returns:
- the rows of every body, in document order.
-
removeRow
Removes the given row from whichever section of this table holds it, so that a caller who added the row throughaddRow()or one of its siblings does not have to know which section that was.- Parameters:
row- the row to remove.- Throws:
IllegalArgumentException- if the row is not in any section of this table.
-
removeAllRows
public void removeAllRows()Removes every row from this table, leaving its sections in place. -
addRow
Appends a new empty row to this table's<tbody>, creating the body if the table has none. This mirrors what a browser does with a<tr>written straight inside a<table>. UseaddHeaderRow()oraddFooterRow()for the other sections; note thatgetAllRows()spans all three.A table may have several
<tbody>elements. This one appends to the first, the same onegetBody()returns; to append to another, callTableRowContainer.addRow()on the body you mean, which is whataddBody()hands back.- Returns:
- the new row.
-
addRow
Appends a row of data cells with the given texts to this table's<tbody>.For example,
addRow("Mercury", "0.330")renders as:<tbody> <tr> <td>Mercury</td> <td>0.330</td> </tr> </tbody>- Parameters:
cellTexts- the text content for each data cell.- Returns:
- the new row.
-
addRow
List equivalent ofaddRow(String...).- Parameters:
cellTexts- the text content for each data cell.- Returns:
- the new row.
-
addRowWithHeader
Appends a row led by a header cell labelling it, followed by data cells with the given texts. The leading cell is a<th scope="row">, which is what lets assistive technology announce the right label for each data cell in the row.For example,
addRowWithHeader("Venus", "4.87", "12,104")renders as:<tbody> <tr> <th scope="row">Venus</th> <td>4.87</td> <td>12,104</td> </tr> </tbody>- Parameters:
header- the text of the leading header cell.cellTexts- the text content for each data cell after it.- Returns:
- the new row.
-
addRowWithHeader
List equivalent ofaddRowWithHeader(String, String...).- Parameters:
header- the text of the leading header cell.cellTexts- the text content for each data cell after it.- Returns:
- the new row.
-
addRows
Appends the given rows to this table's first<tbody>, creating the body if the table has none. As withaddRow(), a table with several bodies gets the rows in the first of them.- Parameters:
rows- the rows to add.
-
addRows
List equivalent ofaddRows(TableRow...).- Parameters:
rows- the rows to add.
-
addHeaderRow
Appends a new empty row to this table's<thead>, creating it if the table has none.- Returns:
- the new row.
-
addHeaderRow
Appends a row of header cells with the given texts to this table's<thead>.For example,
addHeaderRow("Name", "Mass")renders as:<thead> <tr> <th scope="col">Name</th> <th scope="col">Mass</th> </tr> </thead>- Parameters:
cellTexts- the text content for each header cell.- Returns:
- the new row.
-
addHeaderRow
List equivalent ofaddHeaderRow(String...).- Parameters:
cellTexts- the text content for each header cell.- Returns:
- the new row.
-
addHeaderRows
Appends the given rows to this table's<thead>.- Parameters:
rows- the rows to add.
-
addHeaderRows
List equivalent ofaddHeaderRows(TableRow...).- Parameters:
rows- the rows to add.
-