Skip to content

Commit

Permalink
Merge pull request #674 from colares/ticket-2787
Browse files Browse the repository at this point in the history
Add table attributes per column

Fixes #2787
  • Loading branch information
markstory committed May 31, 2012
2 parents 01b3135 + 6262a07 commit 1193774
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Expand Up @@ -1505,6 +1505,18 @@ public function testTableHeaders() {
$result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
$expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
$this->assertTags($result, $expected);

$result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight')), 'Date'));
$expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
$this->assertTags($result, $expected);

$result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight', 'width' => '120px')), 'Date'));
$expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
$this->assertTags($result, $expected);

$result = $this->Html->tableHeaders(array('ID', array('Name' => array()), 'Date'));
$expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
$this->assertTags($result, $expected);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions lib/Cake/View/Helper/HtmlHelper.php
Expand Up @@ -787,7 +787,8 @@ public function image($path, $options = array()) {
/**
* Returns a row of formatted and named TABLE headers.
*
* @param array $names Array of tablenames.
* @param array $names Array of tablenames. Each tablename also can be a key that points to an array with a set
* of attributes to its specific tag
* @param array $trOptions HTML options for TR elements.
* @param array $thOptions HTML options for TH elements.
* @return string Completed table headers
Expand All @@ -796,7 +797,11 @@ public function image($path, $options = array()) {
public function tableHeaders($names, $trOptions = null, $thOptions = null) {
$out = array();
foreach ($names as $arg) {
$out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
if (!is_array($arg)) {
$out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
} else {
$out[] = sprintf($this->_tags['tableheader'], $this->_parseAttributes(current($arg)), key($arg));
}
}
return sprintf($this->_tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
}
Expand Down

0 comments on commit 1193774

Please sign in to comment.