Merge pull request #674 from colares/ticket-2787

Add table attributes per column

Fixes #2787
This commit is contained in:
Mark Story 2012-05-30 18:29:56 -07:00
commit 119377422e
2 changed files with 19 additions and 2 deletions

View file

@ -1505,6 +1505,18 @@ class HtmlHelperTest extends CakeTestCase {
$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);
}
/**

View file

@ -787,7 +787,8 @@ class HtmlHelper extends AppHelper {
/**
* 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
@ -796,7 +797,11 @@ class HtmlHelper extends AppHelper {
public function tableHeaders($names, $trOptions = null, $thOptions = null) {
$out = array();
foreach ($names as $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));
}