Hello,

I hopelessly try to fix the following problem.
I have a view which holds nodes that have taxonomy terms. Depending on tid, I want the row to be a certain color.
For example if the node's taxonomy is set to 'moderate' then the row is highlighted green.
Following recommendations of issue #438138: Is it possible to display the tid as class in the unformatted style?, implementing garland_preprocess_hook with unformatted style in the garland template.php, clearing registry cache, but it doesn't work for me. I don't get the expected output, i. e. class="term-tid". I only get the usual template_preprocess_views_view_unformatted classes (views-row-first, views-row-odd views-row-even...)
Any and all help would be greatly appreciated.

Thanks

Comments

dww’s picture

Status: Active » Fixed

This sounds almost exactly like the row coloring of the issue queue views on this site, except we use the issue status field instead of a taxonomy term. #359122: Add an "Issue table" view style plugin to provide row coloring and the links at the top of the page is where we implemented that, and if you read that issue (and the patches there), that'll give you a good idea how to do this... Even if you don't do your own style plugin, the view template stuff we're doing in project_issue.module should be most of what you need. In particular, check out:

function project_issue_preprocess_views_view_table() from project_issue.module.

Good luck,
-Derek

Status: Fixed » Closed (fixed)

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

Oleksa-1’s picture

function chrysalis_preprocess_views_view_table($variables) {
  $view = $variables['view'];
  if ($view->plugin_name == 'table') {
    foreach ($view->result as $num => $result) {
      $variables['row_classes'][$num][] = "state-$result->field_myfield ";
      $variables['row_classes'][$num][] = "priority-$result->field_myfield ";
    }
  }
  $variables['class'] .= " table";
}

dww,
for me it is still not clear how to put these classes . I am now trying to use same steps like in
function project_issue_preprocess_views_view_table() from project_issue.module

If in my table I have field_myfield - how to enter it in my function?

dww’s picture

Your field has to be in the result set from your view. So you need to add those fields (perhaps as hidden fields) into your view. Then they should start showing up in that $result object.

Oleksa-1’s picture

Hi, dww, it really works, but... In my view i have these fields:

Fields

* [field_gallery_status_value] == Content: Status (field_gallery_status)
* [title] == Node: Title
* [nid] == Node: Nid
* [name] == Taxonomy: Term

And only if i put field nid ($variables['row_classes'][$num][] = "priority-$result->nid ";), it is displayd in tr class ( <tr class="odd state- priority-2">), with all other fields (e.g. $variables['row_classes'][$num][] = "priority-$result->field_gallery_status_value";) it is empty (<tr class="odd state- priority- ">)

I can not understand why ?

off’s picture

Subscribe

Oleksa-1’s picture

@OFF
all this snippets not needed anymore
there is module
views tablehighlighter

off’s picture

I just have how to solved this problem:

function iid_preprocess_views_view_table($variables) {
$view = $variables['view'];
if ($view->plugin_name == 'table') {
foreach ($view->result as $num => $result) {

$variables['row_classes'][$num][] = "$result->nid";
print_r($result);

}
}
$variables['class'] .= " table";
}

But thanks for the module, I'm going to trying this.

Sinan Erdem’s picture

Version: 6.x-2.5 » 7.x-3.x-dev

I really have a neat solution for this.

1. Activate module: http://drupal.org/project/color_field
2. Add a color field named "color" to taxonomy entity. (Got to taxonomy->manage fields)
3. Determine some colors for your terms by editing them.

4. In your view, add relationship "Content: Taxonomy terms on node"
5. Add taxonomy:color as a field, and exclude it from display.
6. In views format, table settings, add "color-[field_color]" for the "Row class".
7. Add a "Global text field" to the header of the view.
8. Choose "Text Format" as "Fully HTML" and paste below JS code into text area (just remember to change YOUR-VIEW-CLASS to your view's class):

<script type="text/javascript">
(function ($) {
$(document).ready(function() {

$(".YOUR-VIEW-CLASS tbody tr").each(function() {
if ( this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/) !== null ){
    var myColor = this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/)[1];
    $(this).css("background", "#" + myColor);
}
});

});
})(jQuery);
</script>
Sinan Erdem’s picture

Update to previous comment:

The code was not working for the items loaded via AJAX (pager or exposed filters, etc). I added the same code also after AJAX load is finished. Latest code is here:

<script type="text/javascript">
(function ($) {

$(document).ready(function() {
$(".issues-table tbody tr").each(function() {
if ( this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/) !== null ){
    var myColor = this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/)[1];
    $(this).css("background", "#" + myColor);
}
});
});

$(document).ajaxComplete(function() {
$(".issues-table tbody tr").each(function() {
if ( this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/) !== null ){
    var myColor = this.className.match(/(?:^|\s)color-(.*?)(?:$|\s)\b/)[1];
    $(this).css("background", "#" + myColor);
}
});
});

})(jQuery);
</script>
runeasgar’s picture

I am not certain this is relevant, but here's how I'm highlighting my issue tracker rows:

// Add classes to task rows
function mcn_intranet_preprocess_views_view_table(&$vars) {
	if ($vars['view']->name == 'tasks') {
		foreach ($vars['result'] as $key => $value) {
			array_push($vars['row_classes'][$key], strtolower($vars['result'][$key]->field_field_priority[0]['rendered']['#title']));
		}
	}
}