Jira Courses, Training and Consulting: Sparxsys Trainings
Thanks for coming here, I hope you are enjoying learning here, I have also written some books in case you want to learn a bit more :)
If you need my help with Drupal, Linux, Jira, Scripting, Automation or want to contact me then raise a ticket for me please :) and I will get back to you, promise. At Sparxsys we provide Atlassian consultancy services, reach out to me at ravi at sparxsys dot com

Table view with rows/columns switched

These days I am working on my other site www.ggsipu.info to build the database of all the colleges affiliated to GGSIPU. I created a Custom Content Type to have various fields like college address, phone, fax, email, rating, etc. During the counselling time the students wants to compare various colleges based on various parameters like infrastructure, campus and other things. Now I wanted to create a view where a table is shown with the names of the colleges in the header and these parameters on rows.

  1. This views snippet allows you to render a tableview with columns and rows switched. Suppose you have the following output:
  2.  
  3. Title   | Price 1 | Price 2
  4. ----------------------------
  5. Node #1 | $10     | $20
  6. Node #2 | $15     | $25
  7.  
  8. but what you really want is the node titles displayed in the header, and the prices as the rows:
  9.         | Node #1 | Node #2
  10. ----------------------------
  11. Price 1 | $10     | $15
  12. Price 2 | $20     | $25

I created a view table, but obviously it showed the college names in the rows as it is the dynamic part. I searched around a lot on drupal and google and found this amazing piece of code that transpose the rows/columns.

You just need to replace YOURTHEME with the name of the theme and VIEWNAME with the view name and that's it!

  1. <?php
  2. function YOURTHEME_views_view_table_VIEWNAME($view, $nodes, $type) {
  3.   $fields = _views_get_fields();
  4.  
  5.   // Table header
  6.   // Reserve first column for labels
  7.   $header = array('');
  8.   // Render first field of all nodes
  9.   foreach ($nodes as $node) {
  10.     $cell['data'] = views_theme_field('views_handle_field', $view->field[0]['queryname'], $fields, $view->field[0], $node, $view);
  11.     $cell['class'] = 'view-cell-header '. views_css_safe('view-field-'. $view->field[0]['queryname']);
  12.     $header[] = $cell;
  13.   }
  14.   // Nuke first field, so it won't get displayed in the rows anymore
  15.   unset($view->field[0]);
  16.  
  17.   // Table rows
  18.   $rows = array();
  19.   foreach ($view->field as $field) {
  20.     $row = array();
  21.     // First column is just the label
  22.     $cell['data'] = $field['label'];
  23.     $cell['class'] = views_css_safe('view-field-'. $field['queryname']);
  24.     $row[] = $cell;
  25.     // Add corresponding column for all nodes
  26.     foreach ($nodes as $node) {
  27.       $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
  28.       $cell['class'] = views_css_safe('view-field-'. $field['queryname']);
  29.       $row[] = $cell;
  30.     }
  31.     $rows[] = $row;
  32.   }
  33.  
  34.   return theme('table', $header, $rows, array('class' => 'show-table'));
  35. }
  36. ?>

It worked like a charm. If you are using sub theme of zen then replace YOURTHEME with "zen" instead of the name of your sub theme.

What next?
Now I want the students to be able to select at least 2 but maximum of 4 colleges using a list box and the view should display only those selected colleges. By exposing filters it creates a text box but not the list box because when you expose a text field it is becomes text field but when you expose a "select list text field" it becomes list box when you expose them.

There must be some way to achieve this.

Drupal Rocks!

Similar posts

Subscribe

* indicates required

Please confirm that you would like to from Sparxsys:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices.

Want to contact me?