Merge pull request #1978 from jim-parry/feature/table

Implement HTML Table for CI4
This commit is contained in:
Instructor, BCIT 2019-05-01 21:26:50 -07:00 committed by GitHub
commit e3da4d50d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1357 additions and 0 deletions

530
system/View/Table.php Normal file
View File

@ -0,0 +1,530 @@
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 1.3.1
* @filesource
*/
namespace CodeIgniter\View;
use CodeIgniter\Database\BaseResult;
/**
* HTML Table Generating Class
*
* Lets you create tables manually or from database result objects, or arrays.
*
* @package CodeIgniter
* @subpackage Libraries
* @category HTML Tables
* @author EllisLab Dev Team
*/
class Table
{
/**
* Data for table rows
*
* @var array
*/
public $rows = [];
/**
* Data for table heading
*
* @var array
*/
public $heading = [];
/**
* Whether or not to automatically create the table header
*
* @var boolean
*/
public $autoHeading = true;
/**
* Table caption
*
* @var string
*/
public $caption = null;
/**
* Table layout template
*
* @var array
*/
public $template = null;
/**
* Newline setting
*
* @var string
*/
public $newline = "\n";
/**
* Contents of empty cells
*
* @var string
*/
public $emptyCells = '';
/**
* Callback for custom table layout
*
* @var function
*/
public $function = null;
/**
* Set the template from the table config file if it exists
*
* @param array $config (default: array())
* @return void
*/
public function __construct($config = [])
{
// initialize config
foreach ($config as $key => $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[] = '&nbsp;';
}
}
$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 .= '<caption>' . $this->caption . '</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('<th', '<th ' . $key . '="' . $val . '"', $temp);
}
}
$out .= $temp . (isset($heading['data']) ? $heading['data'] : '') . $this->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('<td', '<td ' . $key . '="' . $val . '"', $temp);
}
}
$cell = isset($cell['data']) ? $cell['data'] : '';
$out .= $temp;
if ($cell === '' || $cell === null)
{
$out .= $this->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' => '<table border="0" cellpadding="4" cellspacing="0">',
'thead_open' => '<thead>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>',
];
}
// --------------------------------------------------------------------
}

View File

@ -0,0 +1,17 @@
<?php
namespace Tests\Support\View;
class MockTable extends \CodeIgniter\View\Table {
// Override inaccessible protected method
public function __call($method, $params)
{
if (is_callable([$this, '_' . $method]))
{
return call_user_func_array([$this, '_' . $method], $params);
}
throw new BadMethodCallException('Method ' . $method . ' was not found');
}
}

View File

@ -0,0 +1,500 @@
<?php
namespace CodeIgniter\View;
use CodeIgniter\Database\MySQLi\Result;
use Tests\Support\View\MockTable;
class TableTest extends \CIUnitTestCase
{
public function setUp()
{
$this->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' => '</table junk>']);
$this->table->compileTemplate();
$this->assertArrayHasKey('table_close', $this->table->template);
$this->assertEquals('</table junk>', $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 &nbsp;
$this->assertEquals([
[
'Laura',
'Red',
'15',
],
[
'Katie',
'Blue',
'&nbsp;',
],
],
$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('<th>Name</th>', $table);
$this->assertContains('<th>Color</th>', $table);
$this->assertContains('<th>Size</th>', $table);
// Test the first entry
$this->assertContains('<td>Fred</td>', $table);
$this->assertContains('<td>Blue</td>', $table);
$this->assertContains('<td>Small</td>', $table);
$this->assertContains('<caption>Awesome table</caption>', $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('<td>Huh?</td>', $table);
}
// --------------------------------------------------------------------
public function testWithConfig()
{
$customSettings = [
'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">',
];
$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('<table border="1" cellpadding="2" cellspacing="1" class="mytable">', $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('<th>name</th>', $table);
$this->assertContains('<th>email</th>', $table);
// Test the first entry
$this->assertContains('<td>John Doe</td>', $table);
$this->assertContains('<td>foo@bar.com</td>', $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', '<strong>Blue</strong>', 'Small');
$this->table->function = 'htmlspecialchars';
$generated = $this->table->generate();
$this->assertContains('<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>', $generated);
}
public function testInvalidCallback()
{
$this->table->setHeading('Name', 'Color', 'Size');
$this->table->addRow('Fred', '<strong>Blue</strong>', 'Small');
$this->table->function = 'ticklemyfancy';
$generated = $this->table->generate();
$this->assertContains('<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>', $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',
],
];
}
}

View File

@ -13,6 +13,7 @@ Library Reference
pagination
security
sessions
table
throttler
time
typography

View File

@ -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

View File

@ -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
<div class="custom-index container"></div>
*********************
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' => '<table border="0" cellpadding="4" cellspacing="0">',
'thead_open' => '<thead>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
];
$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 border="1" cellpadding="2" cellspacing="1" class="mytable">'
];
$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 border="1" cellpadding="2" cellspacing="1" class="mytable">'
];
$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', '<strong>Blue</strong>', '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::
<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
.. 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
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
.. 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
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr><tr>
<td>four</td><td>five</td><td>six</td>
</tr><tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr><tr>
<td>ten</td><td>eleven</td><td>twelve</td></tr>
</table>
.. 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 border="1" cellpadding="2" cellspacing="1" class="mytable">'
);
$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("&nbsp;");
.. 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();