Drupal Answers Asked by user3428971 on November 17, 2021
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:
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).
Answered by alyssaeliyah on November 17, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP