I have been able to find information on adding classes to tables and other items in my pages, however, I cannot find any information on wrapping the entire table for example in a div through the preprocess function.

I added this for example:

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

My tables are stripped which is what I was after, however, I would like to wrap the entire table itself in <div class="table-responsive">{TABLE}</div>, but am not finding where / how to add the class to show before the table?

Adding a picture of where it could be added:table layout in code


1 Answer 1


you can override views-view-table.tpl.php template to achieve this.

Like :

<div class="table-responsive">
<table <?php if ($classes) { print 'class="'. $classes . '" '; } ?><?php print $attributes; ?>>
   <?php if (!empty($title) || !empty($caption)) : ?>
     <caption><?php print $caption . $title; ?></caption>
  <?php endif; ?>
  <?php if (!empty($header)) : ?>
        <?php foreach ($header as $field => $label): ?>
          <th <?php if ($header_classes[$field]) { print 'class="'. $header_classes[$field] . '" '; } ?>>
            <?php print $label; ?>
        <?php endforeach; ?>
  <?php endif; ?>
    <?php foreach ($rows as $row_count => $row): ?>
      <tr <?php if ($row_classes[$row_count]) { print 'class="' . implode(' ', $row_classes[$row_count]) .'"';  } ?>>
        <?php foreach ($row as $field => $content): ?>
          <td <?php if ($field_classes[$field][$row_count]) { print 'class="'. $field_classes[$field][$row_count] . '" '; } ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
            <?php print $content; ?>
        <?php endforeach; ?>
    <?php endforeach; ?>

Get a look for override views template here

For other tables Override theme_table and change its return statement Like:

return '<div class="table-responsive">'.$output.'</div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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