Hey everyone,

I was wondering/hoping someone could point me in the direction or tell me how to do the following.

I have a view which holds data that contains a status field. Depending on the status I wan the row to be a certain color.

For example if the status is set to stalled then the row is highlighted red.

Any and all help would be greatly appreciated.

Comments

moshe weitzman’s picture

Views does not support this yet. But it could nicely do this, and I think it would be useful. I suggest that this be a Row style for tables just like Lists have. The resulting row style form would probably just be a php textarea that gets the $row as input variable. The php should emit a class to be added to the row. Maybe this feature could put classes on cells as well.

It would be up to the author to add css that colors the row according to the class. Or Views could provide this?

merlinofchaos’s picture

You can do this without a row style just be implementing THEMENAME_preprocess_views_view_table__VIEWNAME(&$vars).

You could go through the rows and add a class as you need. Then in views-view-table--viewname.tpl.php make sure that class is applied; you may not need the .tpl file at all as I believe there is already a variable for a row class, but I can't remember for sure.

This is the kind of thing I shy away from making UI widgets for, because there is a slippery slope I am worried about sliding falling down here. So this is the sort of thing that should be done via theming.

PMatwyuk’s picture

Thanks for the info Moshe!

I'll definately be looking into making this happen, however since we're on the topic of Views perhaps you could tell me if there is a feature I need, in existance.

What I'm looking for is the ability to change the value of the data right from the view itself.

So using the above example say I'm looking at a view of projects, and status is one of the fields I can see. I notice theres one that's completed but is listed as being active. I want to be able to switch it right there in the view from active to complete and update on the spot. This would save me from having to click that project, editing the project and saving the project.

Yes I know its not that hard to go in by why have 4+ clicks when you can have 1? It would also be handy to just allow admins or project managers have this function however I do believe that could easily be done in the hook_perm().

Again any and all information is greatly appreciated, thanks for the quick reply on this one :)

PMatwyuk’s picture

Thanks merlin, I'll definately be giving this a try. :)

merlinofchaos’s picture

Views doesn't support forms at all just now, though that's a planned future feature.

That said, you could probably put a form in the row style with theming, I think.

appel’s picture

I wanted to assign a class to all 'fixed' items in my table view.

Here's how I did it:

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

  foreach ($nodes as $node) {
    $row = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);

		if($node->node_data_field_<FIELDNAME> == <VALUE>)
		{
			$cell['class'] = "view-field ". views_css_safe('status-fixed');
		}
		else
		{
			$cell['class'] = "view-field ". views_css_safe('');
		}

        $row[] = $cell;
      }
    }
    $rows[] = $row;
  }
  return theme('table', $view->table_header, $rows);
}

This code should be put in template.php.

mimisiku’s picture

I have the same problem, but this piece of code didn't worked for me. I am a beginer in PHP and Drupal and maybe i mised a thing:

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

foreach ($nodes as $node) {
$row = array();
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== FALSE) {
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);

if($node->node_data_field_promovare == Promo)
{
$cell['class'] = "view-field". views_css_safe('-promo');
}
else
{
$cell['class'] = "view-field ". views_css_safe('-vip');
}

$row[] = $cell;
}
}
$rows[] = $row;
}
return theme('table', $view->table_header, $rows);

}

all the clases were made view-field-vip .... none of theme view-field-promo ... any sugestions ?

Anonymous’s picture

I got this to work using the snippet below. But my question is, can I inlcude multiple views in it? Or do I have to create an override for each view:

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

  foreach ($nodes as $node) {
    $row = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);

        if($node->casetracker_case_case_status_id == "8")
        {
            $cell['class'] = "view-field ". views_css_safe('approve');
        }
        else
        {
            $cell['class'] = "view-field ". views_css_safe('');
        }

        $row[] = $cell;
      }
    }
    $rows[] = $row;
  }
  return theme('table', $view->table_header, $rows);
}
merlinofchaos’s picture

You can always override just theme_views_view_table and get all tables.

Anonymous’s picture

Status: Active » Fixed

Thanks that worked for me, marking this issue fixed.

Anonymous’s picture

Does this code snippet still work in views2? I tried to load it into my 6.x template.php file and it seems to have had no affect on my views tables.

