Fixed pagination and sorting of tables on pages with ajax

Issue fork drupal-2925598

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

shtelya created an issue. See original summary.

shtelya’s picture

StatusFileSize
new1.63 KB
shtelya’s picture

StatusFileSize
new1.67 KB
shtelya’s picture

StatusFileSize
new2.83 KB
shtelya’s picture

StatusFileSize
new2.79 KB
fabianx’s picture

Category: Feature request » Bug report
Priority: Normal » Major
Related issues: +#2503429: [PP-*] Allow both AJAX and non-AJAX forms to POST to dedicated URLs
vimalabhi89’s picture

I'm having the same trouble. I'm returning a tableselect with pager using an ajax callback and the pager urls inlcude
"ajax_form=1&_wrapper_format=drupal_ajax"
Applied this patch which removed '_wrapper_format' from the url of the pagination links, but still inlcuding the ajax_form piece which throws "BrokenPostRequestException"

https://drupal.stackexchange.com/questions/263557/is-there-a-way-to-add-...

Is there any work around for this? I need to filter the data (1000's of records) to return a table with pager within the same page allowing the users to sort the table or paginate

kalistos’s picture

StatusFileSize
new925 bytes

Fix ajax pager on Drupal 8.5

andypost’s picture

Status: Active » Needs review
rlmumford’s picture

StatusFileSize
new954 bytes

Updated so patch applied on 8.7.x

andypost’s picture

Issue tags: +Needs tests
joegl’s picture

Patch seems to work for pagers, but no code for table sorting and the table sort header links are still broken by AJAX. Maybe I am misinterpreting this issue?

Patch applies cleanly to 8.7 -- updating issue to match.

joegl’s picture

I am also not sure if the patch is the correct way of writing the query for the pager links, or if it's only necessary because the links are automatically being written incorrectly due to #2504709: Prevent _wrapper_format and ajax_form parameters from bleeding through to generated URLs

However, I do know that to get our pagers to work we've been adding the following to the render array for the pager, to manually strip out the _wrapper_format query parameter:

      'pager' => array(
        '#type' => 'pager',
        '#quantity' => 5,
        '#parameters' => ['q'=>'', '_wrapper_format'=>''],
      ),

The patch in this issues fixes the issue so we don't have to manually strip out these paramaters.

joegl’s picture

For tablesorting core/includes/tablesort.inc line 63 is:

    $cell_content = \Drupal::l(new FormattableMarkup('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), new Url('<current>', [], [
      'attributes' => ['title' => $title],
      'query' => array_merge($ts['query'], [
        'sort' => $ts['sort'],
        'order' => $cell_content,
      ]),
    ]));

This seems to run into the same issue as #2504115: AJAX forms should submit to $form['#action'] instead of <current> where the URL is always assumed to be the "current" route. However, when I am rebuilding the table in an AJAX request which say, performs an action on data in a row, the tablesort URL is then written using the URL/route for the AJAX request ("current") instead of the actual page.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andrewbelcher’s picture

To fix table sort, we need to do the same thing in \Drupal\Core\Utility\TableSort::getQueryParameters:

/**
* Composes a URL query parameter array for table sorting links.
*
* @param \Symfony\Component\HttpFoundation\Request|null $request
* A current request.
*
* @return array
* A URL query parameter array that consists of all components of the
* current page request except for those pertaining to table sorting.
*
* @internal
*/
public static function getQueryParameters(Request $request) {
return UrlHelper::filterQueryParameters($request->query->all(), ['sort', 'order']);
}
?>

I suppose the alternative and more generic solution would be to always filter the AJAX query parameters as part of \Drupal\Component\Utility\UrlHelper::filterQueryParameters, then anything that performs query parameter filters will have them removed. Or we could do it as part of URL generation, always removing those parameters if they are set. Not sure what the consequences elsewhere of either of those would be - there may be some places that would want to keep them perhaps?

kpv’s picture

StatusFileSize
new973 bytes

Changed patch from #10 to apply to Drupal 8.8.x version.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

marceldeb’s picture

I've tested path #17. It makes a small difference as i stay on the current page (before the patch i navigated to a blank page with a textfield containing the response).

However, it's not working as expected yet.

In my scenario: I use a Drupal.ajax() request to open a view-page in another page. Pager buttons not working in this scenario.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
hmdnawaz’s picture

I am displaying some results from an API on my Drupal page. I used the Drupal core pagination to paginate the results.

When the page loads, the pagination works fine. But when the results are loaded through ajax, the pagination did not work.

I applied patch #17 and it solves my issue. Now my pagination also work when the results are loaded through ajax.

My Drupal version is 9.3.6

kevin.brocatus’s picture

StatusFileSize
new1.7 KB

Like #16 suggested, I added the same fix for \Drupal\Core\Utility\TableSort::getQueryParameters.

kevin.brocatus’s picture

StatusFileSize
new1.98 KB

Fixed an issue in the previous patch.

The last submitted patch, 23: drupal-ajax_pagination-2925598-23.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 24: drupal-ajax_pagination-2925598-24.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

casey’s picture

StatusFileSize
new1.03 KB

#24 does not apply to D10

casey’s picture

StatusFileSize
new2.1 KB
nileema.jadhav’s picture

sarathkm’s picture

Version: 9.5.x-dev » 10.1.x-dev

Updating the version to 10.1 dev

nileema.jadhav’s picture

StatusFileSize
new3.21 KB

Adding/updating patch for 10.0.x

djsagar’s picture

Version: 10.1.x-dev » 11.x-dev

Updating the version to 11.x-dev

nileema.jadhav’s picture

StatusFileSize
new3.34 KB
djsagar’s picture

StatusFileSize
new119.75 KB

Hi @nileema.jadhav,

not able to apply the patch #35 in drupal 11.

for reference
issue

Thanks!

saidatom’s picture

StatusFileSize
new3.36 KB
casey’s picture

s_leu made their first commit to this issue’s fork.

s_leu’s picture

Re #38 : I don't think that issue solved the problem with the pager here and it's neither addressing the issue of sorting which is addressed here.

s_leu’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

MR appears to have a failure

johnv’s picture

Title: Fixed pagination and sorting of tables on pages with ajax » Pager pagination and table sorting not updated after user change, ajax enabled

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.