7

I want to add a css class to a table which is created using views. I tried to override "views-view-table.php" in my custom theme but that doesn't seems to work. In "views-view-table.php" I replaced the line "<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>" with "<table class="responsive">" and then cleared the cache, yet it didn't work. Can any one help. Thanks in advance.

3

This is an old post, but I'd like to chime in with a couple of solutions.

First, and this may not answer your question directly, but you can change the CSS selectors you are using to target the table.

So, rather than using table.yourclass you could use .yourclass table. This can be accomplished by setting a custom class for the entire view under the advanced section.

Of course, this won't work if you are trying to add classes directly to the element as you might need to do with Bootstrap, i.e. table.table.table-striped.

The second solution will allow you to add the same classes to all your tables. In your template.php file, add the following:

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

If you need to target a specific table, you can also check the $vars variable first to find the name of the view and display being called and only apply your classes to the ones you want to target. I haven't checked, but it's probably found in something like $vars['view']->current_display.

Finally, it's worth noting that Views does actually support custom classes on the table. The variables are called in the template files, there just isn't an option for it in the views GUI. This is an oversight in my opinion, but the module's devs don't seem to plan on fixing it. That said, a patch for it does exist (direct link).

2

You should not need a template file for this. Under the Advanced > Other tab in your view there is a CSS class option. You can add it there and then use the appropriate selectors to your CSS. You can also use hook_preprocess_views_view() to add the class to the $classes variable used in the template.

  • This will add classes to the view itself not the table generated by the view as the OP is asking – undersound Mar 1 at 9:08
0

The file you try to override should be "views-view-table.tpl.php". You forgot the ".tpl". To view all the available files that you can override, use the option "Advanced -> Theme:Information" from the view config page.

0

You are theming the view incorrectly. Follow the below steps for view theming :

  1. Go to Advanced tab in your view.
  2. Click Theme:Information.
  3. Create the file in your theme template directory with the name eg. "views-view-table--abc.tpl.php" if your view name is 'abc' i.e. Replace 'abc' with desired view name.
  4. Click on "Rescan Template File" button and see if name of file you created become bold or not.If yes then your view has been themed, if not then file name might not be correct. For more details https://drupal.org/node/352970
  • Sorry for that it was just a typo error. I am overriding "views-view-table.tpl.php". Its name is already in bold even before I click "Rescan Template File". I just copied this file(views-view-table.tpl.php) from "modules/views/theme/views-view-table.tpl.php" into "mytheme/templates/views-view-table.tpl.php" and made the changes as specified in the question. But, it didn't help. Is there any thing I am missing? – maaz siddiqui Mar 23 '14 at 8:28
  • I am able to override/create .tpl.php files for specific tables such as "views-view-table--abc.tpl.php" and its working great. But I cant to override "views-view-table.tpl.php" using the same process. I don't what's going wrong. – maaz siddiqui Mar 23 '14 at 9:00

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.