I'm a beginner with Drupal so this may be something really simple, but I've been struggling with it for a couple of days now.

What I'm trying to accomplish: I'm trying to create a small list of announcements, which will display three items at a time, and will have "previous" and "next" buttons for people to page through them all. It's important that the "previous" and "next" buttons don't cause the page to refresh.

To that end, I have a view that's set up to display the Post Date & Title of recent announcements (3 at a time), filtered by Node:Published, Node:Type, and Taxonomy:Term. I've turned on paging, and set "Use AJAX" to "yes". When I look at the preview of the view, the AJAX paging works like a charm, however when I embed the view in a page, the AJAX doesn't work - in other words, the pager buttons force the browser to reload the page.

This particular view has a "Block" display, which I embed into my page's .tpl.php file using this code: print views_embed_view('announcement_pager', $display_id = 'block_1');

I've also tried displaying the view using the admin/build/block interface. The view shows up fine this way, but the AJAX still doesn't work.

I've tested this in both Safari and Firefox, with and without Javascript enabled, and both while logged in and logged out. It's the same each time.

Like I said, I'm a beginner with this, so I don't really even know what kind of additional info would be helpful for troubleshooting this. If anyone has specific questions about what I'm doing, let me know. Thanks in advance!

Oh, and here's my exported View:


$view = new view;
$view->name = 'announcement_pager';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'created' => array(
    'label' => 'Posted',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'date_format' => 'custom',
    'custom_date_format' => 'D., M. j, Y',
    'exclude' => 0,
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'relationship' => 'none',
  ),
  'title' => array(
    'label' => 'Title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'created' => array(
    'order' => 'DESC',
    'granularity' => 'minute',
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => '1',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'article' => 'article',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
  'tid' => array(
    'operator' => 'or',
    'value' => array(
      '0' => '24',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => 'tid_op',
      'label' => 'Taxonomy: Term',
      'use_operator' => 0,
      'identifier' => 'tid',
      'optional' => 1,
      'single' => 1,
      'remember' => 0,
    ),
    'type' => 'textfield',
    'limit' => TRUE,
    'vid' => '5',
    'id' => 'tid',
    'table' => 'term_node',
    'field' => 'tid',
    'hierarchy' => 0,
    'relationship' => 'none',
    'reduce_duplicates' => 0,
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('use_ajax', TRUE);
$handler->override_option('items_per_page', 3);
$handler->override_option('use_pager', '1');
$handler->override_option('row_options', array(
  'inline' => array(),
  'separator' => '',
  'hide_empty' => 0,
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);

...and here's the query from the preview:

SELECT node.nid AS nid, node.created AS node_created, node.title AS node_title, DATE_FORMAT((FROM_UNIXTIME(node.created) + INTERVAL -25200 SECOND), '%Y%m%d%H%i') AS node_created_minute FROM node node  INNER JOIN term_node term_node ON node.vid = term_node.vid WHERE (node.status <> 0) AND (node.type in ('article')) AND (term_node.tid = 24) ORDER BY node_created_minute DESC

Comments

merlinofchaos’s picture

Status: Active » Fixed

This particular view has a "Block" display, which I embed into my page's .tpl.php file using this code:

The reason this does not work is that while you're in page.tpl.php, the $scripts variable has already been processed. Running the view adds Views' ajax files and settings into the $scripts, but because it has not been processed, it never appears.

Instead, the best way to do this is to have your theme .info file declare a region for use in this page.tpl.php and put your view into this special region through the block UI. Then the view will render prior to page.tpl.php allowing the $scripts variable to be populated correctly.

info_dump’s picture

Hi Merlin, thanks for your reply. I should have elaborated a bit: I had done what you suggested and it still didn't work. I created a region in the .info file called "sidebar_right_articles", and in the Blocks UI I assigned the view block to that region. Then in my page.tpl.php (actually it's "page--articles.tpl.php", if that makes a difference) I used this code:

<?php if ($sidebar_right_articles) { ?>
			<div id="article_list">
 				 <?php print $sidebar_right_articles ?>
			</div>
<?php } ?>

As I said in the first message, the view block shows up on the page using this method, but the AJAX paging still doesn't work. I've tried clearing my Drupal cache, and my browser cache as well, just in case. I've also tried this code without the div tags, and as a single php code, rather than a broken-up one. They all basically do the same thing: they produce a block that refreshes the window when paging.

Your explanation made a lot of sense, so now I'm confused as to why this method isn't working. Again, I'm trying to be complete in my description but if there are questions about anything I'm doing I'd be happy to answer them.

Thanks again for your help.

info_dump’s picture

Status: Fixed » Active
dawehner’s picture

  • Some questions: Is the ajax_view.js added to your page?
  • Does firebug display js errors?
info_dump’s picture

Update: I've been mainly testing this in Safari (4.0.3), which may not be the best approach. I've just tested the site in Firefox for Mac, and Windows (running in Parallels), as well as IE8 in Parallels. In all three of those browsers the AJAX paging works! Just not Safari. So this appears to be some kind of cross-browser issue.

I guess I'll have to go through the issues archive again to see if anyone else has reported AJAX paging problems with Safari.

@dereine: In answer to your questions, yes ajax_view.js is added, and I'm not getting any javascript errors.

info_dump’s picture

Status: Active » Closed (fixed)

Update #2: I've tested it in Safari again, and discovered that AJAX paging doesn't work ONLY when I'm logged in. From what I've read in the forum, this is a known issue and probably doesn't require any further discussion here - unless there's an obvious fix. Thanks for the help guys.