Anyone have any ideas as to what might be different?

txcrew

Anonymous’s picture

Version: 5.x-1.5 » 6.x-2.0-rc5
Status: Fixed » Active

Sorry, it is throwing an error, I had to empty the cache

Fatal error: Call to undefined function _views_get_fields()

domesticat’s picture

Status: Active » Closed (fixed)

Closed while closing all Views support requests with no activity for 6 months. If you still need help, you may re-open this issue at any time.

dboulet’s picture

Version: 6.x-2.0-rc5 » 6.x-2.6
Status: Closed (fixed) » Active

All the snippets provided above are for Drupal 5, but theming views is much less straight forward in Drupal 6. Can someone point me in the right direction for implementing this in Views 6.x.-2.x?

dboulet’s picture

This is proving to be very difficult in Drupal 6. I'm trying to use merlinofchaos' suggestion in #2 of using the view table preprocess function to add classes to the $vars['row_classes'] variable, but it is very difficult to base those classes on field data, since the original data in $vars['rows'] is destroyed by template_preprocess_views_view_table() and replaced with rendered field data. I am having to resort to implementing hook_theme() in my template.php file to override the preprocess functions, and then re-implement the entire preprocess function in my theme. Seems like a lot of work just to add a few classes to my table rows.

All this would be much easier if the original preprocess function stored the original row results in the $vars variable, so that other preprocess functions can access them later on, would this be possible?

merlinofchaos’s picture

The original data is still available in $view->result -- does that help?

moshe weitzman’s picture

project_issue module does this already and you can see it on drupal.org in these very queues.

dboulet’s picture

@merlinofchaos, I tried using $view->result, but I couldn't find a way to consistently reference the result corresponding to each row. For example, if I use a grouping field in my view, each group is rendered as its own table, and $vars['rows'] will only contain the results for that particular group—meanwhile, $vars['view']->result will always contain all results for the entire view.

@moshe_weitzman, thanks for the tip, I'll check out the Project issue module to see what I can learn from the way it handles views tables.

dboulet’s picture

I've looked at the Project issue module, and it uses data from $vars['view']->result in a preprocess function to add the class names. As i mentioned in my last post, this approach doesn't work when using a grouping field in your view. I've posted a patch that would allow a workaround for this problem, see #536994: Store row data for future use in views table preprocess functions.

dboulet’s picture

Status: Active » Fixed

With #536994 commited, adding classes to table rows should be as easy as adding code which resembles the following example to your theme's template.php:

<?php
/**
* Display a view as a table style.
*/
function mytheme_preprocess_views_view_table(&$vars) {
  $view = $vars['view'];
  if ($view->base_table == 'node') {
    foreach ($vars['result'] as $index => $data) {
      // Get existing classes
      $classes = is_array($vars['row_classes'][$index]) ? $vars['row_classes'][$index] : array();
      // Node language class
      if (isset($data->node_language) && !empty($data->node_language)) {
        $classes[] = 'node-lang-'. check_plain($data->node_language);
      }
      // Node status class
      switch ($data->node_status) {
        case "1":
          $classes[] = 'node-published';
          break;
        case "0":
          $classes[] = 'node-unpublished';
          break;
      }
      $vars['row_classes'][$index] = $classes;
    }
  }
}
?>
aren cambre’s picture

Status: Fixed » Active

Is there a concept of cell classes? E.g., what if I wanted table cells to have different colors depending on their values?

dboulet’s picture

@Aren Cambre, One way that you can do this is add the classes to the row, and then target specific cells in your CSS. For example:

table tr.classname td.views-field-fieldname {
  background-color: #f00;
}

Just replace 'classname' with the class and 'fieldname' with the name of the specific field.

dboulet’s picture

Status: Active » Fixed

Setting back to fixed.

Status: Fixed » Closed (fixed)

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

trevorkjorlien’s picture

[deleted]

mitchell’s picture

eabquina’s picture

Issue summary: View changes

Had a problem first the views table highlighter doesn't work on Simplenews HTML Mails that was generated through Emogrifier.

Then just recently, I realized that if I just copied the Views Table Highlighter .css classes to my theme, it works perfectly on the newsletters (simplenews) sent...