From 6b245b2f4e2c7fb5055710391e819aee28c2ceda Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Wed, 1 May 2019 12:48:12 -0700 Subject: [PATCH] Implement HTML Table for CI4 --- system/View/Table.php | 530 ++++++++++++++++++++++ tests/_support/View/MockTable.php | 17 + tests/system/View/TableTest.php | 500 ++++++++++++++++++++ user_guide_src/source/libraries/index.rst | 1 + user_guide_src/source/outgoing/index.rst | 1 + user_guide_src/source/outgoing/table.rst | 308 +++++++++++++ 6 files changed, 1357 insertions(+) create mode 100644 system/View/Table.php create mode 100644 tests/_support/View/MockTable.php create mode 100644 tests/system/View/TableTest.php create mode 100644 user_guide_src/source/outgoing/table.rst diff --git a/system/View/Table.php b/system/View/Table.php new file mode 100644 index 000000000000..b61e9b37b0b2 --- /dev/null +++ b/system/View/Table.php @@ -0,0 +1,530 @@ + $val) + { + $this->template[$key] = $val; + } + } + + // -------------------------------------------------------------------- + + /** + * Set the template + * + * @param array $template + * @return boolean + */ + public function setTemplate($template) + { + if (! is_array($template)) + { + return false; + } + + $this->template = $template; + return true; + } + + // -------------------------------------------------------------------- + + /** + * Set the table heading + * + * Can be passed as an array or discreet params + * + * @param mixed + * @return CI_Table + */ + public function setHeading($args = []) + { + $this->heading = $this->_prepArgs(func_get_args()); + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Set columns. Takes a one-dimensional array as input and creates + * a multi-dimensional array with a depth equal to the number of + * columns. This allows a single array with many elements to be + * displayed in a table that has a fixed column count. + * + * @param array $array + * @param integer $columnLimit + * @return array + */ + public function makeColumns($array = [], $columnLimit = 0) + { + if (! is_array($array) || count($array) === 0 || ! is_int($columnLimit)) + { + return false; + } + + // Turn off the auto-heading feature since it's doubtful we + // will want headings from a one-dimensional array + $this->autoHeading = false; + + if ($columnLimit === 0) + { + return $array; + } + + $new = []; + do + { + $temp = array_splice($array, 0, $columnLimit); + + if (count($temp) < $columnLimit) + { + for ($i = count($temp); $i < $columnLimit; $i ++) + { + $temp[] = ' '; + } + } + + $new[] = $temp; + } + while (count($array) > 0); + + return $new; + } + + // -------------------------------------------------------------------- + + /** + * Set "empty" cells + * + * Can be passed as an array or discreet params + * + * @param mixed $value + * @return CI_Table + */ + public function setEmpty($value) + { + $this->emptyCells = $value; + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Add a table row + * + * Can be passed as an array or discreet params + * + * @param mixed + * @return CI_Table + */ + public function addRow($args = []) + { + $this->rows[] = $this->_prepArgs(func_get_args()); + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Prep Args + * + * Ensures a standard associative array format for all cell data + * + * @param array + * @return array + */ + protected function _prepArgs($args) + { + // If there is no $args[0], skip this and treat as an associative array + // This can happen if there is only a single key, for example this is passed to table->generate + // array(array('foo'=>'bar')) + if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) + { + $args = $args[0]; + } + + foreach ($args as $key => $val) + { + is_array($val) || $args[$key] = ['data' => $val]; + } + + return $args; + } + + // -------------------------------------------------------------------- + + /** + * Add a table caption + * + * @param string $caption + * @return CI_Table + */ + public function setCaption($caption) + { + $this->caption = $caption; + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Generate the table + * + * @param mixed $tableData + * @return string + */ + public function generate($tableData = null) + { + // The table data can optionally be passed to this function + // either as a database result object or an array + if (! empty($tableData)) + { + if ($tableData instanceof BaseResult) + { + $this->_setFromDBResult($tableData); + } + elseif (is_array($tableData)) + { + $this->_setFromArray($tableData); + } + } + + // Is there anything to display? No? Smite them! + if (empty($this->heading) && empty($this->rows)) + { + return 'Undefined table data'; + } + + // Compile and validate the template date + $this->_compileTemplate(); + + // Validate a possibly existing custom cell manipulation function + if (isset($this->function) && ! is_callable($this->function)) + { + $this->function = null; + } + + // Build the table! + + $out = $this->template['table_open'] . $this->newline; + + // Add any caption here + if ($this->caption) + { + $out .= '' . $this->caption . '' . $this->newline; + } + + // Is there a table heading to display? + if (! empty($this->heading)) + { + $out .= $this->template['thead_open'] . $this->newline . $this->template['heading_row_start'] . $this->newline; + + foreach ($this->heading as $heading) + { + $temp = $this->template['heading_cell_start']; + + foreach ($heading as $key => $val) + { + if ($key !== 'data') + { + $temp = str_replace('template['heading_cell_end']; + } + + $out .= $this->template['heading_row_end'] . $this->newline . $this->template['thead_close'] . $this->newline; + } + + // Build the table rows + if (! empty($this->rows)) + { + $out .= $this->template['tbody_open'] . $this->newline; + + $i = 1; + foreach ($this->rows as $row) + { + // We use modulus to alternate the row colors + $name = fmod($i ++, 2) ? '' : 'alt_'; + + $out .= $this->template['row_' . $name . 'start'] . $this->newline; + + foreach ($row as $cell) + { + $temp = $this->template['cell_' . $name . 'start']; + + foreach ($cell as $key => $val) + { + if ($key !== 'data') + { + $temp = str_replace('emptyCells; + } + elseif (isset($this->function)) + { + $out .= call_user_func($this->function, $cell); + } + else + { + $out .= $cell; + } + + $out .= $this->template['cell_' . $name . 'end']; + } + + $out .= $this->template['row_' . $name . 'end'] . $this->newline; + } + + $out .= $this->template['tbody_close'] . $this->newline; + } + + $out .= $this->template['table_close']; + + // Clear table class properties before generating the table + $this->clear(); + + return $out; + } + + // -------------------------------------------------------------------- + + /** + * Clears the table arrays. Useful if multiple tables are being generated + * + * @return Table + */ + public function clear() + { + $this->rows = []; + $this->heading = []; + $this->autoHeading = true; + $this->caption = null; + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Set table data from a database result object + * + * @param BaseResult $object Database result object + * @return void + */ + protected function _setFromDBResult($object) + { + // First generate the headings from the table column names + if ($this->autoHeading === true && empty($this->heading)) + { + $this->heading = $this->_prepArgs($object->getFieldNames()); + } + + foreach ($object->getResultArray() as $row) + { + $this->rows[] = $this->_prepArgs($row); + } + } + + // -------------------------------------------------------------------- + + /** + * Set table data from an array + * + * @param array $data + * @return void + */ + protected function _setFromArray($data) + { + if ($this->autoHeading === true && empty($this->heading)) + { + $this->heading = $this->_prepArgs(array_shift($data)); + } + + foreach ($data as &$row) + { + $this->rows[] = $this->_prepArgs($row); + } + } + + // -------------------------------------------------------------------- + + /** + * Compile Template + * + * @return void + */ + protected function _compileTemplate() + { + if ($this->template === null) + { + $this->template = $this->_defaultTemplate(); + return; + } + + $this->temp = $this->_defaultTemplate(); + foreach (['table_open', 'thead_open', 'thead_close', 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'tbody_open', 'tbody_close', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close'] as $val) + { + if (! isset($this->template[$val])) + { + $this->template[$val] = $this->temp[$val]; + } + } + } + + // -------------------------------------------------------------------- + + /** + * Default Template + * + * @return array + */ + protected function _defaultTemplate() + { + return [ + 'table_open' => '', + 'thead_open' => '', + 'thead_close' => '', + 'heading_row_start' => '', + 'heading_row_end' => '', + 'heading_cell_start' => '', + 'tbody_open' => '', + 'tbody_close' => '', + 'row_start' => '', + 'row_end' => '', + 'cell_start' => '', + 'row_alt_start' => '', + 'row_alt_end' => '', + 'cell_alt_start' => '', + 'table_close' => '
', + 'heading_cell_end' => '
', + 'cell_end' => '
', + 'cell_alt_end' => '
', + ]; + } + + // -------------------------------------------------------------------- +} diff --git a/tests/_support/View/MockTable.php b/tests/_support/View/MockTable.php new file mode 100644 index 000000000000..d286b2261f78 --- /dev/null +++ b/tests/_support/View/MockTable.php @@ -0,0 +1,17 @@ +table = new MockTable(); + } + + // Setter Methods + // -------------------------------------------------------------------- + + public function testSetTemplate() + { + $this->assertFalse($this->table->setTemplate('not an array')); + + $template = ['a' => 'b']; + + $this->table->setTemplate($template); + $this->assertEquals($template, $this->table->template); + } + + public function testSetEmpty() + { + $this->table->setEmpty('nada'); + $this->assertEquals('nada', $this->table->emptyCells); + } + + public function testSetCaption() + { + $this->table->setCaption('awesome cap'); + $this->assertEquals('awesome cap', $this->table->caption); + } + + /* + * @depends test_prep_args + */ + + public function testSetHeading() + { + // uses _prep_args internally, so we'll just do a quick + // check to verify that func_get_args and prep_args are + // being called. + + $this->table->setHeading('name', 'color', 'size'); + + $this->assertEquals([ + ['data' => 'name'], + ['data' => 'color'], + ['data' => 'size'], + ], + $this->table->heading + ); + } + + /* + * @depends test_prep_args + */ + + public function testAddRow() + { + // uses _prep_args internally, so we'll just do a quick + // check to verify that func_get_args and prep_args are + // being called. + + $this->table->addRow('my', 'pony', 'sings'); + $this->table->addRow('your', 'pony', 'stinks'); + $this->table->addRow('my pony', '>', 'your pony'); + + $this->assertCount(3, $this->table->rows); + + $this->assertEquals([ + ['data' => 'your'], + ['data' => 'pony'], + ['data' => 'stinks'], + ], + $this->table->rows[1] + ); + } + + // Uility Methods + // -------------------------------------------------------------------- + + public function testPrepArgs() + { + $expected = [ + ['data' => 'name'], + ['data' => 'color'], + ['data' => 'size'], + ]; + + $this->assertEquals( + $expected, + $this->table->prepArgs(['name', 'color', 'size']) + ); + + // with cell attributes + // need to add that new argument row to our expected outcome + $expected[] = [ + 'data' => 'weight', + 'class' => 'awesome', + ]; + + $this->assertEquals( + $expected, + $this->table->prepArgs(['name', 'color', 'size', ['data' => 'weight', 'class' => 'awesome']]) + ); + } + + public function testDefaultTemplateKeys() + { + $keys = [ + 'table_open', + 'thead_open', + 'thead_close', + 'heading_row_start', + 'heading_row_end', + 'heading_cell_start', + 'heading_cell_end', + 'tbody_open', + 'tbody_close', + 'row_start', + 'row_end', + 'cell_start', + 'cell_end', + 'row_alt_start', + 'row_alt_end', + 'cell_alt_start', + 'cell_alt_end', + 'table_close', + ]; + + foreach ($keys as $key) + { + $this->assertArrayHasKey($key, $this->table->defaultTemplate()); + } + } + + public function testCompileTemplate() + { + $this->assertFalse($this->table->setTemplate('invalid_junk')); + + // non default key + $this->table->setTemplate(['nonsense' => 'foo']); + $this->table->compileTemplate(); + + $this->assertArrayHasKey('nonsense', $this->table->template); + $this->assertEquals('foo', $this->table->template['nonsense']); + + // override default + $this->table->setTemplate(['table_close' => '']); + $this->table->compileTemplate(); + + $this->assertArrayHasKey('table_close', $this->table->template); + $this->assertEquals('', $this->table->template['table_close']); + } + + public function testMakeColumns() + { + // Test bogus parameters + $this->assertFalse($this->table->makeColumns('invalid_junk')); + $this->assertFalse($this->table->makeColumns([])); + $this->assertFalse($this->table->makeColumns(['one', 'two'], '2.5')); + + // Now on to the actual column creation + + $five_values = [ + 'Laura', + 'Red', + '15', + 'Katie', + 'Blue', + ]; + + // No column count - no changes to the array + $this->assertEquals( + $five_values, + $this->table->makeColumns($five_values) + ); + + // Column count of 3 leaves us with one   + $this->assertEquals([ + [ + 'Laura', + 'Red', + '15', + ], + [ + 'Katie', + 'Blue', + ' ', + ], + ], + $this->table->makeColumns($five_values, 3) + ); + } + + public function testClear() + { + $this->table->setHeading('Name', 'Color', 'Size'); + + // Make columns changes auto_heading + $rows = $this->table->makeColumns([ + 'Laura', + 'Red', + '15', + 'Katie', + 'Blue', + ], 3); + + foreach ($rows as $row) + { + $this->table->addRow($row); + } + + $this->assertFalse($this->table->autoHeading); + $this->assertCount(3, $this->table->heading); + $this->assertCount(2, $this->table->rows); + + $this->table->clear(); + + $this->assertTrue($this->table->autoHeading); + $this->assertEmpty($this->table->heading); + $this->assertEmpty($this->table->rows); + } + + public function testSetFromArray() + { + $data = [ + [ + 'name', + 'color', + 'number', + ], + [ + 'Laura', + 'Red', + '22', + ], + [ + 'Katie', + 'Blue', + ], + ]; + + $this->table->autoHeading = false; + $this->table->setFromArray($data); + $this->assertEmpty($this->table->heading); + + $this->table->clear(); + + $this->table->setFromArray($data); + $this->assertCount(2, $this->table->rows); + + $expected = [ + ['data' => 'name'], + ['data' => 'color'], + ['data' => 'number'], + ]; + + $this->assertEquals($expected, $this->table->heading); + + $expected = [ + ['data' => 'Katie'], + ['data' => 'Blue'], + ]; + + $this->assertEquals($expected, $this->table->rows[1]); + } + + public function testSetFromObject() + { + // This needs to be passed by reference to CI_DB_result::__construct() + $dummy = new \stdClass(); + $dummy->connID = null; + $dummy->resultID = null; + + $DBResult = new DBResultDummy($dummy->connID, $dummy->resultID); + + $this->table->setFromDBResult($DBResult); + + $expected = [ + ['data' => 'name'], + ['data' => 'email'], + ]; + + $this->assertEquals($expected, $this->table->heading); + + $expected = [ + 'name' => ['data' => 'Foo Bar'], + 'email' => ['data' => 'foo@bar.com'], + ]; + + $this->assertEquals($expected, $this->table->rows[1]); + } + + public function testGenerate() + { + // Prepare the data + $data = [ + [ + 'Name', + 'Color', + 'Size', + ], + [ + 'Fred', + 'Blue', + 'Small', + ], + [ + 'Mary', + 'Red', + 'Large', + ], + [ + 'John', + 'Green', + 'Medium', + ], + ]; + + $this->table->setCaption('Awesome table'); + + $table = $this->table->generate($data); + + // Test the table header + $this->assertContains('Name', $table); + $this->assertContains('Color', $table); + $this->assertContains('Size', $table); + + // Test the first entry + $this->assertContains('Fred', $table); + $this->assertContains('Blue', $table); + $this->assertContains('Small', $table); + + $this->assertContains('Awesome table', $table); + } + + public function testGenerateEmptyCell() + { + // Prepare the data + $data = [ + [ + 'Name', + 'Color', + 'Size', + ], + [ + 'Fred', + 'Blue', + 'Small', + ], + [ + 'Mary', + 'Red', + null, + ], + [ + 'John', + 'Green', + 'Medium', + ], + ]; + + $this->table->setCaption('Awesome table'); + $this->table->setEmpty('Huh?'); + $table = $this->table->generate($data); + + $this->assertContains('Huh?', $table); + } + + // -------------------------------------------------------------------- + + public function testWithConfig() + { + $customSettings = [ + 'table_open' => '', + ]; + + $table = new \CodeIgniter\View\Table($customSettings); + + // Prepare the data + $data = [ + [ + 'Name', + 'Color', + 'Size', + ], + [ + 'Fred', + 'Blue', + 'Small', + ], + [ + 'Mary', + 'Red', + 'Large', + ], + [ + 'John', + 'Green', + 'Medium', + ], + ]; + + $generated = $table->generate($data); + + $this->assertContains('
', $generated); + } + + public function testGenerateFromDBResult() + { + // This needs to be passed by reference to CI_DB_result::__construct() + $dummy = new \stdClass(); + $dummy->connID = null; + $dummy->resultID = null; + $DBResult = new DBResultDummy($dummy->connID, $dummy->resultID); + + $table = $this->table->generate($DBResult); + + // Test the table header + $this->assertContains('', $table); + $this->assertContains('', $table); + + // Test the first entry + $this->assertContains('', $table); + $this->assertContains('', $table); + } + + public function testUndefined() + { + // Prepare the data + $data = []; + + $table = $this->table->generate($data); + + $this->assertEquals('Undefined table data', $table); + } + + // -------------------------------------------------------------------- + + public function testCallback() + { + $this->table->setHeading('Name', 'Color', 'Size'); + $this->table->addRow('Fred', 'Blue', 'Small'); + + $this->table->function = 'htmlspecialchars'; + + $generated = $this->table->generate(); + + $this->assertContains('', $generated); + } + + public function testInvalidCallback() + { + $this->table->setHeading('Name', 'Color', 'Size'); + $this->table->addRow('Fred', 'Blue', 'Small'); + + $this->table->function = 'ticklemyfancy'; + + $generated = $this->table->generate(); + + $this->assertContains('', $generated); + } + +} + +// We need this for the _set_from_db_result() test +class DBResultDummy extends Result +{ + + public function getFieldNames(): array + { + return [ + 'name', + 'email', + ]; + } + + public function getResultArray(): array + { + return [ + [ + 'name' => 'John Doe', + 'email' => 'john@doe.com', + ], + [ + 'name' => 'Foo Bar', + 'email' => 'foo@bar.com', + ], + ]; + } + +} diff --git a/user_guide_src/source/libraries/index.rst b/user_guide_src/source/libraries/index.rst index 888a3b86b10f..d3012f52eba9 100644 --- a/user_guide_src/source/libraries/index.rst +++ b/user_guide_src/source/libraries/index.rst @@ -13,6 +13,7 @@ Library Reference pagination security sessions + table throttler time typography diff --git a/user_guide_src/source/outgoing/index.rst b/user_guide_src/source/outgoing/index.rst index 925b5e59a9d9..12995a7daf9e 100644 --- a/user_guide_src/source/outgoing/index.rst +++ b/user_guide_src/source/outgoing/index.rst @@ -12,6 +12,7 @@ View components are used to build what is returned to the user. view_renderer view_layouts view_parser + table response api_responses localization diff --git a/user_guide_src/source/outgoing/table.rst b/user_guide_src/source/outgoing/table.rst new file mode 100644 index 000000000000..b55150443737 --- /dev/null +++ b/user_guide_src/source/outgoing/table.rst @@ -0,0 +1,308 @@ +################ +HTML Table Class +################ + +The Table Class provides methods that enable you to auto-generate HTML +tables from arrays or database result sets. + +.. contents:: + :local: + +.. raw:: html + +
+ +********************* +Using the Table Class +********************* + +Initializing the Class +====================== + +The Table class is not provided as a service, and should be instantiated +"normally", for instance:: + + $table = new \CodeIgniter\View\Table(); + +Examples +======== + +Here is an example showing how you can create a table from a +multi-dimensional array. Note that the first array index will become the +table heading (or you can set your own headings using the ``setHeading()`` +method described in the function reference below). + +:: + + $table = new \CodeIgniter\View\Table(); + + $data = array( + array('Name', 'Color', 'Size'), + array('Fred', 'Blue', 'Small'), + array('Mary', 'Red', 'Large'), + array('John', 'Green', 'Medium') + ); + + echo $table->generate($data); + +Here is an example of a table created from a database query result. The +table class will automatically generate the headings based on the table +names (or you can set your own headings using the ``setHeading()`` +method described in the class reference below). + +:: + + $table = new \CodeIgniter\View\Table(); + + $query = $db->query('SELECT * FROM my_table'); + + echo $table->generate($query); + +Here is an example showing how you might create a table using discrete +parameters:: + + $table = new \CodeIgniter\View\Table(); + + $table->setHeading('Name', 'Color', 'Size'); + + $table->addRow('Fred', 'Blue', 'Small'); + $table->addRow('Mary', 'Red', 'Large'); + $table->addRow('John', 'Green', 'Medium'); + + echo $table->generate(); + +Here is the same example, except instead of individual parameters, +arrays are used:: + + $table = new \CodeIgniter\View\Table(); + + $table->setHeading(array('Name', 'Color', 'Size')); + + $table->addRow(['Fred', 'Blue', 'Small']); + $table->addRow(['Mary', 'Red', 'Large']); + $table->addRow(['John', 'Green', 'Medium']); + + echo $table->generate(); + +Changing the Look of Your Table +=============================== + +The Table Class permits you to set a table template with which you can +specify the design of your layout. Here is the template prototype:: + + $template = [ + 'table_open' => '
nameemailJohn Doefoo@bar.comFred<strong>Blue</strong>SmallFredBlueSmall
', + + 'thead_open' => '', + 'thead_close' => '', + + 'heading_row_start' => '', + 'heading_row_end' => '', + 'heading_cell_start' => '', + + 'tbody_open' => '', + 'tbody_close' => '', + + 'row_start' => '', + 'row_end' => '', + 'cell_start' => '', + + 'row_alt_start' => '', + 'row_alt_end' => '', + 'cell_alt_start' => '', + + 'table_close' => '
', + 'heading_cell_end' => '
', + 'cell_end' => '
', + 'cell_alt_end' => '
' + ]; + + $table->setTemplate($template); + +.. note:: You'll notice there are two sets of "row" blocks in the + template. These permit you to create alternating row colors or design + elements that alternate with each iteration of the row data. + +You are NOT required to submit a complete template. If you only need to +change parts of the layout you can simply submit those elements. In this +example, only the table opening tag is being changed:: + + $template = [ + 'table_open' => '' + ]; + + $table->setTemplate($template); + +You can also set defaults for these by passing an array of template settings +to the Table constructor.:: + + $customSettings = [ + 'table_open' => '
' + ]; + + $table = new \CodeIgniter\View\Table($customSettings); + + +*************** +Class Reference +*************** + +.. php:class:: Table + + .. attribute:: $function = NULL + + Allows you to specify a native PHP function or a valid function array object to be applied to all cell data. + :: + + $table = new \CodeIgniter\View\Table(); + + $table->setHeading('Name', 'Color', 'Size'); + $table->addRow('Fred', 'Blue', 'Small'); + + $table->function = 'htmlspecialchars'; + echo $table->generate(); + + In the above example, all cell data would be ran through PHP's :php:func:`htmlspecialchars()` function, resulting in:: + + + + .. php:method:: generate([$tableData = NULL]) + + :param mixed $tableData: Data to populate the table rows with + :returns: HTML table + :rtype: string + + Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object. + + .. php:method:: setCaption($caption) + + :param string $caption: Table caption + :returns: Table instance (method chaining) + :rtype: Table + + Permits you to add a caption to the table. + :: + + $table->setCaption('Colors'); + + .. php:method:: setHeading([$args = [] [, ...]]) + + :param mixed $args: An array or multiple strings containing the table column titles + :returns: Table instance (method chaining) + :rtype: Table + + Permits you to set the table heading. You can submit an array or discrete params:: + + $table->setHeading('Name', 'Color', 'Size'); + + $table->setHeading(array('Name', 'Color', 'Size')); + + .. php:method:: addRow([$args = array()[, ...]]) + + :param mixed $args: An array or multiple strings containing the row values + :returns: Table instance (method chaining) + :rtype: Table + + Permits you to add a row to your table. You can submit an array or discrete params:: + + $table->addRow('Blue', 'Red', 'Green'); + + $table->addRow(array('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:: + + $cell = ['data' => 'Blue', 'class' => 'highlight', 'colspan' => 2]; + $table->addRow($cell, 'Red', 'Green'); + + // generates + // + + .. php:method:: makeColumns([$array = [] [, $columnLimit = 0]]) + + :param array $array: An array containing multiple rows' data + :param int $columnLimit: Count of columns in the table + :returns: An array of HTML table columns + :rtype: array + + This method takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. + This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:: + + $list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']; + + $newList = $table->makeColumns($list, 3); + + $table->generate($newList); + + // Generates a table with this prototype + +
Fred<strong>Blue</strong>SmallBlueRedGreen
+ + + + + + + + +
onetwothree
fourfivesix
seveneightnine
teneleventwelve
+ + + .. php:method:: setTemplate($template) + + :param array $template: An associative array containing template values + :returns: TRUE on success, FALSE on failure + :rtype: bool + + Permits you to set your template. You can submit a full or partial template. + :: + + $template = array( + 'table_open' => '' + ); + + $table->setTemplate($template); + + .. php:method:: setEmpty($value) + + :param mixed $value: Value to put in empty cells + :returns: Table instance (method chaining) + :rtype: Table + + Lets you set a default value for use in any table cells that are empty. + You might, for example, set a non-breaking space:: + + $table->setEmpty(" "); + + .. php:method:: clear() + + :returns: Table instance (method chaining) + :rtype: Table + + Lets you clear the table heading, row data and caption. If + you need to show multiple tables with different data you + should to call this method after each table has been + generated to clear the previous table information. + + Example :: + + $table = new \CodeIgniter\View\Table(); + + + $table->setCaption('Preferences') + ->setHeading('Name', 'Color', 'Size') + ->addRow('Fred', 'Blue', 'Small') + ->addRow('Mary', 'Red', 'Large') + ->addRow('John', 'Green', 'Medium'); + + echo $table->generate(); + + $table->clear(); + + $table->setCaption('Shipping') + ->setHeading('Name', 'Day', 'Delivery') + ->addRow('Fred', 'Wednesday', 'Express') + ->addRow('Mary', 'Monday', 'Air') + ->addRow('John', 'Saturday', 'Overnight'); + + echo $table->generate();