Table Pager Rendered in an Ajax Callback Shows White Page 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, &amp;$form_state){ 
    $table_html = '';             
    $rows = array();

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

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

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

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

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


}

It looks like the image below:



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



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?
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:


In your callback function, save the current path in a session variable:


$_SESSION['page_path'] = current_path();


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'; 
}



Copy the theme_pager_link() function into your *.module file.
Rename the function YOUR_MODULE_NAME_pager_link.
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' =&gt; $query));
} 
else {
  $attributes['href'] = url($_GET['q'], array('query' =&gt; $query));
}



Clear the caches.