Hello, I want to colour a Views result text.

I have created a 'select list' field type. So I want the text to be coloured when Views displays the results.
For example: red for "Bad", yellow for "more less", green for "good"
I thought of doing it in CSS, but I will need some IF conditions in PHP code, right?
thanks.

Comments

nevets’s picture

Look at the theme documentation for views, you can override the theme function for a single field. In this case I would add a class to the surround div that reflects the value of the field and then add css to apply the color based on the classes.

mortenson’s picture

Ok, I can build 3 new clases in style.css, one for red text, another for green text, another for yellow text.

now, If I edit my views, and then go to theme:Information, I can see the field I want get colored.
If I double click on:

Field Contenido: Text: opinion (field_select) (ID: field_select_value): views-view-field.tpl.php

I just get:

print $output;

Should I edit this code? how?

mortenson’s picture

still stuck here

mortenson’s picture

ok finally did it!

Here is how:

Create 3 new clases in your file style.css

div.red {
color: red;
}

div.yellow {
color: orange;
}

div.green {
color: #33cc00;
}

Now create this file in the theme folder (the one you are using):views-view-field--viewname--fieldname-value.tpl.php

In my case I named the file: views-view-field--ppppppp--field-select-value.tpl.php

Now put the code you need in that file.

I used:

  switch ( $output ) {
  case 'Excelente' :
    $css_class = 'green';
    break;

case 'Bueno' :
    $css_class = 'green';
    break;

  case 'Regular':
    $css_class = 'yellow';
    break;

  default:   // I am assuming the value is one of three states
               // and catching the regular value under the default
    $css_class = 'red';
    break;
  }
print $css_class;"> print $output;

the $output variable contains what it is being outputed in your field.

good luck!

amarula’s picture

just wonderd how did you passed the div/ class value so that the right css will be activated ?
Thanks,
Rami