Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Table Heading

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
lynlyn
Smarty n00b


Joined: 06 Jul 2006
Posts: 3

PostPosted: Thu Jul 06, 2006 3:10 am    Post subject: Table Heading Reply with quote

I don't know if anyone had already suggested this feature (table heading for smarty's html_table)

apparently, smarty's current html_table (see http://smarty.php.net/manual/en/language.function.html.table.php ) can only specify the tr and td tags... Observe the table width...

if i want to create table like this (below)


with the use of smarty, i have to use 2 smarty html_table (1st, for the heading then the 2nd for the actual data)... however using 2 tables is not advisable because what if for the heading table i had accidentally specified different col width from the data table (maybe due to careless programming)... see below
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 06, 2006 5:03 am    Post subject: Reply with quote

hmmm. Perhaps allow the cols attribute to optionally be an array of strings? If it is, then the cols value is computed from the count of the array elements and the TH element values are taken from each element of the array? Would a th_attr have to be added? Seems that some discussion is in order.
Back to top
View user's profile Send private message
lynlyn
Smarty n00b


Joined: 06 Jul 2006
Posts: 3

PostPosted: Thu Jul 06, 2006 7:25 am    Post subject: Reply with quote

hmm... i think adding th_attr is better... then auto compute cols based on the value of th_attr (count(th_attr)) Question
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 06, 2006 2:46 pm    Post subject: Reply with quote

lynlyn wrote:
hmm... i think adding th_attr is better... then auto compute cols based on the value of th_attr (count(th_attr)) Question


But that alone wouldn't give you the content for the TH's unless th_attr was associative which is unline the other *_attr attributes.
Back to top
View user's profile Send private message
lynlyn
Smarty n00b


Joined: 06 Jul 2006
Posts: 3

PostPosted: Fri Jul 07, 2006 7:09 am    Post subject: Reply with quote

ah yes... your choice then Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jul 07, 2006 3:42 pm    Post subject: Reply with quote

Someone should review this patch. It adds a th_attr attribute (which works just like td_attr and tr_attr and modifies the semantics of the cols attribute. The cols attribute, while still optional, now allows either an integer value to specify the number of columns, or it allows a array of column names to be used for the TH elements or it allows a comma separated string for each of the column names (eg: cols="foo,bar") In the latter 2 cases, the number of elements determines the number of columns that will be rendered.

Possible problems:

- I've noticed that the html_table code has some possible bugs related to bad values being set (eg: try cols=0 for the existing code). I'm not too concerned about fixing these atm, but it might be worthwhile to address.

- I have not considered the effects of the funky vdir attributes et al on this patch.

Code:
Index: libs/plugins/function.html_table.php
===================================================================
RCS file: /repository/smarty/libs/plugins/function.html_table.php,v
retrieving revision 1.11
diff -u -u -r1.11 function.html_table.php
--- libs/plugins/function.html_table.php   21 Jan 2005 17:46:15 -0000   1.11
+++ libs/plugins/function.html_table.php   7 Jul 2006 15:30:29 -0000
@@ -46,7 +46,7 @@
     $table_attr = 'border="1"';
     $tr_attr = '';
     $td_attr = '';
-    $cols = 3;
+    $cols_count = 3;
     $rows = 3;
     $trailpad = '&';
     $vdir = 'down';
@@ -65,6 +65,19 @@
                 break;
 
             case 'cols':
+                if (is_array($_value) && !empty($_value)) {
+                    $cols = $_value;
+                    $cols_count = count($_value);
+                } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
+                    $cols = explode(',', $_value);
+                    $cols_count = count($cols);
+                } elseif (!empty($_value)) {
+                    $cols_count = (int)$_value;
+                } else {
+                    $cols_count = $cols;
+                }
+                break;
+
             case 'rows':
                 $$_key = (int)$_value;
                 break;
@@ -79,6 +92,7 @@
 
             case 'tr_attr':
             case 'td_attr':
