Download & Extend

Row Color decided on field data

Project:Views
Version:6.x-2.6
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

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

#1

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?

#2

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.

#3

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 :)

#4

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

#5

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.

#6

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

Here's how I did it:

<?php
/**
* 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.

#7

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 ?

#8

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:

<?php
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);
}
?>

#9

You can always override just theme_views_view_table and get all tables.

#10

Status:active» fixed

Thanks that worked for me, marking this issue fixed.

#11

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

#12

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()

#13

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.

#14

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?

#15

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?

#16

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

#17

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

#18

@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.

#19

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.

#20

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;
    }
  }
}
?>

#21

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?

#22

@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.

#23

Status:active» fixed

Setting back to fixed.

#24

Status:fixed» closed (fixed)

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

#25

[deleted]

#26

nobody click here