Skip to content

Commit

Permalink
Issue #24: Add setRowAttribute().
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Oct 27, 2024
1 parent 03f208c commit f15a965
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/TSection/TableSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public function addRowClass($rowName, $class) {
return $this;
}

/**
* @param string $rowName
* @param string $name
* @param string $value
*
* @return $this
*/
public function setRowAttribute($rowName, $name, $value) {
$this->rowAttributes->nameSetAttribute($rowName, $name, $value);
return $this;
}

/**
* @param string[] $rowClasses
* Format: $[$rowName] = $class
Expand Down
11 changes: 11 additions & 0 deletions src/Table/TBodyWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public function addRowClass($rowName, $class) {
return $this;
}

/**
* @param string $rowName
* @param string $class
*
* @return $this
*/
public function setRowAttribute($rowName, $name, $value) {
$this->tbody->setRowAttribute($rowName, $name, $value);
return $this;
}

/**
* @param string[] $rowClasses
* Format: $[$rowName] = $class
Expand Down
11 changes: 8 additions & 3 deletions tests/src/CellbrushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ function testRowAndColGroupsWithClasses() {
$this->assertEquals($expected, $table->render());
}

function testCellAttributes() {
function testAttributes() {

$table = Table::create()
->addRowNames(['row0', 'row1', 'row2'])
Expand All @@ -903,14 +903,15 @@ function testCellAttributes() {
->td('row2', 'col2', 'Diag 2')
->setCellAttribute('row0', 'col1', 'attr', 'testattr')
->setCellAttribute('row2', 'col2', 'attr2', 'testattr2')
->setRowAttribute('row1', 'title', 'row 1 title')
->setColAttribute('col1', 'title', 'col 1 title')
;

$expected = <<<EOT
<table>
<tbody>
<tr><td>Diag 0</td><td title="col 1 title" attr="testattr"></td><td></td></tr>
<tr><td></td><td title="col 1 title">Diag 1</td><td></td></tr>
<tr title="row 1 title"><td></td><td title="col 1 title">Diag 1</td><td></td></tr>
<tr><td></td><td title="col 1 title"></td><td attr2="testattr2">Diag 2</td></tr>
</tbody>
</table>
Expand Down Expand Up @@ -941,6 +942,10 @@ function testRowAndColGroupsWithAttributes() {
->setCellAttribute('banana', 'name', 'title', 'banana name')
->setCellAttribute('banana.description', 'info', 'title', 'banana description')
->setCellAttribute('banana.info', 'info.price', 'title', 'banana price')
// A row attribute on a row group is ignored, because there is no <tr>
// that would represent the entire row group.
->setRowAttribute('banana', 'title', 'banana row')
->setRowAttribute('banana.info', 'title', 'banana info row')
->setColAttribute('info', 'col-attr', 'info col')
->setColAttribute('info.price', 'col-attr', 'price col')
;
Expand All @@ -958,7 +963,7 @@ function testRowAndColGroupsWithAttributes() {
</thead>
<tbody>
<tr><th title="banana name" rowspan="2">Banana</th><td title="banana description" col-attr="info col" colspan="2">A yellow fruit.</td></tr>
<tr><td col-attr="color tbody col">yellow</td><td title="banana price" col-attr="price col">60 cent</td></tr>
<tr title="banana info row"><td col-attr="color tbody col">yellow</td><td title="banana price" col-attr="price col">60 cent</td></tr>
<tr><th rowspan="2">Coconut</th><td col-attr="info col" colspan="2">Has liquid inside.</td></tr>
<tr><td col-attr="color tbody col">brown</td><td col-attr="price col">3 dollar</td></tr>
</tbody>
Expand Down

0 comments on commit f15a965

Please sign in to comment.