Am looking for a solution to implement $form['pager'] = ['#type' => 'pager'];
into AJAX callback (without reloading the page). I have read alot of posts concluding there is no a drupal way to do this at the moment but on visiting this drupal page: https://api.drupal.org/api/drupal/elements it actually implements it. How do i get such results.
My Code
class TableAjaxPager extends FormBase{ $query = \Drupal::database()->select('users_field_data', 'u'); $query->fields('u', ['uid','name','mail']); //For the pagination we need to extend the pagerselectextender and //limit in the query $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10); $results = $pager->execute()->fetchAll(); // Initialize an empty array $output = array(); // Next, loop through the $results array foreach ($results as $result) { if ($result->uid != 0 && $result->uid != 1) { $output[$result->uid] = [ 'userid' => $result->uid, // 'userid' was the key used in the header 'Username' => $result->name, // 'Username' was the key used in the header 'email' => $result->mail, // 'email' was the key used in the header ]; } } $form['table'] = [ '#type' => 'table', '#header' => $header, '#options' => $output, '#empty' => t('No users found'), ]; // Finally add the pager. $form['pager'] = array( '#type' => 'pager' ); return $form; }
buildForm
OR altering an existing form