Pager not working when $form[‘table’] is called with AJAX

For a statistics page I let users select the right file in a select list. After that I generate a table with stats. This table also has a pager (that worked without the ajax part).

Now I did everything in an ajax callback, when I now want to click on next page the URL is: http://localhost/drupal2/system/ajax?page=1 and that doesn’t work ofcourse.

My code:

function push_notifications_theme() { return array( 'push_notifications_form_table' => array(   'render element' => 'form', ), ); }  function push_notifications_form_table_form($form = array(), &$form_state) { $form = array(); // $id = $_GET['id'];  $qry = db_select('push_notifications_api', 'pt'); $qry->distinct(); $qry->fields('pt', array('app_name')); $qry->orderBy('app_name', 'ASC'); $names = $qry->execute(); foreach($names as $record) { $options[$record->app_name] = $record->app_name; }  $form['title'] = array( '#type' => 'fieldset', '#title' => t('Application statistics'), '#description' => t('Select an application below to see the statistics.'), '#tree' => TRUE, );  $form['title']['changethis'] = array( '#title' => t('Application Name'), '#type' => 'select', '#options' => $options, '#required' => TRUE, '#ajax' => array(   'callback' => 'push_notifications_form_table_form_callback',   'wrapper' => 'replace_textfield_div', ), );  $form['replace_textfield'] = array( '#prefix' => '<div id="replace_textfield_div">', '#suffix' => '</div>', );  if(!empty($form_state['values']['title']['changethis'])) {  $appname = $form_state['values']['title']['changethis']; $id = db_query('SELECT app_id FROM push_notifications_api WHERE app_name = :app_name', array(':app_name' => $appname))->fetchField();  $form['replace_textfield']['id'] = array( '#type' => 'fieldset', '#title' => t('@appname: Push notifications', array('@appname' => $appname)), '#description' => t('Newest push notifications are on top.'), '#weight' => 10, );    $go_back_link = array( '!link' => l(t('go back to the overview'), 'http://localhost/drupal2/admin/config/services/push_notifications'), );  $form['replace_textfield']['goback'] = array( '#type' => 'fieldset', '#title' => t('Go back'), '#description' => t('If you want to view stats from other application you can !link', $go_back_link), '#weight' => 25, );  // $query = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id ORDER BY msg_timestamp DESC', array(':app_id' => $id)); // $qCount = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id', array(':app_id' => $id))->rowCount();  $query = db_select("push_notifications_messages", "n"); $query->fields("n", array('msg_id', 'msg_message', 'msg_receiver', 'msg_device', 'msg_appID', 'msg_timestamp')); $query->orderBy('msg_timestamp', 'DESC'); $query->condition('msg_appID', $id); $query = $query->extend('TableSort')->extend('PagerDefault')->limit(15); $result1 = $query->execute();   $form['replace_textfield']['table'] = array(     '#theme' => 'table',     '#header' => array(t('Message'), t('Device'), t('Token'), t('Date')),     'rows' => array(),     '#weight' => 15,     );  foreach($result1 as $key => $result) {     $form['replace_textfield']['table']["#rows"]["'r$key'"] = array(         'c1' => array(             'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_message))),           ),         'c2' => array(             'data' => array('#type' => 'item', '#markup' => t('@device', array('@device' => $result->msg_device))),           ),         'c3' => array(             'data' => array('#type' => 'item', '#markup' => t('@token', array('@token' => substr($result->msg_receiver, -50)))),           ),         'c4' => array(             'data' => array('#type' => 'item', '#markup' => t('@timestamp', array('@timestamp' => date("d F Y H:i:s", $result->msg_timestamp)))),           ),             ); }   $form['replace_textfield']['pager'] = array('#markup' => theme('pager'), '#weight' => 20);  }    return $form; }  function push_notifications_form_table_form_callback($form, $form_state) { // The form has already been submitted and updated. We can return the replaced // item as it is. return $form['replace_textfield']; }  function theme_push_notifications_form_table(&$variables) { // Get the userful values. $form = $variables['form']; $rows = $form['rows']; $header = $form['#header'];  // Setup the structure to be rendered and returned. $content = array( '#theme' => 'table', '#header' => $header, '#rows' => array(), );  // Traverse each row.  @see element_chidren(). foreach (element_children($rows) as $row_index) { $row = array(); // Traverse each column in the row.  @see element_children(). foreach (element_children($rows[$row_index]) as $col_index) {   // Render the column form element.   $row[] = drupal_render($rows[$row_index][$col_index]); } // Add the row to the table. $content['#rows'][] = $row; }  // Redner the table and return. return drupal_render($content); } 

Screenshot of result: screenshot

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Pager not working when $form[‘table’] is called with AJAX

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.