Is there a way to 'flip' views so the nodes are in the columns and the fields are in the rows? It'd be useful for comparing a few nodes side-by-side

Comments

nevets’s picture

You could do this with a table view and by theming the view "flip" the default presentation of the information.

merlinofchaos’s picture

Status: Active » Fixed

There is no way to do this in the UI, but as nevets says, you can theme the view and restructure the output as you like. This is a fairly complex task as it uses tables and in Views 1 tables are kind of a pain to theme, but it is possible.

mdowsett’s picture

It sounds like a pain...but I'm willing to try.

Can you give some example code?

This kind of thing would sure help with things like product comparison charts.

nevets’s picture

Ok, make a table view, make sure it works. Then add the following function in your themes template.php file. You will need to replace VIEWNAME with your views name.

function phptemplate_views_view_table_VIEWNAME($view, $nodes, $type) {
  $fields = _views_get_fields();

  $r = 0;
  
  foreach ( $view->table_header as $header ) {
  	$rows[$r][] = $header;
  	$r++;
  }
  
  $r = 0;
  foreach ($view->field as $field) {
    if ($fields[$field['id']]['visible'] !== FALSE) {
    	foreach ($nodes as $node) {
	      $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
	      $cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
	      $rows[$r][] = $cell;
	    }
	    $r++;
    }
  }
  return theme('table', array(), $rows);
}
mdowsett’s picture

amazing! it worked!

I don't think I've ever had a question answered so well and quickly with it working. And I'm no coder by any means but I did it! (I'm proud).

Fantastic work nevets

merlinofchaos’s picture

Just popping in to say "thank you" to nevets for helping out! It's appreciated!

mdowsett’s picture

Category: support » feature
Status: Fixed » Active

I'd like to suggest this get's worked into Views some how...it'd be nice to have an option in the views creation to "flip" it.

What d'ya think?!

sun’s picture

Title: nodes in the columns » Howto: Flip columns and rows in table output
Component: node data » Documentation
Category: feature » task

Can someone add this to the handbooks, please?

mbeenon’s picture

Hmm, tried this code, didnt work. using drupal 6 and views 2

merlinofchaos’s picture

Version: 5.x-1.6

That's why.

mbeenon’s picture

So then the code must be different! Tell me!

mbeenon’s picture

please and thankyou.

faunapolis’s picture

Subscribing, interested in this too.

mdowsett’s picture

I thought there was a module to do this in D6....? Or even built-in to Views2...? I could be wrong...

pepeelfrances’s picture

I'm interesting too. There is also this http://drupal.org/node/174578#comment-819915 but it's for Drupal 5.X.

Someone know so how to flip columns and rows with drupal 6.x and with Wiews2 ?

Thanks :)

mjlF95’s picture

I'm trying to do this with a view created with the editablefields module http://drupal.org/project/editablefields using the view type "Editablefields - table- no form". I got the function posted in this thread to work properly on a regular table view. I also got it to apply to the editablefield table view by changing the function name to:

function phptemplate_views_editablefields_view_table_noform_VIEWNAME($view, $nodes, $type) {

I can see that it's worked properly on the first two rows but all of the editable fields do not display. I assume that it's because it's a different function that's being overwritten than what the flip-column/row code was originally written for.

I think this is the function from the editablefields module:

/**
 * Display the nodes of a view as a table.
 */
function theme_views_editablefields_view_table_noform($view, $nodes, $type) {
  $fields = _views_get_fields();

  $editable=_handle_editablefield_form_input($view, $nodes);

  foreach ($nodes as $node) {
    $row = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $cell['data'] = views_theme_field('views_editablefields_handle_field', $field['queryname'], $fields, $field, $node, $view);
        $cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
        $row[] = $cell;
      }
    }
    $rows[] = $row;
  }
  $html=theme('table', $view->table_header, $rows );
  
  if ($editable) {
      if ($nodes) {
	$form['editablefields-d-'.$view->name]['submit']= array('#type' => 'submit', '#value' => t('Update'));
	$form['editablefields-d-'.$view->name]['#value']=$html;
	drupal_process_form('editablefields-d-'.$view->name,$form);
	$eform=drupal_render($form);
	return $eform;
      }
    }
    return $html;
}

Is there anyone who could re-write this flip-column/row code to apply to views of this type?

mdowsett’s picture

now that I've upgraded that site I originally asked for this for to D6, I need a fix for this flip for D6....any ideas?

danieldd’s picture

Subscribing. Also very interested in a Views2/ Drupal 6 solution. Doesn't appear that http://drupal.org/node/174578 applies to Views 2.

In my case the main use would be for charts. None of existing Drupal chart modules provide functionality for flipping columns/ rows.

mdowsett’s picture

This may be a solution...I'm trying to figure out how to name the tpl file properly to make it work.

http://drupal.org/node/174578

mdowsett’s picture

were you ever able to do this (view flipping) with Drupal6?

nevets’s picture

I have not tried the module but people following this thread might find Views Crosstab useful

mdowsett’s picture

I tried Views Crosstab but couldn't get it to do what I wanted it to. But then again, I don't want to add anything up, just view the same data as the table view. I think the Crosstab module is for stuff very different.

esmerel’s picture

Status: Active » Closed (won't fix)

Original issue resolved with themeing/editable fields. If this is also a 2.x issue, a separate issue should be logged and referred back to this one as appropriate.