Skip to content

Commit

Permalink
Fix HtmlHelper::tableCells()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed May 2, 2015
1 parent bc2d222 commit 8d9f401
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/View/Helper/HtmlHelper.php
Expand Up @@ -871,9 +871,9 @@ public function tableHeaders(array $names, array $trOptions = null, array $thOpt
/**
* Returns a formatted string of table rows (TR's with TD's in them).
*
* @param array $data Array of table data
* @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
* @param array $evenTrOptions HTML options for even TR elements
* @param array|string $data Array of table data
* @param array|bool|null $oddTrOptions HTML options for odd TR elements if true useCount is used
* @param array|bool|null $evenTrOptions HTML options for even TR elements
* @param bool $useCount adds class "column-$i"
* @param bool $continueOddEven If false, will use a non-static $count variable,
* so that the odd/even count is reset to zero just for that call.
Expand Down Expand Up @@ -912,9 +912,16 @@ public function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $
if (is_array($cell)) {
$cellOptions = $cell[1];
$cell = $cell[0];
} elseif ($useCount) {
$cellOptions['class'] = 'column-' . ++$i;
}

if ($useCount) {
if (isset($cellOptions['class'])) {
$cellOptions['class'] .= ' column-' . ++$i;
} else {
$cellOptions['class'] = 'column-' . ++$i;
}
}

$cellsOut[] = $this->formatTemplate('tablecell', [
'attrs' => $this->templater()->formatAttributes($cellOptions),
'content' => $cell
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -1821,6 +1821,21 @@ public function testTableCells()
$result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
$expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
$this->assertEquals($expected, $result);

$tr = [
'td content 1',
'td content 2',
['td content 3', ['class' => 'foo']]
];
$result = $this->Html->tableCells($tr, null, null, true);
$expected = [
'<tr',
['td' => ['class' => 'column-1']], 'td content 1', '/td',
['td' => ['class' => 'column-2']], 'td content 2', '/td',
['td' => ['class' => 'foo column-3']], 'td content 3', '/td',
'/tr'
];
$this->assertHtml($expected, $result);
}

/**
Expand Down

0 comments on commit 8d9f401

Please sign in to comment.