How can one add a class to the rendered table when a view is outputted as table?

In html-list format for example one can add a class to a wrapper OR/AND to the list. Is this possible to the table as well?


Members fund testing for the Drupal project. Drupal Association Learn more


eridolfi’s picture

I have done it before using views templates. Are you familiar with using views templates? If not, there is a link under "Advanced" that is titled, "Theme Information" or something like that. You can click that link and look for style output.

There are a number of different filename suggestions. You can get very specific with the naming. Once you have created the file in your theme's folder, simply click the style output link. Copy the code and then paste it into the empty file in your theme's folder.

If you have questions, please let me know.

bartvdputte’s picture

Thanks for your reply.

Eventually I did it this way, but in my opinion it makes no sense creating different "view templates" for each necessary view that has a table output, only to add a class to the table.

Besides that, it's not consistent with how view formatting works in other formats. As I said in my original post, in the html list format you can easily add classes in the UI. Why not in the table format?

typhonius’s picture

I'm also looking for this. Views has the ability to give a class to a row as well as give a class to an entire view, so why not allow classes to be given to tables?

Taking a look in theme/views-view-table.tpl.php, it looks like the functionality to add a class to a table is there, just not in the Views UI interface.
<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>

jhuon’s picture

Same problem here. Looking in the style output:
$classes: A class or classes to apply to the table, based on settings.
But I can't find this setting in Views UI neither.

danharper’s picture

I'm looking for this also.

moniuch’s picture

I second this request, I totally agree with #3 and while I'm sure everybody found a workaround to do that in template, I think it's leading us to a disease called tpl'itis.

agerson’s picture

Component: Miscellaneous » table style
Category: support » bug

Yes, this implies a setting is available in the GUI when its not.

dawehner’s picture

Category: bug » feature
Anybody’s picture


zmove’s picture

+1 for this.

codesmith’s picture


schifazl’s picture


alex.87’s picture


AntonLargiader’s picture

Actually the class specified in the "Advanced" area does work, but it acts on a DIV rather than on the table, so the CSS needs to read:

.yourclass table {}

rather than

table.yourclass {}

This is view-independent so you can have a favorite table style and select it for any view(s) you want. I was happy to find that I did not have to make a style for each view table or create a style for all view tables, which the other documentation led me to believe.’s picture

Issue summary: View changes


schifazl’s picture

I think that this could be closed, since the solution in #14 works perfectly!

moniuch’s picture

I would vote that we close all requests and resort to workarounds, just because there's no will to address users' feedback.

schifazl’s picture

I thinked for a while about this issue. Adding a class to the table while it was already added to the entire view seems redundant to me, so I don't think that #14 is a workaround, but everyone has his own opinion ;)

zmove’s picture

It can be great to have the control over table class even if you can have a custom class in a parent div because more and more framework like bootstrap require to precisely give classes to your elements.

cybercampbell’s picture

Easiest way is to put the following in your template.php:

function THEME_preprocess_views_view_table(&$vars) {
  $vars['classes_array'][] = 'table';

and if you want striped tables:

function THEME_preprocess_views_view_table(&$vars) {
  $vars['classes_array'][] = 'table table-striped';

Hope that helps.

zmove’s picture

And if you want to have some table stripped and some tables not :)

Definitely it should be configurable in views.

mooru’s picture

The code in #20 is not working even after clearing cache’s picture

Maybe just add a line of jquery to a custom js file?

$( "table" ).addClass( "table table-striped" );

minorOffense’s picture

Status: Active » Needs review
2.6 KB
None View

I've created a patch which adds the option in the UI and the necessary changes to the preprocess function.

minorOffense’s picture

2.64 KB
None View

Last patch introduced a PHP warning. This removes the warning.

alex.designworks’s picture

Status: Needs review » Reviewed & tested by the community

#25 works great

jedsaet’s picture

+1 RTBC to #25

nmillin’s picture

#25 works great

big_smile’s picture

#25 works for me too.

Cameron Tod’s picture

#25 works great here too. +1 :)

milos.kroulik’s picture

#25 works great and would be great addition to Views.

Anybody’s picture

+1 I agree. Any maintainer feedback? Can you include this into the next dev release and stable release afterwards?

mxwright’s picture

Priority: Normal » Major
colan’s picture

We've recently switched our testing from the old to DrupalCI. Because of a bug in the new system, #2623840: Views (D7) patches not being tested, older patches must be re-uploaded. On re-uploading the patch, please set the status to "Needs Review" so that the test bot will add it to its queue.

If all tests pass, change the Status back to "Reviewed & tested by the community". We'll most likely commit the patch immediately without having to go through another round of peer review.

We apologize for the trouble, and appreciate your patience.

Anybody’s picture

Status: Reviewed & tested by the community » Needs review
2.64 KB

Re-uploaded #25 as of #34

Anybody’s picture

Status: Needs review » Reviewed & tested by the community
knalstaaf’s picture

Support for field tokens would top it off though…

Sneakyvv’s picture

Status: Reviewed & tested by the community » Needs review
2.64 KB

#35 works great, thanks!

However... I renamed the patch according to the patch naming convention as it is a real hassle to update contrib modules if the patch names don't start with the module name.

Sneakyvv’s picture

Status: Needs review » Reviewed & tested by the community

back to RTBC as new patch passes tests and only changes a typo (and patch name).

darrenwh’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/theme/
    @@ -466,10 +466,25 @@ function template_preprocess_views_view_table(&$vars) {
    +    if (!empty($class)) {

    Same here why not just use isset($class)

  2. +++ b/theme/
    @@ -466,10 +466,25 @@ function template_preprocess_views_view_table(&$vars) {
    +      // Trim empty class entries

    This comment is missing a period (.) at the end

  3. +++ b/theme/
    @@ -466,10 +466,25 @@ function template_preprocess_views_view_table(&$vars) {
    +      foreach ($class as $key => $c) {

    Could the value be more descriptive what is c$?

minorOffense’s picture

Status: Needs work » Needs review
2.7 KB

I made some of the changes. Fixed the comment. I added an isset but left most as !empty since I actually want to check if something isn't blank or 0. The variable will most always actually be set but I want to prevent blank values from getting into the classes array.

interdruper’s picture

Status: Needs review » Reviewed & tested by the community

#41 works fine.

Note that #20 approach also works fine, if you want to easily add a class to every table from views in your theme.

dreftymac’s picture

For those who are not averse to a javascript solution, here is some sample jQuery code.

<script data-dmid="d7views_cssinject_tableclasses">
jQuery(document).ready(function() {
DamienMcKenna’s picture

What's the rationale to use views_clean_css_identifier() vs drupal_clean_css_identifier()?

minorOffense’s picture

It mirrors how the list classes are sanitized. Well at least when I originally wrote the patch years ago. If that's changed then just use whatever the current function should be.

nironan’s picture

#41 did the trick for me, thanks!