+            case 'th_attr':
                 $$_key = $_value;
                 break;
         }
@@ -87,25 +101,33 @@
     $loop_count = count($loop);
     if (empty($params['rows'])) {
         /* no rows specified */
-        $rows = ceil($loop_count/$cols);
+        $rows = ceil($loop_count/$cols_count);
     } elseif (empty($params['cols'])) {
         if (!empty($params['rows'])) {
             /* no cols specified, but rows */
-            $cols = ceil($loop_count/$rows);
+            $cols_count = ceil($loop_count/$rows);
         }
     }
 
     $output = "<table $table_attr>\n";
 
+    if (is_array($cols)) {
+        for ($r=0; $r<$cols_count; $r++) {
+            $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
+            $output .= $cols[$r];
+            $output .= "</th>\n";
+        }
+    }
+
     for ($r=0; $r<$rows; $r++) {
         $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
-        $rx =  ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
+        $rx =  ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count;
 
-        for ($c=0; $c<$cols; $c++) {
-            $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
+        for ($c=0; $c<$cols_count; $c++) {
+            $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c;
             if ($inner!='cols') {
                 /* shuffle x to loop over rows*/
-                $x = floor($x/$cols) + ($x%$cols)*$rows;
+                $x = floor($x/$cols_count) + ($x%$cols_count)*$rows;
             }
 
             if ($x<$loop_count) {
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Jul 07, 2006 3:47 pm    Post subject: Reply with quote

Hi,

another additional feature would be a "caption" parameter für a table caption.

<table>
<caption></caption>
</table>

for barriere free reasons needed...

Maybe also a <thead> and <tbody> section should be sorround the <tr>. the former for the <th> section and the latter for the <tbody>..

Or maybe if the loop parameter is two dimensional, that one <tbody> is created for each dimension..

Another idea on this plugin... the "trailpad" parameter has & nbsp; as default value, but for semantic reasons it should be an empty string. I suppose the & nbsp; is only used, cause of the "border-problem" with ie, which can be worked arround with css.

Bye,
Markus

EDIT:
& nbsp; without space, but the phpBB strips it...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jul 07, 2006 4:31 pm    Post subject: Reply with quote

@kills: Thanks for the feedback! As for trailpad...I seem to recall some problems with older browsers and empty cells. Also, html_table doesn't really do anything special to support CSS except for allowing html attributes to be specified in the *_attr arrays.

Never-the-less, you gave good ideas. I've added support for caption and now also use the thead and tbody -- but I don't see the need to support tfoot, colgroup and col items. Nor do I think that multiple row headers should be supported. I'm also worried that this might break older browsers -- but of course, I can't test since I don't have access to any Smile

Everyone, please review/test the new patch. In addition to the attributes mentioned in the last post, there is now also a caption attribute.

Code:
Index: libs/plugins/function.html_table.php
===================================================================
RCS file: /repository/smarty/libs/plugins/function.html_table.php,v
retrieving revision 1.11
diff -u -u -r1.11 function.html_table.php
--- libs/plugins/function.html_table.php   21 Jan 2005 17:46:15 -0000   1.11
+++ libs/plugins/function.html_table.php   7 Jul 2006 16:30:26 -0000
@@ -15,12 +15,15 @@
  * Purpose:  make an html table from an array of data<br>
  * Input:<br>
  *         - loop = array to loop through
- *         - cols = number of columns
+ *         - cols = number of columns, comma separated list of column names
+ *                  or array of column names
  *         - rows = number of rows
  *         - table_attr = table attributes
+ *         - th_attr = table heading attributes (arrays are cycled)
  *         - tr_attr = table row attributes (arrays are cycled)
  *         - td_attr = table cell attributes (arrays are cycled)
  *         - trailpad = value to pad trailing cells with
+ *         - caption = text for caption element
  *         - vdir = vertical direction (default: "down", means top-to-bottom)
  *         - hdir = horizontal direction (default: "right", means left-to-right)
  *         - inner = inner loop (default "cols": print $loop line by line,
@@ -46,12 +49,13 @@
     $table_attr = 'border="1"';
     $tr_attr = '';
     $td_attr = '';
-    $cols = 3;
+    $cols_count = 3;
     $rows = 3;
     $trailpad = '&';
     $vdir = 'down';
     $hdir = 'right';
     $inner = 'cols';
+    $caption = '';
 
     if (!isset($params['loop'])) {
         $smarty->trigger_error("html_table: missing 'loop' parameter");
@@ -65,6 +69,19 @@
                 break;
 
             case 'cols':
+                if (is_array($_value) && !empty($_value)) {
+                    $cols = $_value;
+                    $cols_count = count($_value);
+                } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
+                    $cols = explode(',', $_value);
+                    $cols_count = count($cols);
+                } elseif (!empty($_value)) {
+                    $cols_count = (int)$_value;
+                } else {
+                    $cols_count = $cols;
+                }
+                break;
+
             case 'rows':
                 $$_key = (int)$_value;
                 break;
@@ -74,11 +91,13 @@
             case 'hdir':
             case 'vdir':
             case 'inner':
+            case 'caption':
                 $$_key = (string)$_value;
                 break;
 
             case 'tr_attr':
             case 'td_attr':
+            case 'th_attr':
                 $$_key = $_value;
                 break;
         }
@@ -87,25 +106,40 @@
     $loop_count = count($loop);
     if (empty($params['rows'])) {
         /* no rows specified */
-        $rows = ceil($loop_count/$cols);
+        $rows = ceil($loop_count/$cols_count);
     } elseif (empty($params['cols'])) {
         if (!empty($params['rows'])) {
             /* no cols specified, but rows */
-            $cols = ceil($loop_count/$rows);
+            $cols_count = ceil($loop_count/$rows);
         }
     }
 
     $output = "<table $table_attr>\n";
 
+    if (!empty($caption)) {
+        $output .= '<caption>' . $caption . "</caption>\n";
+    }
+
+    if (is_array($cols)) {
+        $output .= "<thead><tr>\n";
+        for ($r=0; $r<$cols_count; $r++) {
+            $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
+            $output .= $cols[$r];
+            $output .= "</tr></th>\n";
+        }
+        $output .= "</thead>\n";
+    }
+
+    $output .= "<tbody>\n";
     for ($r=0; $r<$rows; $r++) {
         $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
-        $rx =  ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
+        $rx =  ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count;
 
-        for ($c=0; $c<$cols; $c++) {
-            $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
+        for ($c=0; $c<$cols_count; $c++) {
+            $x =  ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c;
             if ($inner!='cols') {
                 /* shuffle x to loop over rows*/
-                $x = floor($x/$cols) + ($x%$cols)*$rows;
+                $x = floor($x/$cols_count) + ($x%$cols_count)*$rows;
             }
 
             if ($x<$loop_count) {
@@ -116,6 +150,7 @@
         }
         $output .= "</tr>\n";
     }
+    $output .= "</tbody>\n";
     $output .= "</table>\n";
     
     return $output;
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Jul 09, 2006 12:38 am    Post subject: Reply with quote

FYI: I committed a patch to CVS. It also has a fix for the TR/TH error I introduced in the last patch and it now also properly observes hdir. A manual patch for html_table is coming soon.

Thanks to all for the suggestions and testing!
Back to top
View user's profile Send private message
snlr
Smarty Rookie


Joined: 09 Jul 2003
Posts: 8
Location: Berlin

PostPosted: Fri Mar 23, 2007 10:14 am    Post subject: Reply with quote

Support for tfoot would be really nice though:
    1. "This division enables user agents to support scrolling of table bodies independently of the table head and foot."
    2. "When long tables are printed, the table head and foot information may be repeated on each page that contains table data." (w3.org/TR/html401/struct/tables.html#h-11.2.3)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP