Been trying to figure out how to create custom views styles and am having no luck. I want to be able to output the data as a table but not like the default table view.

By default it does:

Label Label Label Label
Value Value Value Value

I need it to do:

Label Value
Label Value
Label Value

etc...

Could you please at least point me in the right direction and I'll pick it up from there...

CommentFileSizeAuthor
#13 form2table.pdf6.36 KBLEternity

Comments

LEternity’s picture

Hi!

I have a related question. I would like to get an output like this:

Label Label Label Label
Label Value Value Value Value
Label Value Value Value Value
Label Value Value Value Value
Label Value Value Value Value

Does anyone know how to do this? Any hint will be appreciated!

dawehner’s picture

Thats only for the first request

<?php
<?php
// $Id$
/**
 * @file
 *   preprocess example
 */

/*
Label1 Label1 Label3 Label4
Value1 Value2 Value3 Value4
Value5 ...

I need it to do:

Label1 Value1 Value5
Label2 Value2
Label3 Value3
 */


function hook_preprocess_views_view_table(&$vars) {
  $header = $vars['header'];
  $rows = $vars['rows'];

  // fill the labels
  $header_ = array();
  $rows_ = array();
  $row = array();
  foreach ($header as $field => $label) {
    $row[$field] = $label;
  }
  $rows_[] = $row;

  // now we have to reorder the rows
  /* Before:
    array(array(1, 2, 3)
          array(4, 5, 6))
    After:
    array(array(1, 4),
          array(2, 5),
          array(3, 6))
   */
  $rows_ = revert_array_dim_2($rows);

  // set the variables
  $vars['header'] = $header_;
  $vars['rows'] = $rows_;
}

function revert_array_dim_2($array) {
  foreach ($array as $key1 => $val1) {
    foreach($val1 as $key2 => $val2)
      $return[$key2][$key1] = $val2;
  }

  return $return;
}
?>

I did only tested the revert_array_dim_2 function but this worked fine.

@LEternity
Could you please write label und value numbers like in my code, i don't know whether you want to reorder the rows

mstef’s picture

@dereine: Thank you. I'm assuming I can just add that into any module. I do not want to override all views that use a table output - only a handful. Is there a way to make this a new type of table output, or a way to specify which views I want to use this with?

Thanks again

dawehner’s picture

you have the full view object in $vars['view'] to decide whether you want to run this code.

mstef’s picture

Did you test this? I added it (unedited) to a helper module and rechecked the views and nothing has changed..

dawehner’s picture

as i sad i just tested the helper procedure.

Perhaps you could see whether $rows_ has the right content.

mstef’s picture

Title: Data Output as Table - Label Column & Row Column » Data Output as Table - Label Column & Value Column

warning: implode() [function.implode]: Bad arguments. in /var/www/sites/all/modules/views/theme/views-view-table.tpl.php on line 33.

dawehner’s picture

Here is a new version.

This time tested :)

<?php

function custom_preprocess_views_view_table(&$vars) {
  $header = $vars['header'];
  $rows = $vars['rows'];

  // now we have to reorder the rows
  /* Before:
    array(array(1, 2, 3)
          array(4, 5, 6))
    After:
    array(array(1, 4),
          array(2, 5),
          array(3, 6))
   */
  $rows_ = revert_array_dim_2($rows);

  // fill the labels
  $header_ = array();
  $i = 0;
  foreach ($header as $field => $label) {
    $rows_[$i][0] = $label;
    ksort($rows_[$i]);
    $i ++;
  }

  // set the variables
  $vars['header'] = $header_;
  $vars['rows'] = $rows_;

  // recalc row css
  unset($vars['row_classes']);
  $i = 0;
  foreach ($vars['rows'] as $key => $row) {
    $vars['row_classes'][][0] = (($key % 2) == 0) ? 'odd' : 'even';
    $i++;
  }
}

function revert_array_dim_2($array) {
  $i = 1;
  foreach ($array as $key1 => $val1) {
    $j = 0;
    foreach($val1 as $key2 => $val2) {
      $return[$j][$i] = $val2;
      $j++;
    }
    $i++;
  }

  return $return;
}
?>

Not as easy as i thought at the beginning

mstef’s picture

Thank you very much...not easy at all. I appreciate it. I usually have no problem writing this stuff myself, I've just never done anything custom for views so I had no idea where to start. I'll have to look over the API to find out how to exclude this code from the views I don't want to alter.

dawehner’s picture

@LEternity

your part is really quite easy :) but i asked you something

LEternity’s picture

@dereine: Sorry it took me so long to get back to you. I really appreciate your help. Here you go:

LabelX1 LabelX2 LabelX3 LabelX4
LabelY1 Value1 Value2 Value3 Value4
LabelY2 Value5 Value6 Value7 Value8

I think the difficult part in this are the x and y axis labels...

dawehner’s picture

mh can you post how you see a default table?

so write a before and a after version, this would be nice!

LEternity’s picture

StatusFileSize
new6.36 KB

Sure, here you go:

Before

LabelX1 LabelX2 LabelX3 LabelX4
Value1 Value2 Value3 Value4
Value5 Value6 Value7 Value8

After

LabelX1 LabelX2 LabelX3 LabelX4
LabelY1 Value1 Value2 Value3 Value4
LabelY2 Value5 Value6 Value7 Value8

In other words: I would like to add labels in the second (after) version that are not present in the first (before) version. In an ideal world, the combination of LabelX1 and LabelY1 would determine what value you get for Value1.

I've attached the real world example that I would like to imitate. Please note that the grey areas are calculated values.

PS: I won't imitate its ugliness...

dawehner’s picture

Thats possible right now.

The first row seems to be the "title" of the node. So set the header of this table column to LABELX1. The Rest is possible by default.

LEternity’s picture

I think I'll just theme the tables since every one of them is different (unfortunately) and a function might not be the best solution. It's a huge data project...

Anonymous’s picture

Sorry for the noob question but where exactly does this code get inserted? I would appreciate specifics as I am not a coder at all. Very much appreciated. =)

esmerel’s picture

Status: Active » Closed (fixed)

General close of issues over 6 months old without activity.