Drupal Table Display

Drupal provides nice theming options for table display. However, various options available using tabular display are not documented well. This article will try to list various options to use when creating tables using drupal theme functions.

Creating a table display:

<?php
$header
= array('Header1', 'Header2', 'Header3');
$rows =array();
$rows[] = array('Value1Row1', 'Value2Row1', 'Value3Row1');
$rows[] = array('Value4Row2', 'Value5Row2', 'Value6Row2');
$rows[] = array('Value7Row2', 'Value8Row2', 'Value9Row3');
print
theme('table',  array('header' => $header, 'rows' => $rows));
?>

Display table with border=1

<?php
theme
('table',  array('header' => $header, 'rows' => $rows, 'attributes' => array('border' => 1)));
?>

Adding Class and Id to a table

<?php
theme
('table', array('header' => $header, 'rows' => $data_rows
, 'attributes' => array('id' => array('table-id'),  'class'   => array('table-class'))));
?>

Row specific classes

<?php
$rows
[] = array (
   
'data' => array('A', 'B', 'C',)
  ,
'class' => array('table-row-class')
  );
?>

Rowspan and Colspan

<?php
$rows
[] = array(
   
'data' => array(
      array(
'data' => 'A', 'rowspan' => 2)
    , array(
'data' => 'B', 'rowspan' => 2)
    , array(
'data' => 'C', 'colspan' => 2)
    ),
   
'class' => array('table-row-class')
  );
?>

Other Options

<?php
theme
('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('border' => '1')
,
'caption' => 'Caption data here', 'empty' =>t('No Data Available.')))
?>