Skip to content

Commit

Permalink
Merge pull request #1984 from jim-parry/enhance/table
Browse files Browse the repository at this point in the history
Add footing to HTML Table
  • Loading branch information
jim-parry authored May 2, 2019
2 parents e3da4d5 + 926b7ee commit a310392
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
62 changes: 57 additions & 5 deletions system/View/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class Table
*/
public $heading = [];

/**
* Data for table footing
*
* @var array
*/
public $footing = [];

/**
* Whether or not to automatically create the table header
*
Expand Down Expand Up @@ -151,14 +158,28 @@ public function setTemplate($template)
* Can be passed as an array or discreet params
*
* @param mixed
* @return CI_Table
* @return Table
*/
public function setHeading($args = [])
{
$this->heading = $this->_prepArgs(func_get_args());
return $this;
}

/**
* Set the table footing
*
* Can be passed as an array or discreet params
*
* @param mixed
* @return Table
*/
public function setFooting($args = [])
{
$this->footing = $this->_prepArgs(func_get_args());
return $this;
}

// --------------------------------------------------------------------

/**
Expand Down Expand Up @@ -215,7 +236,7 @@ public function makeColumns($array = [], $columnLimit = 0)
* Can be passed as an array or discreet params
*
* @param mixed $value
* @return CI_Table
* @return Table
*/
public function setEmpty($value)
{
Expand All @@ -231,7 +252,7 @@ public function setEmpty($value)
* Can be passed as an array or discreet params
*
* @param mixed
* @return CI_Table
* @return Table
*/
public function addRow($args = [])
{
Expand Down Expand Up @@ -273,7 +294,7 @@ protected function _prepArgs($args)
* Add a table caption
*
* @param string $caption
* @return CI_Table
* @return Table
*/
public function setCaption($caption)
{
Expand Down Expand Up @@ -399,10 +420,34 @@ public function generate($tableData = null)

$out .= $this->template['row_' . $name . 'end'] . $this->newline;
}
}

// Any table footing to display?
if (! empty($this->footing))
{
$out .= $this->template['tfoot_open'] . $this->newline . $this->template['footing_row_start'] . $this->newline;

$out .= $this->template['tbody_close'] . $this->newline;
foreach ($this->footing as $footing)
{
$temp = $this->template['footing_cell_start'];

foreach ($footing as $key => $val)
{
if ($key !== 'data')
{
$temp = str_replace('<th', '<th ' . $key . '="' . $val . '"', $temp);
}
}

$out .= $temp . (isset($footing['data']) ? $footing['data'] : '') . $this->template['footing_cell_end'];
}

$out .= $this->template['footing_row_end'] . $this->newline . $this->template['tfoot_close'] . $this->newline;
}

$out .= $this->template['tbody_close'] . $this->newline;

// And finally, close off the table
$out .= $this->template['table_close'];

// Clear table class properties before generating the table
Expand All @@ -422,6 +467,7 @@ public function clear()
{
$this->rows = [];
$this->heading = [];
$this->footing = [];
$this->autoHeading = true;
$this->caption = null;
return $this;
Expand Down Expand Up @@ -512,6 +558,12 @@ protected function _defaultTemplate()
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tfoot_open' => '<tfoot>',
'tfoot_close' => '</tfoot>',
'footing_row_start' => '<tr>',
'footing_row_end' => '</tr>',
'footing_cell_start' => '<td>',
'footing_cell_end' => '</td>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr>',
Expand Down
27 changes: 27 additions & 0 deletions tests/system/View/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ public function testSetHeading()
);
}

public function testSetFooting()
{
// uses _prep_args internally, so we'll just do a quick
// check to verify that func_get_args and prep_args are
// being called.

$subtotal = 12345;

$this->table->setFooting('Subtotal', $subtotal);

$this->assertEquals([
['data' => 'Subtotal'],
['data' => $subtotal],
],
$this->table->footing
);
}

/*
* @depends test_prep_args
*/
Expand Down Expand Up @@ -327,6 +345,10 @@ public function testGenerate()

$this->table->setCaption('Awesome table');

$subtotal = 12345;

$this->table->setFooting('Subtotal', $subtotal);

$table = $this->table->generate($data);

// Test the table header
Expand All @@ -339,7 +361,12 @@ public function testGenerate()
$this->assertContains('<td>Blue</td>', $table);
$this->assertContains('<td>Small</td>', $table);

// Check for the caption
$this->assertContains('<caption>Awesome table</caption>', $table);

// Test the table footing
$this->assertContains('<td>Subtotal</td>', $table);
$this->assertContains('<td>12345</td>', $table);
}

public function testGenerateEmptyCell()
Expand Down
1 change: 0 additions & 1 deletion user_guide_src/source/libraries/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Library Reference
pagination
security
sessions
table
throttler
time
typography
Expand Down
32 changes: 26 additions & 6 deletions user_guide_src/source/outgoing/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ specify the design of your layout. Here is the template prototype::
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',

'tfoot_open' => '<tfoot>',
'tfoot_close' => '</tfoot>',

'footing_row_start' => '<tr>',
'footing_row_end' => '</tr>',
'footing_cell_start' => '<td>',
'footing_cell_end' => '</td>',

'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',

Expand Down Expand Up @@ -193,9 +201,21 @@ Class Reference

Permits you to set the table heading. You can submit an array or discrete params::

$table->setHeading('Name', 'Color', 'Size');
$table->setHeading('Name', 'Color', 'Size'); // or

$table->setHeading(['Name', 'Color', 'Size']);

.. php:method:: setFooting([$args = [] [, ...]])
:param mixed $args: An array or multiple strings containing the table footing values
:returns: Table instance (method chaining)
:rtype: Table

Permits you to set the table footing. You can submit an array or discrete params::

$table->setFooting('Subtotal', $subtotal, $notes); // or

$table->setHeading(array('Name', 'Color', 'Size'));
$table->setFooting(['Subtotal', $subtotal, $notes]);

.. php:method:: addRow([$args = array()[, ...]])
Expand All @@ -205,9 +225,9 @@ Class Reference

Permits you to add a row to your table. You can submit an array or discrete params::

$table->addRow('Blue', 'Red', 'Green');
$table->addRow('Blue', 'Red', 'Green'); // or

$table->addRow(array('Blue', 'Red', 'Green'));
$table->addRow(['Blue', 'Red', 'Green']);

If you would like to set an individual cell's tag attributes, you can use an associative array for that cell.
The associative key **data** defines the cell's data. Any other key => val pairs are added as key='val' attributes to the tag::
Expand Down Expand Up @@ -257,9 +277,9 @@ Class Reference
Permits you to set your template. You can submit a full or partial template.
::

$template = array(
$template = [
'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
);
];
$table->setTemplate($template);

Expand Down

0 comments on commit a310392

Please sign in to comment.