blog-banner

Drupal : Disable Sticky Header Behavior of Theme_Table()

    Drupal 7 Sticky Header

     

    Lately, I have been working on a potential Drupal 6 project that needs to display information in a formatted table structure. Needless to say, I have been using a lot of pager_query() and theme_table(). Yes, I do understand the benefits  hook_views_data() but this is a different case.

    One thing I found to be annoying about theme_table() is it's the default behavior of adding a sticky table header and not having an argument that can be passed to disable it when needed as introduced in Drupal 7 http://drupal.org/node/224333#sticky_headers

    The snippet that sets the sticky header is as below,


    function theme_table($header, $rows, $attributes = array(), $caption = NULL) {
    // Add sticky headers, if applicable.
    if (count($header)) {
    drupal_add_js('misc/tableheader.js');
    // Add 'sticky-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
    $attributes['class'] = empty($attributes['class']) ? 'sticky-enabled' : ($attributes['class'] . ' sticky-enabled');
    }
    }

    ?>

    I did try the old phptemplate_preprocess_page() the trick that appears first in Google search results. For some reason, it didn't work for me, besides I'm not happy adding too much code into the expensive function.

    Preprocess function trick code snippet from Drupal forum,


    function phptemplate_preprocess_page(&$vars) {
    $vars['tabs2'] = menu_secondary_local_tasks();
    // Hook into color.module
    if (module_exists('color')) {
    _color_page_alter($vars);
    }

    // remove sticky table headers
    $js = drupal_add_js();
    unset(
    $js['module']['misc/tableheader.js']);
    $vars['scripts'] = drupal_get_js('header', $js);
    }

    ?>

    Alternatively, I tried CSS display: none trick which produced the same output as I expected with the least code. All it needed is to add this one-line style to the custom CSS file.

    table.sticky-header { display: none !important; }

    Bingo !!