0

I have a view created /admin/structure/views and it is a table.

How do I add a class (f.ex. "bgred") for a specific TD (f.ex "title") in this table without using JavaScript ? I mean I have some hidden field (f.ex. "Status") and want to add class for field "Title" only when Status==1, is this possible ?

I want to add class to TD element not some div inside TD...

<td class="bgred">

0
1
1) click on desire field under 'Fields' in your view

2) select 'Style Settings' 

3) check 'Customize field HTML' checkbox

4) check 'Create a CSS class' checkbox

5) enter 'replace-this-class' in 'CSS class' text field

then you should look into hook_views_post_render - this where you can access the actual html output for each row. if you add a class to the field as described above, you will be able then do php find and replace based on any logic.

my_module_views_post_render(&$view, &$output, &$cache) {

    foreach ($view -> result as $result) {

        //not sure about that - you would need to debug that
        $result_status = $result -> status; 

        //your php logic and new class assignment
        if($result_status == 1) {
            $class = 'new-class';
        }

        //what you want to replace - again double check that
        $replace = '<td class="replace-this-class';

        //alter `$output` variable
        $output = str_replace($replace, '<td class="'. $class .'"', $output);

    }   

}

This is very ugly but it works. Of course you will need to look into the $output variable to make sure that you are targeting correct html.

2
  • Can I somehow add such class based on value from other field (like hidden one f.ex. "Status"), so when Status==1 I want to add class "bgred" ? – Michal Przybylowicz May 19 '14 at 14:56
  • that is a very very different question. I don't think adding it to TD is possible using views alone. Please update your question and see updated answer. – sylwester May 19 '14 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.