diff options
-rw-r--r-- | system/libraries/Table.php | 138 | ||||
-rw-r--r-- | user_guide/libraries/table.html | 41 |
2 files changed, 145 insertions, 34 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php index 6d36121f2..1f920ea9e 100644 --- a/system/libraries/Table.php +++ b/system/libraries/Table.php @@ -35,7 +35,7 @@ class CI_Table { var $template = NULL; var $newline = "\n"; var $empty_cells = ""; - + var $function = FALSE; function CI_Table() { @@ -75,7 +75,7 @@ class CI_Table { function set_heading() { $args = func_get_args(); - $this->heading = (is_array($args[0])) ? $args[0] : $args; + $this->heading = $this->_prep_args($args); } // -------------------------------------------------------------------- @@ -110,7 +110,7 @@ class CI_Table { $new = array(); while(count($array) > 0) { - $temp = array_splice($array, 0, $col_limit); + $temp = array_splice($array, 0, $col_limit); if (count($temp) < $col_limit) { @@ -156,11 +156,59 @@ class CI_Table { function add_row() { $args = func_get_args(); - $this->rows[] = (is_array($args[0])) ? $args[0] : $args; + $this->rows[] = $this->_prep_args($args); } // -------------------------------------------------------------------- + + /** + * Prep Args + * + * Ensures a standard associative array format for all cell data + * + * @access public + * @param type + * @return type + */ + function _prep_args($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]) AND (count($args) == 1 && is_array($args[0]))) + { + // args sent as indexed array + if ( ! isset($args[0]['data'])) + { + foreach ($args[0] as $key => $val) + { + if (is_array($val) && isset($val['data'])) + { + $args[$key] = $val; + } + else + { + $args[$key] = array('data' => $val); + } + } + } + } + else + { + foreach ($args as $key => $val) + { + if ( ! is_array($val)) + { + $args[$key] = array('data' => $val); + } + } + } + + return $args; + } + // -------------------------------------------------------------------- + /** * Add a table caption * @@ -208,7 +256,9 @@ class CI_Table { // Compile and validate the template date $this->_compile_template(); - + // set a custom cell manipulation function to a locally scoped variable so its callable + $function = $this->function; + // Build the table! $out = $this->template['table_open']; @@ -225,23 +275,40 @@ class CI_Table { // Is there a table heading to display? if (count($this->heading) > 0) { + $out .= $this->template['thead_open']; + $out .= $this->newline; $out .= $this->template['heading_row_start']; - $out .= $this->newline; + $out .= $this->newline; foreach($this->heading as $heading) { - $out .= $this->template['heading_cell_start']; - $out .= $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; + $out .= isset($heading['data']) ? $heading['data'] : ''; $out .= $this->template['heading_cell_end']; } $out .= $this->template['heading_row_end']; - $out .= $this->newline; + $out .= $this->newline; + $out .= $this->template['thead_close']; + $out .= $this->newline; } - + // Build the table rows if (count($this->rows) > 0) { + $out .= $this->template['tbody_open']; + $out .= $this->newline; + $i = 1; foreach($this->rows as $row) { @@ -258,15 +325,33 @@ class CI_Table { foreach($row as $cell) { - $out .= $this->template['cell_'.$name.'start']; + $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 === "" OR $cell === NULL) { $out .= $this->empty_cells; } else { - $out .= $cell; + if ($function !== FALSE && is_callable($function)) + { + $out .= $function($cell); + } + else + { + $out .= $cell; + } } $out .= $this->template['cell_'.$name.'end']; @@ -275,6 +360,9 @@ class CI_Table { $out .= $this->template['row_'.$name.'end']; $out .= $this->newline; } + + $out .= $this->template['tbody_close']; + $out .= $this->newline; } $out .= $this->template['table_close']; @@ -321,7 +409,7 @@ class CI_Table { return FALSE; } - $this->heading = $query->list_fields(); + $this->heading = $this->_prep_args($query->list_fields()); } // Next blast through the result array and build out the rows @@ -330,7 +418,7 @@ class CI_Table { { foreach ($query->result_array() as $row) { - $this->rows[] = $row; + $this->rows[] = $this->_prep_args($row); } } } @@ -353,21 +441,15 @@ class CI_Table { $i = 0; foreach ($data as $row) - { - if ( ! is_array($row)) - { - $this->rows[] = $data; - break; - } - + { // If a heading hasn't already been set we'll use the first row of the array as the heading if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) { - $this->heading = $row; + $this->heading = $this->_prep_args($row); } else { - $this->rows[] = $row; + $this->rows[] = $this->_prep_args($row); } $i++; @@ -391,7 +473,7 @@ class CI_Table { } $this->temp = $this->_default_template(); - foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val) + foreach (array('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])) { @@ -412,12 +494,18 @@ class CI_Table { { return array ( '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>', diff --git a/user_guide/libraries/table.html b/user_guide/libraries/table.html index 1c67e4b8e..b233dbf85 100644 --- a/user_guide/libraries/table.html +++ b/user_guide/libraries/table.html @@ -2,17 +2,16 @@ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>HTML Table Class : CodeIgniter User Guide</title> +<title>CodeIgniter User Guide : HTML Table Class</title> -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> +<link rel='stylesheet' type='text/css' media='all' href='http://codeigniter.com/user_guide/userguide.css' /> -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> +<script type="text/javascript" src="http://codeigniter.com/user_guide/nav/nav.js"></script> +<script type="text/javascript" src="http://codeigniter.com/user_guide/nav/prototype.lite.js"></script> +<script type="text/javascript" src="http://codeigniter.com/user_guide/nav/moo.fx.js"></script> +<script type="text/javascript" src="http://codeigniter.com/user_guide/nav/user_guide_menu.js"></script> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv='expires' content='-1' /> <meta http-equiv= 'pragma' content='no-cache' /> <meta name='robots' content='all' /> @@ -24,7 +23,7 @@ <!-- START NAVIGATION --> <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> +<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="http://codeigniter.com/user_guide/images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> <div id="masthead"> <table cellpadding="0" cellspacing="0" border="0" style="width:100%"> <tr> @@ -199,6 +198,14 @@ $this->table->set_template($tmpl); <code>$this->table->add_row('Blue', 'Red', 'Green');</code> <code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code> +<p>If you would like to set an individual cell's tag attributes, you can use an associative array for that cell. The associative key <dfn>'data'</dfn> defines the cell's data. Any other key => val pairs are added as <dfn>key='val'</dfn> attributes to the tag:</p> + +<code>$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);<br /> +$this->table->add_row($cell, 'Red', 'Green');<br /> +<br /> +// generates<br /> +// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td> +</code> <h2>$this->table->make_columns()</h2> @@ -274,6 +281,22 @@ $this->table->add_row('John', 'Saturday', 'Overnight');<br /> echo $this->table->generate(); </code> +<h2>$this->table->function</h2> + +<p>Allows you to specify a native PHP function or a valid function array object to be applied to all cell data.</p> + +<code>$this->load->library('table');<br /> +<br /> +$this->table->set_heading('Name', 'Color', 'Size');<br /> +$this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');<br /> +<br /> +$this->table->function = 'htmlspecialchars';<br /> +echo $this->table->generate();<br /> +</code> + +<p>In the above example, all cell data would be ran through PHP's <dfn>htmlspecialchars()</dfn> function, resulting in:</p> + +<code><td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td></code> </div> <!-- END CONTENT --> |