Use tfoot tag in Drupal for summary/totals in tables

The Drupal 6 theme_table function is missing the ability to use tfoot tag for totals etc. This become hard when you add a jQuery table sorter and the totals start jumping around with the other rows. I made my own table function to get around this problem in Drupal 6. AS for now not even Druapl 7 have this feature.

You can implement this as your own theme_hook but I think it's not necessary.

<?php

/**
 * Replacement of Drupal theme function allowing tfooter
 *
 * @param unknown_type $header
 * @param unknown_type $footer
 * @param unknown_type $rows
 * @param unknown_type $attributes
 * @param unknown_type $caption
 * @return unknown
 */
function mymodule_theme_table($header, $footer = NULL, $rows, $attributes = array(), $caption = NULL) {

 
// Add sticky headers, if applicable.
 
if (count($header)) {
   
drupal_add_js('misc/tableheader.js');
   
// Add 'sticky-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
   
$attributes['class'] = empty($attributes['class']) ? 'sticky-enabled' : ($attributes['class'] .' sticky-enabled');
  }

 
$output = '<table'. drupal_attributes($attributes) .">\n";

  if (isset(
$caption)) {
   
$output .= '<caption>'. $caption ."</caption>\n";
  }

 
// Format the table header:
 
if (count($header)) {
   
$ts = tablesort_init($header);
   
// HTML requires that the thead tag has tr tags in it followed by tbody
    // tags. Using ternary operator to check and see if we have any rows.
   
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
    foreach (
$header as $cell) {
     
$cell = tablesort_header($cell, $header, $ts);
     
$output .= _theme_table_cell($cell, TRUE);
    }
   
// Using ternary operator to close the tags based on whether or not there are rows
   
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
  }
  else {
   
$ts = array();
  }
 
  if (
count($footer)) {
    
// HTML requires that the tfoot tag has tr tags in it followed by tbody
     // tags. Using ternary operator to check and see if we have any rows.
    
$output .= (count($rows) ? ' <tfoot><tr>' : ' <tr>');
    
$i = 0;
     foreach (
$footer as $cell) {
      
$cell = tablesort_cell($cell, $footer, $ts, $i++);
      
$output .= _theme_table_cell($cell);
     }
    
// Using ternary operator to close the tags based on whether or not there are rows
    
$output .= (count($rows) ? " </tr></tfoot>\n" : "</tr>\n");
   }

 
// Format the table rows:
 
if (count($rows)) {
   
$output .= "<tbody>\n";
   
$flip = array('even' => 'odd', 'odd' => 'even');
   
$class = 'even';
    foreach (
$rows as $number => $row) {
     
$attributes = array();

     
// Check if we're dealing with a simple or complex row
     
if (isset($row['data'])) {
        foreach (
$row as $key => $value) {
          if (
$key == 'data') {
           
$cells = $value;
          }
          else {
           
$attributes[$key] = $value;
          }
        }
      }
      else {
       
$cells = $row;
      }
      if (
count($cells)) {
       
// Add odd/even class
       
$class = $flip[$class];
        if (isset(
$attributes['class'])) {
         
$attributes['class'] .= ' '. $class;
        }
        else {
         
$attributes['class'] = $class;
        }

       
// Build row
       
$output .= ' <tr'. drupal_attributes($attributes) .'>';
       
$i = 0;
        foreach (
$cells as $cell) {
         
$cell = tablesort_cell($cell, $header, $ts, $i++);
         
$output .= _theme_table_cell($cell);
        }
       
$output .= " </tr>\n";
      }
    }
   
$output .= "</tbody>\n";
  }

 
$output .= "</table>\n";
  return
$output;
}
?>

And this is how I implement it

<?php
//instead of:
//$out .= theme('table', $tableHeaders, $tableRows, $attributes = array('class' => 'team-projects tablesorter', 'id' => 'team-projects-table'));
$out .= mymodule_theme_table($tableHeaders, $sum_row, $tableRows, $attributes = array('class' => 'team-projects tablesorter', 'id' => 'team-projects-table'));
?>

Based on the patch http://drupal.org/files/theme_table_tfoot-806982-10.patch described here http://drupal.org/node/806982

Knowledge keywords: