0

I have the callback function below that renders a table with a pager. Every page of the table is 20 rows. Below is the code:

function myid_print_all_submitted_id_callback($form, &$form_state){ 
    $table_html = '';             
    $rows = array();

    // Build the sortable table header.
    $header = array(
        'idnumber' => array('data' => t('ID Number'), 'field' => 'idnumber'), 
        'student' => array('data' => t('Student'), 'field' => 'student'), 
        'status' => array('data' => t('Lacking ID Information | Status'), 'field' => 'status'),
    );      

    $query = db_select('student', 't1',array('target' => 'import'));
    $result = $query
        ->fields('t1', array('idnumber','student','status'))                     
        ->condition('t1.studid', 1 ,'=')                          
        ->extend('TableSort')->extend('PagerDefault')->limit(20)      
        ->execute();  

    foreach($result as $record){
        $rows[] = array(
            'idnumber' => $record->idnumber, 
            'student' => $record->student,            
            'status' => $record->status, 
        );
    }

    $table_html = theme('table', array('header' => $header, 'rows' => $rows)) . theme("pager"); 

    //Build the table  
    $form['table'] = array(
        '#type' => 'item',
        '#markup' => $table_html,
        '#empty' => t('Table has no row!'),
        '#prefix' => '<div id="myid_table_wrapper_div">',
        '#suffix' => '</div>', 
    ); 
    return $form['table'];

}

It looks like the image below:

enter image description here

When I click the next page it shows a blank page:

enter image description here

Where did I go wrong? It was redirected to http://19.46.1.13/myid/system/ajax?page=1, when it must be redirected to http://19.46.1.13/myid/user/1/myid_print?page=2.

I was guessing it is because the table pager was not loaded yet because it was rendered by a callback function. Any suggestions/solutions on how to solve it?

0

This sounds like a known bug in core:

Pager, tablesort links in a form point to system/ajax when reloaded via AJAX https://www.drupal.org/node/1181370

Someone has offered up a "quick fix" in comment #22:

As a quick fix I overrode theme_pager_link() to set the url of my AJAX-loaded pager to the page url instead of $_GET['q'] (which was system/ajax).

[Edit...]

I haven't tested this, but give it a try:

  1. In your callback function, save the current path in a session variable:
$_SESSION['page_path'] = current_path();
  1. Copy and paste this function into your *.module file:
/** 
 * Implements hook_theme_registry_alter().
 */ 
function YOUR_MODULE_NAME_theme_registry_alter(&$theme_registry) { 
  $theme_registry['pager_link']['theme path'] = drupal_get_path('module', 'YOUR_MODULE_NAME'); 
  $theme_registry['pager_link']['function'] = 'YOUR_MODULE_NAME_pager_link'; 
}
  1. Copy the theme_pager_link() function into your *.module file.

  2. Rename the function YOUR_MODULE_NAME_pager_link.

  3. In the function, replace the following line:

$attributes['href'] = url($_GET['q'], array('query' => $query));

with:

if (isset($_SESSION['page_path'])) {
  $attributes['href'] = url($_SESSION['page_path'], array('query' => $query));
} 
else {
  $attributes['href'] = url($_GET['q'], array('query' => $query));
}
  1. Clear the caches.
  • Where will I override theme_pager_link()? – user46175 Aug 7 '15 at 1:23
  • Please guide me on how to do this. – user46175 Aug 7 '15 at 1:43
  • I've added some steps in my answer. I haven't tested it, but it should work. – othermachines Aug 7 '15 at 22:13
  • not working with me....Why? – user46175 Sep 15 '15 at 5:50
  • current_path() returns system/ajax? why? – user46175 Sep 15 '15 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy