Skip to content

feat: add setHeaders() method to HTMLTable for proper thead support #51

@usernane

Description

@usernane

Problem Statement

Currently, HTMLTable has no built-in way to add a header row with <th> cells. The existing setFirstColCellType('th') changes the first column, not the first row. To create a header row, developers must manually loop through cells, call getCell(0, $c)->setNodeName('th'), and offset all data rows by 1.

Proposed Solution

Add a setHeaders(array $headers) method that:

  1. Creates a <thead> section with a single <tr> containing <th> cells
  2. Accepts an array of header labels (strings or HTMLNode instances)
  3. Keeps data row indices 0-based (headers don't count as a data row)
$table = new HTMLTable(3, 4);
$table->setHeaders(['Name', 'Status', 'Owner', 'Created']);
$table->setValue(0, 0, 'Project A');  // First data row, not header

Generated HTML:

<table>
  <thead><tr><th>Name</th><th>Status</th><th>Owner</th><th>Created</th></tr></thead>
  <tbody>
    <tr><td>Project A</td>...</tr>
  </tbody>
</table>

This mirrors the CLI Command::$this->table($data, $headers) API which already accepts a separate headers array.

Alternatives Considered

  • setFirstRowCellType('th') — simpler but doesn't generate semantic <thead>, and wastes a data row for headers
  • Manual loop with getCell(0, $c)->setNodeName('th') — current workaround, verbose and error-prone

Breaking Change

No — purely additive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions