Drupal - Theme table with columstriping


/ Published in: PHP
Save to your folder(s)

This snipped adds columnstriping to cells. It works fine for most cases, but will behave strange when combined with colspan.
Cells get an additional class 'even-col' or 'odd-col'


Copy this code and paste it in your HTML
  1. /**
  2.  * Return a themed table.
  3.  *
  4.  * @param $header
  5.  * An array containing the table headers. Each element of the array can be
  6.  * either a localized string or an associative array with the following keys:
  7.  * - "data": The localized title of the table column.
  8.  * - "field": The database field represented in the table column (required if
  9.  * user is to be able to sort on this column).
  10.  * - "sort": A default sort order for this column ("asc" or "desc").
  11.  * - Any HTML attributes, such as "colspan", to apply to the column header cell.
  12.  * @param $rows
  13.  * An array of table rows. Every row is an array of cells, or an associative
  14.  * array with the following keys:
  15.  * - "data": an array of cells
  16.  * - Any HTML attributes, such as "class", to apply to the table row.
  17.  *
  18.  * Each cell can be either a string or an associative array with the following keys:
  19.  * - "data": The string to display in the table cell.
  20.  * - "header": Indicates this cell is a header.
  21.  * - Any HTML attributes, such as "colspan", to apply to the table cell.
  22.  *
  23.  * Here's an example for $rows:
  24.  * @verbatim
  25.  * $rows = array(
  26.  * // Simple row
  27.  * array(
  28.  * 'Cell 1', 'Cell 2', 'Cell 3'
  29.  * ),
  30.  * // Row with attributes on the row and some of its cells.
  31.  * array(
  32.  * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky'
  33.  * )
  34.  * );
  35.  * @endverbatim
  36.  *
  37.  * @param $attributes
  38.  * An array of HTML attributes to apply to the table tag.
  39.  * @param $caption
  40.  * A localized string to use for the <caption> tag.
  41.  * @return
  42.  * An HTML string representing the table.
  43.  */
  44. function MyTheme_table($header, $rows, $attributes = array(), $caption = NULL) {
  45. $output = '<table'. drupal_attributes($attributes) .">\n";
  46.  
  47. if (isset($caption)) {
  48. $output .= '<caption>'. $caption ."</caption>\n";
  49. }
  50.  
  51. // Format the table header:
  52. if (count($header)) {
  53. $ts = tablesort_init($header);
  54. $output .= ' <thead><tr>';
  55. $i = 0;
  56. foreach ($header as $cell) {
  57. $zebra = $i % 2 ? 'even-col' : 'odd-col';
  58. if (is_array($cell)) {
  59. if (isset($cell['class'])) {
  60. $cell['class'] .= " $zebra";
  61. }
  62. else {
  63. $cell['class'] = $zebra;
  64. }
  65. }
  66. else {
  67. $cell = array('data' => $cell, 'class' => $zebra);
  68. }
  69. $cell = tablesort_header($cell, $header, $ts);
  70. $output .= _theme_table_cell($cell, TRUE);
  71. $i++;
  72. }
  73. $output .= " </tr></thead>\n";
  74. }
  75.  
  76. // Format the table rows:
  77. $output .= "<tbody>\n";
  78. if (count($rows)) {
  79. $flip = array('even' => 'odd', 'odd' => 'even');
  80. $class = 'even';
  81. foreach ($rows as $number => $row) {
  82. $attributes = array();
  83.  
  84. // Check if we're dealing with a simple or complex row
  85. if (isset($row['data'])) {
  86. foreach ($row as $key => $value) {
  87. if ($key == 'data') {
  88. $cells = $value;
  89. }
  90. else {
  91. $attributes[$key] = $value;
  92. }
  93. }
  94. }
  95. else {
  96. $cells = $row;
  97. }
  98.  
  99. // Add odd/even class
  100. $class = $flip[$class];
  101. if (isset($attributes['class'])) {
  102. $attributes['class'] .= ' '. $class;
  103. }
  104. else {
  105. $attributes['class'] = $class;
  106. }
  107.  
  108. // Build row
  109. $output .= ' <tr'. drupal_attributes($attributes) .'>';
  110. $i = 0;
  111. foreach ($cells as $cell) {
  112. $zebra = $i % 2 ? 'even-col' : 'odd-col';
  113. if (is_array($cell)) {
  114. if (isset($cell['class'])) {
  115. $cell['class'] .= " $zebra";
  116. }
  117. else {
  118. $cell['class'] = $zebra;
  119. }
  120. }
  121. else {
  122. $cell = array('data' => $cell, 'class' => $zebra);
  123. }
  124.  
  125. $cell = tablesort_cell($cell, $header, $ts, $i++);
  126. $output .= _theme_table_cell($cell);
  127. }
  128. $output .= " </tr>\n";
  129. }
  130. }
  131.  
  132. $output .= "</tbody></table>\n";
  133. return $output;
  134. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.