Download & Extend

Views Pager Not Working Within AHAH Form

Project:AHAH helper
Version:6.x-2.1
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

I've searched high and low and can't find anything in google or in the ahah_helper or views issue queues about this. The closest thing I found was this "type" of issue for the finder module that is using its own ahah code: http://drupal.org/node/592038

I have an ahah form and am using ahah_helper. When the form is submitted, views results are shown and sometimes contain a pager. Unfortunately, the pager does not work with or without using ajax (setting in views) for the pager.

I've played around with it, but don't know enough about how the two are interacting to know to fix it. The pager items for the view when it's *not* in the ahah form look like:

http://[domain]/home?page=3 (no ajax)
or
http://[domain]/home?page=2&view_name=recent_content&view_display_id=block_1&view_args=&view_path=node%2F12&view_base_path=null&view_dom_id=2&pager_element=0 (with ajax once pager has been clicked once)

but once the view is shown in the ahah form, the pager links look like:

http://[domain]/ahah_helper/list_view?page=2&&list_view[deck]=59&list_view[filter]=recent&form_build_id=form-4c1a74a28a7e84e9460be2113dfccb70&form_token=b0aae8298de1b0663d242a69380d18d7&form_id=custom_get_list_view_form&op=Go%21

once the form has been submitted at least once.

So, the problem is that the pager links have been changed from the "real" $_GET['q'] info to the ahah_helper/* path since the form was updated with ahah so that's what it thinks is the current $_GET['q'].

Do you have some idea of how to work around this?

Thanks,
Kristen

Comments

#1

Version:6.x-2.0» 6.x-2.1

I'm having the same problem but am not using Views. I'm putting some themed search results (using Drupals Search module API) at the end of a custom search form. The themed search results include pager controls courtesy of the theme_pager function. When clicking on one of the pager links an error occurs: warning: array_shift() The argument should be an array in .../ahah_helper.module on line 132.

#2

What I ended up doing is adding some custom jquery to "hijack" the pager links and "submit" the form if the user clicks on the pager link rather than have it do the normal new page. I had to add some additional code to keep track of the page=[n] and set that in the $_GET['page'] variable so the query ends up doing the correct thing.

Note that I use the same AHAH form on multiple pages and had to keep track of the pager number based on the page they are on, so the code is a bit more complicated than it would be if the form is only used on one page. When you see "current page" data, that is what it is for.

Here's my jquery:

Drupal.behaviors.listView = function(context) {

  $('ul.pager li a[href]').bind('click', {}, function() {
        // grab the pager number from the href
        var matches = this.href.match(/.*?page=(\d*).*?/);
        var pager_number = 0;
        if (matches != null && matches[1]) {
          pager_number = matches[1];
        }
        // set pager number in the php session
        $.get(get_list_view_ajax_page()+'&pager_num='+pager_number,
          null,
          function () {
            // click the hidden submit on the form
            $('#edit-list-view-hidden-submit').click();
          });
        return false;
      }
      );
}

function get_list_view_ajax_page() {
  var current_page = location.pathname;
  current_page = escape(current_page.substring(1)); // get rid of leading slash
  ajax_page = 'http://'+location.host+'/list-view-widget-session-data?page='+current_page;
  return ajax_page;
}

The hidden submit button in my form looks like:

  $form['list_view']['hidden-submit'] = array(
      '#type' => 'submit',
      '#value' => 'hidden',
      '#ahah' => array(
        'event' => 'click',
        'path' => ahah_helper_path(array('list_view')),
        'wrapper' => 'list-view-wrapper',
        'method' => 'replace',
       ),
      '#prefix' => '<div class="hidden-submit">',
      '#suffix' => '</div>',
      );

Here's my code to set the pager stuff in the session:

1) Here's the menu item that is called in the jquery

  $items['list-view-widget-session-data'] = array(
      'page callback' => 'xyz_list_view_widget_data_ajax',
      'type' => MENU_CALLBACK,
      'access arguments' => array('access content'),
      );

2) Here's the function that is used

function xyz_list_view_widget_data_ajax() {
  $data_ftn = 'xyz_get_list_view_widget_data';
  $session_key = 'list_view_widget_data';
  $page = urldecode($_GET['page']);
  $data = array();
  if (function_exists($data_ftn)) {
    $data = $data_ftn($page);
  }
  if (isset($_GET['pager_num'])) {
    // need to update the pager number
    $pager_num = $_GET['pager_num'];
    $data[$page]['pager_num'] = $pager_num;
    $_SESSION[$session_key] = $data;
  }
  echo drupal_to_js(array(
        'widget_data'=>$data[$page],
        )
      );
  exit;
}

Here's the function called to get the pager number from the session:

function xyz_get_list_view_widget_data($page = null) {
  $key = 'list_view_widget_data';
  $page_session_data = (isset($_SESSION[$key]) ? $_SESSION[$key] : array());
  if (! is_array($page_session_data)) {
    $page_session_data = array();
  }
  if ($page && ! is_array($page_session_data[$page])) {
    $page_session_data[$page] = array();
  }
  return $page_session_data;
}

Here's the code to set the pager number for the form based on the session data:

  $current_page = $_GET['q'];
  $list_view_widget_data = xyz_get_list_view_widget_data($current_page);
  $pager_num = $list_view_widget_data[$current_page]['pager_num'];
  if ($pager_num) {
    $_GET['page'] = $pager_num; // set this so that pager_query code knows the page
  }

#3

I seem to be having the same issue here. I can get the pager_query() and even sortable headers to display correctly on the first submit. however, when i click on a header to sort or when i click on the page number in pager, the form seems to simply be rebuilt but not submitted. I have to resubmit the form to get the values back. I can't seem to get the above code to work for me though (of course hacking it apart to fit my needs). Anyone else having these issues?

nobody click here