Hi!

I request that tags be placed inside the pager so that the pager output would be more like this.

<ul class="pager">
<li class="pager-current first"><span>1</span></li>
<li class="pager-item"><a href="/products?page=1" title="Go to page 2" class="active"><span>2</span></a></li>
<li class="pager-item"><a href="/products?page=2" title="Go to page 3" class="active"><span>3</span></a></li>
<li class="pager-item"><a href="/products?page=3" title="Go to page 4" class="active"><span>4</span></a></li>
<li class="pager-next"><a href="/products?page=1" title="Go to next page" class="active"><span>next ›</span></a></li>
<li class="pager-last last"><a href="/products?page=3" title="Go to last page" class="active"><span>last »</span></a></li>
</ul>

The main reason is so I can add some CSS like this to hide the numbers...

.pager li span {display:none;}

Then I can style the pager to have boxes, or circles, or images, etc. instead of numbers without having to re-theme the pager. (which I confess I don't know how to do.)

At the moment I'm using jQuery to insert these span tags which works fine. Something like this...

$(document).ready(function(){
  $(".pager-item a").wrapInner("<span>" + "</span>");
  $(".pager-current").wrapInner("<span>" + "</span>");
});

However it doesn't work if AJAX = yes in your View.

Is there another way?

Thanks for reading!
-Tim

Comments

JoeMcGuire’s picture

Preprocess theme functions to change their markup

Views it only using the default pager theme functions. Also it's important the markup stays a generic as possible. There is good reason to add additional containers / span tags if they are needed for a design like multiple CSS background images.

You can achieve this by changing the output from the pager theme functions.

Inside your theme's template.php add the following preprocess function

<?php
function phptemplate_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array()) {
  $page = isset($_GET['page']) ? $_GET['page'] : '';
  if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
    $parameters['page'] = $new_page;
  }

  $query = array();
  if (count($parameters)) {
    $query[] = drupal_query_string_encode($parameters, array());
  }
  $querystring = pager_get_querystring();
  if ($querystring != '') {
    $query[] = $querystring;
  }

  // Set each pager link title
  if (!isset($attributes['title'])) {
    static $titles = NULL;
    if (!isset($titles)) {
      $titles = array(
        t('« first') => t('Go to first page'),
        t('‹ previous') => t('Go to previous page'),
        t('next ›') => t('Go to next page'),
        t('last »') => t('Go to last page'),
      );
    }
    if (isset($titles[$text])) {
      $attributes['title'] = $titles[$text];
    }
    else if (is_numeric($text)) {
      $attributes['title'] = t('Go to page @number', array('@number' => $text));
    }
  }

  return l('<span>' . $text . '</span>', $_GET['q'], array('html' => TRUE, 'attributes' => $attributes, 'query' => count($query) ? implode('&', $query) : NULL));
}
?>

This function is a copy of the core theme_pager_link but with the function name prefix changed from theme_ to phptemplate_ so that is overrides. You will need to clear your cache for this to be picked up.

I've also added the additional span in the link in the last line and set the link option key html to TRUE to allow the span.

jQuery to add contains

I don't recommend using jQuery for this as it's more suitable to the pre-process method mentioned above. However just for the note - your script can continue to work with AJAX. Drupal allows scripts to notified when content is added to the page so they can modify it if they want. To get this working you'll want to change your script to the following,

Drupal.behaviors.theSpanMaker = function (context) {
  $(".pager-item a", context).wrapInner("<span>" + "</span>");
  $(".pager-current", context).wrapInner("<span>" + "</span>");
};

Checkout more about how to get the best out of JavaScript in drupal:
http://drupal.org/node/205296

merlinofchaos’s picture

Category: feature » support
Status: Active » Fixed

Wow, what a great answer!

Since Views doesn't control that markup, changing to a support request and marking fixed. Thanks for helping out!

TimG1’s picture

Wow thanks, I'll give those a shot!

-Tim

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.