Add vertical striping to views table

Public

add the following to template.php (changed on line 39, 78, 113, 167)

</> CopyGet raw version
php
  1. /**
  2.  * implements template_preprocess_views_view_table
  3.  *
  4.  * add vertical striping to views tables
  5.  */
  6. function template_preprocess_views_view_table(&$vars) {
  7. $view = $vars['view'];
  8.  
  9. // We need the raw data for this grouping, which is passed in as $vars['rows'].
  10. // However, the template also needs to use for the rendered fields. We
  11. // therefore swap the raw data out to a new variable and reset $vars['rows']
  12. // so that it can get rebuilt.
  13. // Store rows so that they may be used by further preprocess functions.
  14. $result = $vars['result'] = $vars['rows'];
  15. $vars['rows'] = array();
  16. $vars['field_classes'] = array();
  17. $vars['header'] = array();
  18.  
  19. $options = $view->style_plugin->options;
  20. $handler = $view->style_plugin;
  21.  
  22. $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  23. $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
  24.  
  25. $fields = &$view->field;
  26. $columns = $handler->sanitize_columns($options['columns'], $fields);
  27.  
  28. $active = !empty($handler->active) ? $handler->active : '';
  29. $order = !empty($handler->order) ? $handler->order : 'asc';
  30.  
  31. $query = tablesort_get_query_parameters();
  32. if (isset($view->exposed_raw_input)) {
  33. $query += $view->exposed_raw_input;
  34. }
  35.  
  36. // Fields must be rendered in order as of Views 2.3, so we will pre-render
  37. // everything.
  38. $renders = $handler->render_fields($result);
  39.  
  40. $odd = true;
  41.  
  42. foreach ($columns as $field => $column) {
  43. // Create a second variable so we can easily find what fields we have and what the
  44. // CSS classes should be.
  45. $vars['fields'][$field] = drupal_clean_css_identifier($field);
  46. if ($active == $field) {
  47. $vars['fields'][$field] .= ' active';
  48. }
  49.  
  50. // render the header labels
  51. if ($field == $column && empty($fields[$field]->options['exclude'])) {
  52.  
  53. $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
  54. if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
  55. $vars['header'][$field] = $label;
  56. }
  57. else {
  58. $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
  59.  
  60. if ($active == $field) {
  61. $initial = ($order == 'asc') ? 'desc' : 'asc';
  62. }
  63.  
  64. $title = t('sort by @s', array('@s' => $label));
  65. if ($active == $field) {
  66. $label .= theme('tablesort_indicator', array('style' => $initial));
  67. }
  68.  
  69. $query['order'] = $field;
  70. $query['sort'] = $initial;
  71. $link_options = array(
  72. 'html' => TRUE,
  73. 'attributes' => array('title' => $title),
  74. 'query' => $query,
  75. );
  76. $vars['header'][$field] = l($label, $_GET['q'], $link_options);
  77. }
  78.  
  79. $vars['header_classes'][$field] = $odd ? 'col-odd' : 'col-even';
  80.  
  81. // Set up the header label class.
  82. if ($fields[$field]->options['element_default_classes']) {
  83. $vars['header_classes'][$field] .= " views-field views-field-" . $vars['fields'][$field];
  84. }
  85. $class = $fields[$field]->element_label_classes(0);
  86. if ($class) {
  87. if ($vars['header_classes'][$field]) {
  88. $vars['header_classes'][$field] .= ' ';
  89. }
  90. $vars['header_classes'][$field] .= $class;
  91. }
  92. // Add a CSS align class to each field if one was set
  93. if (!empty($options['info'][$field]['align'])) {
  94. $vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  95. }
  96.  
  97. // Add a header label wrapper if one was selected.
  98. if ($vars['header'][$field]) {
  99. $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
  100. if ($element_label_type) {
  101. $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
  102. }
  103. }
  104.  
  105. }
  106.  
  107. // Add a CSS align class to each field if one was set
  108. if (!empty($options['info'][$field]['align'])) {
  109. $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
  110. }
  111.  
  112. // Render each field into its appropriate column.
  113. foreach ($result as $num => $row) {
  114. $vars['field_classes'][$field][$num] = $odd ? 'col-odd' : 'col-even';
  115.  
  116. // Add field classes
  117. if ($fields[$field]->options['element_default_classes']) {
  118. $vars['field_classes'][$field][$num] .= " views-field views-field-" . $vars['fields'][$field];
  119. }
  120. if ($classes = $fields[$field]->element_classes($num)) {
  121. if ($vars['field_classes'][$field][$num]) {
  122. $vars['field_classes'][$field][$num] .= ' ';
  123. }
  124.  
  125. $vars['field_classes'][$field][$num] .= $classes;
  126. }
  127. $vars['field_attributes'][$field][$num] = array();
  128.  
  129. if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
  130. $field_output = $renders[$num][$field];
  131. $element_type = $fields[$field]->element_type(TRUE, TRUE);
  132. if ($element_type) {
  133. $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
  134. }
  135.  
  136. // Don't bother with separators and stuff if the field does not show up.
  137. if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
  138. continue;
  139. }
  140.  
  141. // Place the field into the column, along with an optional separator.
  142. if (!empty($vars['rows'][$num][$column])) {
  143. if (!empty($options['info'][$column]['separator'])) {
  144. $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
  145. }
  146. }
  147. else {
  148. $vars['rows'][$num][$column] = '';
  149. }
  150.  
  151. $vars['rows'][$num][$column] .= $field_output;
  152. }
  153. }
  154.  
  155. // Remove columns if the option is hide empty column is checked and the field is not empty.
  156. if (!empty($options['info'][$field]['empty_column'])) {
  157. $empty = TRUE;
  158. foreach ($vars['rows'] as $num => $columns) {
  159. $empty &= empty($columns[$column]);
  160. }
  161. if ($empty) {
  162. foreach ($vars['rows'] as $num => &$column_items) {
  163. unset($column_items[$column]);
  164. unset($vars['header'][$column]);
  165. }
  166. }
  167. }
  168. if(!$fields[$field]->options['exclude']) {
  169. $odd = !$odd;
  170. }
  171. }
  172.  
  173. // Hide table header if all labels are empty.
  174. if (!array_filter($vars['header'])) {
  175. $vars['header'] = array();
  176. }
  177.  
  178. $count = 0;
  179. foreach ($vars['rows'] as $num => $row) {
  180. $vars['row_classes'][$num] = array();
  181. if ($row_class_special) {
  182. $vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
  183. }
  184. if ($row_class = $handler->get_row_class($num)) {
  185. $vars['row_classes'][$num][] = $row_class;
  186. }
  187. }
  188.  
  189. if ($row_class_special) {
  190. $vars['row_classes'][0][] = 'views-row-first';
  191. $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
  192. }
  193.  
  194. $vars['classes_array'] = array('views-table');
  195. if (empty($vars['rows']) && !empty($options['empty_table'])) {
  196. $vars['rows'][0][0] = $view->display_handler->render_area('empty');
  197. // Calculate the amounts of rows with output.
  198. $vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
  199. $vars['field_classes'][0][0] = 'views-empty';
  200. }
  201.  
  202.  
  203. if (!empty($options['sticky'])) {
  204. drupal_add_js('misc/tableheader.js');
  205. $vars['classes_array'][] = "sticky-enabled";
  206. }
  207. $vars['classes_array'][] = 'cols-' . count($vars['header']);
  208.  
  209. // Add the summary to the list if set.
  210. if (!empty($handler->options['summary'])) {
  211. $vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
  212. }
  213.  
  214. // Add the caption to the list if set.
  215. if (!empty($handler->options['caption'])) {
  216. $vars['caption'] = filter_xss_admin($handler->options['caption']);
  217. }
  218. else {
  219. $vars['caption'] = '';
  220. }
  221. }