3

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;
}
4
  • Are you building a form through buildForm OR altering an existing form Commented Apr 11, 2018 at 12:42
  • @ Razeem Ahmad yes Commented Apr 11, 2018 at 14:16
  • @Razeem Ahmad have updated my code. Commented Apr 11, 2018 at 14:23
  • I'm having the same issue but still no solution, just happy I'm not the only one. You're revering to '/api/drupal/elements' but it seams that the page is using /views/ajax so it's a views page. Maybe we're able to use the views ajax call or the views pager. Going to try it out.
    – h3rj4n
    Commented Apr 12, 2018 at 10:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.