Add a select to alter table headings down the left as apposed to at the top of a table (specifically useful if fields have very long titles)

e.g.:

<table>
<tr>
<th>Very very long field name with extra information</th>
<td>Node id x field value</td>
<td>Node id y field value</td>

...etc.
</tr>
<tr>
<th>Another very very long field name and even more information</th>
<td>Node id x field value</td>
<td>Node id y field value</td>

...etc.
</tr>

</table>

Comments

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Due to the complexity involved with tables, plus column sorting, this would probably need to be a separate style plugin. It's neat, but it strikes me as being used only very very rarely, and I don't think I'm going to put it in Views core. Sorry.

(As a workaround you might try the grid view, which does have a horizontal/vertical switch)

uberellis-dupe’s picture

Aw :( Forgive my ignorance - part of me hoped it was a simple row to column switch (Didn't consider sorting, truth be told o: ). I'll give grid view a shot! Thanks for the rapid response! :)

MadOverlord’s picture

Title: Vertical vs. Horizontal Titles for Table View » Vertical vs. Horizontal Titles for Table View (partial solution using theming)

Just posting this as a possible fix; I needed this kind of layout to nicely format a bunch of product information (20 or so fields), and the current layout options just didn't cut it. For this kind of stuff, a vertical table is just the ticket, since you can get the rows to line up in a nice way.

I couldn't find a style plugin that does this (if there is, let me know) so I hacked together a couple of theme files that change the unformatted output to a formatted table. There are several limitations on this (no grouping, no table headers) but it does what I need and may be helpful to the NPB (Next Poor Bastard).

You need to make your own versions of the style output file (originally views-view-unformatted.tpl.php) and the row style output file (originally views-view-fields.tpl.php) and tell your view to use the new versions. Here's the code for the style output:

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 *
 * Hacked to output a 2-column table.  Must also hack the specific
 * views-view-fields file!  Sorry, no headers!
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <table class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </table>
<?php endforeach; ?>

and for the row style output:

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 *
 * Hacked and simplified to do a 2-column table.  Ignores field separators.
 * All fields must have labels!  Must also hack the specific
 * views-view-unformatted file.  Sorry, no programmable headers!
 */
 
 $oddEven = "odd";
 
?>
<?php foreach ($fields as $id => $field): ?>
  <tr class="<?php print $oddEven; ?> views-field-<?php print $field->class; ?>">
      <td class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>
      </td>
      <td class="field-content">
        <?php print $field->content; ?>
      </td>
  </tr>
<?php if ($oddEven == "odd") { $oddEven = "even"; } else { $oddEven = "odd"; }; ?>
<?php endforeach; ?>
smaier’s picture

MadOverlord’s picture

Yes, that looks like it does the same thing. Wish I'd found it before I did the above hackery but both would seem to do the trick.

Thanks for taking the time to add the linkage; it will help TNPB (The Next Poor Bastard)

kiliweb’s picture

You can also override the output theme of the table style (something like : views-view-table.tpl.php)

Here the code :

<?php
// $Id: views-view-table.tpl.php,v 1.8 2009/01/28 00:43:43 merlinofchaos Exp $
/**
 * @file views-view-table.tpl.php
 * Template to display a view as a table.
 *
 * - $title : The title of this group of rows.  May be empty.
 * - $header: An array of header labels keyed by field id.
 * - $fields: An array of CSS IDs to use for each field id.
 * - $class: A class or classes to apply to the table, based on settings.
 * - $row_classes: An array of classes to apply to each row, indexed by row
 *   number. This matches the index in $rows.
 * - $rows: An array of row items. Each row is an array of content.
 *   $rows are keyed by row number, fields within rows are keyed by field ID.
 * @ingroup views_templates
 */
?>
<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  
  <tbody>
    <?php foreach ($rows[0] as $count => $row): ?>
      <tr>
	  <?php echo '<td>'.$header[$count].'</td><td>'.$row.'</td>'; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
mattbk’s picture

Will this template work with D7? For the life of me I can't get the display to change for the view I am working on.

mattbk’s picture

Never mind, I was assuming I needed to modify it when I really didn't. Works fine if you drop it into your theme folder.

gautamb’s picture

This was helpful but I'm having a small problem with it. It only displays 1 column. I have large table and I wanted 4 columns to be displayed. how can I modify it to display more than one?

eric.chenchao’s picture

Hi Gautabmb,

I have found a practical way to present vertical table by using jquery plugin.

You can download this js script:
http://plugins.jquery.com/project/tabletranspose

and write a simple custom module with the function, for example

function mymodulename_views_pre_render(&$view) {
  $view_id = 'yourviewname';
  if ($view->name == $view_id) {
    // we can access view arguments
    drupal_add_js(drupal_get_path('module', 'mymodulename') . '/jquery.tabletranspose.js', 'module');
    drupal_add_js('
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    // replace the element id and class belonging to your own table
    $(".view-id-' . $view_id . ' .views-table").tableTranspose().addClass("table-transpose-processed");
  });
}', 'inline');
  }
}

Voila! And you can also use sorting function.

Cheers, Eric

kuroneko007’s picture

Hi cityreader,

This sounds like a perfect solution to the problem that I have been searching for a long time.
However, I'm still pretty much a Drupal n00b, could you explain where I should insert the php code you provided? I take it I don't need to add any script tags to load the plugin as this is done by your php code.

Thanks!

kuroneko007’s picture

Never mind, I figured it out for myself. First time writing a custom module and first time using a jQuery plugin, but it works!

Just one thing, the above code wouldn't do anything until I removed the line
if (Drupal.jsEnabled) {....}

Had me stuck for about 2 days, but I learned a lot in that time!

Edit: Turns out that the variable Drupal.jsEnabled has been removed since Drupal 7, so checking for this was giving me a FALSE every time.

drupalgambatte’s picture

I wonder if this kind of a hack is possible for a node display, perhaps a particular content type;
It is possible to define fields and list them all one below another as an unformatted list, but can we convert this long list of "field name" = "field value" array into a two column table so that it is displayed neatly and nicely,

naught101’s picture

@Merlin: An excellent use-case for vertical tables is for comparisons, where there are few nodes, with many fields:
https://www.ohloh.net/p/compare?project_0=Joomla!&project_1=WordPress&pr...
or
http://www.phonescoop.com/phones/compare.php?p=3541,3768

While that's probably not useful for generic, include-everything views, it would certainly be useful with nodequeue, flag, or relation-based views.

Note that table sorting is not usually relevant in these kind of tables, since there's so few entities being shown. Grid view doesn't work for this use-case, because the fields need to be side-by side, and they aren't if there are some fields missing from some nodes.

naught101’s picture

https://drupal.org/project/views_hacks includes a module called "views_flipped_table" which works exactly as specified in the original post. Unfortunately the parent "module" is somewhat abandoned. The module appears to be pretty small, and well coded, although there are no tests. Is there any chance of it getting included in views? If so, I would be happy to do some work on getting into appropriate shape. What would be required?

naught101’s picture

Status: Closed (won't fix) » Fixed

I have taken over maintainership of the views_flipped_table, which has been split off from views_hacks, and is now available at https://drupal.org/project/views_flipped_table.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.