diff --git a/system/View/Table.php b/system/View/Table.php index b61e9b37b0b2..1923a60603ec 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -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 * @@ -151,7 +158,7 @@ public function setTemplate($template) * Can be passed as an array or discreet params * * @param mixed - * @return CI_Table + * @return Table */ public function setHeading($args = []) { @@ -159,6 +166,20 @@ public function setHeading($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; + } + // -------------------------------------------------------------------- /** @@ -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) { @@ -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 = []) { @@ -273,7 +294,7 @@ protected function _prepArgs($args) * Add a table caption * * @param string $caption - * @return CI_Table + * @return Table */ public function setCaption($caption) { @@ -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('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 @@ -422,6 +467,7 @@ public function clear() { $this->rows = []; $this->heading = []; + $this->footing = []; $this->autoHeading = true; $this->caption = null; return $this; @@ -512,6 +558,12 @@ protected function _defaultTemplate() 'heading_row_end' => '', 'heading_cell_start' => '', 'heading_cell_end' => '', + 'tfoot_open' => '', + 'tfoot_close' => '', + 'footing_row_start' => '', + 'footing_row_end' => '', + 'footing_cell_start' => '', + 'footing_cell_end' => '', 'tbody_open' => '', 'tbody_close' => '', 'row_start' => '', diff --git a/tests/system/View/TableTest.php b/tests/system/View/TableTest.php index ee3278b11221..98e98ecf12be 100644 --- a/tests/system/View/TableTest.php +++ b/tests/system/View/TableTest.php @@ -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 */ @@ -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 @@ -339,7 +361,12 @@ public function testGenerate() $this->assertContains('Blue', $table); $this->assertContains('Small', $table); + // Check for the caption $this->assertContains('Awesome table', $table); + + // Test the table footing + $this->assertContains('Subtotal', $table); + $this->assertContains('12345', $table); } public function testGenerateEmptyCell() diff --git a/user_guide_src/source/libraries/index.rst b/user_guide_src/source/libraries/index.rst index d3012f52eba9..888a3b86b10f 100644 --- a/user_guide_src/source/libraries/index.rst +++ b/user_guide_src/source/libraries/index.rst @@ -13,7 +13,6 @@ Library Reference pagination security sessions - table throttler time typography diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst index b55150443737..bb8053f92967 100644 --- a/user_guide_src/source/outgoing/table.rst +++ b/user_guide_src/source/outgoing/table.rst @@ -101,6 +101,14 @@ specify the design of your layout. Here is the template prototype:: 'heading_cell_start' => '', 'heading_cell_end' => '', + 'tfoot_open' => '', + 'tfoot_close' => '', + + 'footing_row_start' => '', + 'footing_row_end' => '', + 'footing_cell_start' => '', + 'footing_cell_end' => '', + 'tbody_open' => '', 'tbody_close' => '', @@ -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()[, ...]]) @@ -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:: @@ -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->setTemplate($template);