Class Table

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.html.Table
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.ClickNotifier<Table>, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasAriaLabel, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, Serializable

@NullMarked @Tag("table") public class Table extends com.vaadin.flow.component.HtmlComponent implements com.vaadin.flow.component.ClickNotifier<Table>, com.vaadin.flow.component.HasAriaLabel
Component representing a <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.

Since:
25.3
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty table.
  • Method Summary

    Modifier and Type
    Method
    Description
    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(com.vaadin.flow.component.Component... components)
    Appends the given components to this table's caption, creating it if none exists yet.
    addCaption(List<? extends com.vaadin.flow.component.Component> components)
    List equivalent of addCaption(Component...).
    Appends a new empty <colgroup> to this table, after the caption and any column groups it already has, and before the <thead>.
    Appends a new <colgroup> holding the given columns to this table.
    Appends the given <colgroup> to this table.
    addColumnGroup(List<? extends TableColumn> columns)
    List equivalent of addColumnGroup(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 of addFooterRow(String...).
    void
    Appends the given rows to this table's <tfoot>.
    void
    addFooterRows(List<? extends TableRow> rows)
    List equivalent of addFooterRows(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 of addHeaderRow(String...).
    void
    Appends the given rows to this table's <thead>.
    void
    addHeaderRows(List<? extends TableRow> rows)
    List equivalent of addHeaderRows(TableRow...).
    Appends a new empty row to this table's <tbody>, creating the body if the table has none.
    addRow(String... cellTexts)
    Appends a row of data cells with the given texts to this table's <tbody>.
    addRow(List<String> cellTexts)
    List equivalent of addRow(String...).
    void
    addRows(TableRow... rows)
    Appends the given rows to this table's <tbody>.
    void
    addRows(List<? extends TableRow> rows)
    List equivalent of addRows(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)
    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.
    Returns the first <tbody> of this table, creating one if the table has none.
    Returns this table's caption, creating one if the table has none.
    @Nullable String
    Returns the text content of this table's caption, or null if the table has no caption or the caption holds something other than text.
    Returns the column groups of this table, in document order.
    Returns this table's <tfoot>, creating one if the table has none.
    Returns this table's <thead>, creating one if the table has none.
    boolean
    Reports whether this table already has a <caption>.
    boolean
    Reports whether this table already has a <tfoot>.
    boolean
    Reports whether this table already has a <thead>.
    void
    Removes every row from this table, leaving its sections in place.
    void
    Removes the given <tbody> from this table.
    void
    Removes this table's caption, if it has one.
    void
    Removes the given column group from this table.
    void
    Removes this table's <tfoot>, if it has one.
    void
    Removes this table's <thead>, if it has one.
    void
    setCaption(@Nullable TableCaption caption)
    Puts the given caption on this table, replacing the one it already has, if any.
    void
    setCaptionText(@Nullable String text)
    Sets the text content of this table's caption, creating the caption if the table has none.
    void
    setFoot(@Nullable TableFoot foot)
    Puts the given <tfoot> on this table, replacing the one it already has, if any.
    void
    setHead(@Nullable TableHead head)
    Puts the given <thead> on this table, replacing the one it already has, if any.

    Methods inherited from class com.vaadin.flow.component.HtmlComponent

    getTitle, setTitle

    Methods 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, whenAttached

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.ClickNotifier

    addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasAriaLabel

    getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy, setAriaLabelledBy

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods 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, setWidthFull

    Methods 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

      public TableCaption getCaption()
      Returns this table's caption, creating one if the table has none.
      Returns:
      the table's <caption>.
    • hasCaption

      public boolean hasCaption()
      Reports whether this table already has a <caption>. Unlike getCaption(), this does not create one.
      Returns:
      true if the table has a caption.
    • getCaptionText

      public @Nullable String getCaptionText()
      Returns the text content of this table's caption, or null if the table has no caption or the caption holds something other than text. Unlike getCaption(), 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 null is returned instead. Use hasCaption() to tell a table with no caption from one whose caption holds markup.

      Returns:
      the caption text, or null if there is none to report.
    • setCaptionText

      public void setCaptionText(@Nullable String text)
      Sets the text content of this table's caption, creating the caption if the table has none.

      Passing null removes the caption, as setCaption(TableCaption) does, so that whatever getCaptionText() reports can be handed straight back.

      Parameters:
      text - the caption text, or null to remove the caption.
    • setCaption

      public void setCaption(@Nullable TableCaption caption)
      Puts the given caption on this table, replacing the one it already has, if any.
      Parameters:
      caption - the caption to use, or null to remove the one the table has.
    • addCaption

      public TableCaption addCaption(com.vaadin.flow.component.Component... components)
      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

      public TableCaption addCaption(List<? extends com.vaadin.flow.component.Component> components)
      List equivalent of addCaption(Component...).
      Parameters:
      components - the components to append.
      Returns:
      the caption.
    • removeCaption

      public void removeCaption()
      Removes this table's caption, if it has one.
    • addColumnGroup

      public TableColumnGroup 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

      public TableColumnGroup addColumnGroup(TableColumnGroup group)
      Appends the given <colgroup> to this table.
      Parameters:
      group - the column group to add.
      Returns:
      the given group, for chaining.
    • addColumnGroup

      public TableColumnGroup addColumnGroup(TableColumn... columns)
      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

      public TableColumnGroup addColumnGroup(List<? extends TableColumn> columns)
      List equivalent of addColumnGroup(TableColumn...).
      Parameters:
      columns - the columns the new group should hold.
      Returns:
      the new column group.
    • getColumnGroups

      public List<TableColumnGroup> getColumnGroups()
      Returns the column groups of this table, in document order.
      Returns:
      the table's <colgroup> elements.
    • removeColumnGroup

      public void removeColumnGroup(TableColumnGroup group)
      Removes the given column group from this table.
      Parameters:
      group - the column group to remove.
    • getHead

      public TableHead getHead()
      Returns this table's <thead>, creating one if the table has none.
      Returns:
      the table's <thead>.
    • hasHead

      public boolean hasHead()
      Reports whether this table already has a <thead>. Unlike getHead(), this does not create one.
      Returns:
      true if the table has a head.
    • setHead

      public void setHead(@Nullable TableHead head)
      Puts the given <thead> on this table, replacing the one it already has, if any.
      Parameters:
      head - the head to use, or null to remove the one the table has.
    • removeHead

      public void removeHead()
      Removes this table's <thead>, if it has one.
    • getFoot

      public TableFoot getFoot()
      Returns this table's <tfoot>, creating one if the table has none.
      Returns:
      the table's <tfoot>.
    • hasFoot

      public boolean hasFoot()
      Reports whether this table already has a <tfoot>. Unlike getFoot(), this does not create one.
      Returns:
      true if the table has a foot.
    • setFoot

      public void setFoot(@Nullable TableFoot foot)
      Puts the given <tfoot> on this table, replacing the one it already has, if any.
      Parameters:
      foot - the foot to use, or null to remove the one the table has.
    • removeFoot

      public void removeFoot()
      Removes this table's <tfoot>, if it has one.
    • getBodies

      public List<TableBody> getBodies()
      Returns the <tbody> elements of this table, in document order. A table may have several.
      Returns:
      the table's bodies.
    • getBody

      public TableBody getBody()
      Returns the first <tbody> of this table, creating one if the table has none.
      Returns:
      the table's first body.
    • addBody

      public TableBody addBody()
      Appends a new <tbody> to this table, after any bodies it already has and before the <tfoot>.
      Returns:
      the new body.
    • addBody

      public TableBody addBody(TableBody body)
      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

      public void removeBody(TableBody body)
      Removes the given <tbody> from this table.
      Parameters:
      body - the body to remove.
    • getAllRows

      public List<TableRow> 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 All because this is the only row accessor that crosses sections: TableRowContainer.getRows(), TableRowContainer.getRows() and TableRowContainer.getRows() each return the rows of one container, and addRow() appends to the body.

      Returns:
      all the rows of this table.
    • removeAllRows

      public void removeAllRows()
      Removes every row from this table, leaving its sections in place.
    • addRow

      public TableRow 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>. Use addHeaderRow() or addFooterRow() for the other sections; note that getAllRows() spans all three.
      Returns:
      the new row.
    • addRow

      public TableRow addRow(String... cellTexts)
      Appends a row of data cells with the given texts to this table's <tbody>.
      Parameters:
      cellTexts - the text content for each data cell.
      Returns:
      the new row.
    • addRow

      public TableRow addRow(List<String> cellTexts)
      List equivalent of addRow(String...).
      Parameters:
      cellTexts - the text content for each data cell.
      Returns:
      the new row.
    • addRowWithHeader

      public TableRow addRowWithHeader(String header, String... cellTexts)
      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.
      Parameters:
      header - the text of the leading header cell.
      cellTexts - the text content for each data cell after it.
      Returns:
      the new row.
    • addRowWithHeader

      public TableRow addRowWithHeader(String header, List<String> cellTexts)
      Parameters:
      header - the text of the leading header cell.
      cellTexts - the text content for each data cell after it.
      Returns:
      the new row.
    • addRows

      public void addRows(TableRow... rows)
      Appends the given rows to this table's <tbody>.
      Parameters:
      rows - the rows to add.
    • addRows

      public void addRows(List<? extends TableRow> rows)
      List equivalent of addRows(TableRow...).
      Parameters:
      rows - the rows to add.
    • addHeaderRow

      public TableRow addHeaderRow()
      Appends a new empty row to this table's <thead>, creating it if the table has none.
      Returns:
      the new row.
    • addHeaderRow

      public TableRow addHeaderRow(String... cellTexts)
      Appends a row of header cells with the given texts to this table's <thead>.
      Parameters:
      cellTexts - the text content for each header cell.
      Returns:
      the new row.
    • addHeaderRow

      public TableRow addHeaderRow(List<String> cellTexts)
      List equivalent of addHeaderRow(String...).
      Parameters:
      cellTexts - the text content for each header cell.
      Returns:
      the new row.
    • addHeaderRows

      public void addHeaderRows(TableRow... rows)
      Appends the given rows to this table's <thead>.
      Parameters:
      rows - the rows to add.
    • addHeaderRows

      public void addHeaderRows(List<? extends TableRow> rows)
      List equivalent of addHeaderRows(TableRow...).
      Parameters:
      rows - the rows to add.
    • addFooterRow

      public TableRow addFooterRow()
      Appends a new empty row to this table's <tfoot>, creating it if the table has none.
      Returns:
      the new row.
    • addFooterRow

      public TableRow addFooterRow(String... cellTexts)
      Appends a row of data cells with the given texts to this table's <tfoot>.
      Parameters:
      cellTexts - the text content for each data cell.
      Returns:
      the new row.
    • addFooterRow

      public TableRow addFooterRow(List<String> cellTexts)
      List equivalent of addFooterRow(String...).
      Parameters:
      cellTexts - the text content for each data cell.
      Returns:
      the new row.
    • addFooterRows

      public void addFooterRows(TableRow... rows)
      Appends the given rows to this table's <tfoot>.
      Parameters:
      rows - the rows to add.
    • addFooterRows

      public void addFooterRows(List<? extends TableRow> rows)
      List equivalent of addFooterRows(TableRow...).
      Parameters:
      rows - the rows to add.