Pagination not working correctly in AJAX view with exposed filters

Created on 24 November 2022, almost 2 years ago
Updated 28 August 2024, 3 months ago

Problem/Motivation

When I use Views module and enable pager and use exposed filters, they will not work in case you go to the view page with ?page=[number] query parameter.

Steps to reproduce

How to recreate it:

1. Add Article content type or use another one.
2. Create several Article nodes. In my case I created 50 nodes. One of the article should have unique name, for example Test123.
3. Create View, that shows all Article content, add page with /test path, enable Pager (full or mini) and show 10 items per page, enable Ajax, add Exposed filter for Title field and use Contains operator.
4. Go to the /test?page=4 page, in my case it shows the latest page, because I have 50 items and 10 items per page.
5. Try to use Title filtering and type existed article name, in my case Test123. Then you will see empty page.

It happens because Views Ajax uses query param and grab page from it and sends it to the server. So it filters all articles by Test123 title and shows result of the 5th page, and since we have only one result, the 5th page is empty.

Proposed resolution

Modify core/modules/views/js/ajax_view.js:82 to replace the page number parameter.

Before modify:

        queryString = queryString
        .slice(1)
        .replace(/q=[^&]+&?|&?render=[^&]+/, '');
      if (queryString !== '') {

After modify:

        queryString = queryString
        .slice(1)
        .replace(/q=[^&]+&?|page=[^&]+&?|&?render=[^&]+/, '');
      if (queryString !== '') {
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Views 

Last updated about 5 hours ago

Created by

🇺🇦Ukraine lobodacyril

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇺🇦Ukraine lobodacyril

    I'm not sure that's possible to solve without changing JS code since each AJAX request is sent using URL. Look at the JS code, it takes window.location.search and sends all the URL query parameters via AJAX. It means if the query contains page parameter, it will be sent also. That's why we need to remove sending page param.

  • 🇺🇦Ukraine lobodacyril

    The patch stopped working so I changed and it works on 9.5.3

  • 🇺🇦Ukraine lobodacyril

    Sorry wrong patch

  • 🇳🇱Netherlands Lendude Amsterdam

    Well, at least it doesn't break any existing test, that's positive.

    We do need test coverage for this bug though, so setting back to needs work for that.

  • 🇺🇦Ukraine lobodacyril

    This is a patch for 10.1.x version

  • last update about 1 year ago
    29,672 pass
  • 🇧🇪Belgium p-neyens

    This is a patch for 10.2.x version.

  • First commit to issue fork.
  • I'm not sure this or the original behaviour works as expected when the query parameter is not the first entry, or the first query ends with a q or render.

    I've provided some example test cases to showcase the behaviour here: https://playcode.io/1830989

    const queryStrings = [
      'q=/path',
      'faq=should_not_be_affected',
      'myrender=should_also_not_be_affected',
      'q=/path&a=1&q=/second-path',
      'q=/path&render=value',
      'q=/path1&q=/path1&faq=true&dont_render=1&q=/path&render=render_value&page=1&final_value=1',
    ];
    for (const queryString of queryStrings) {
      const output = [];
      output.push(queryString.replace(/q=[^&]+&?|&?render=[^&]+/, ''));
      output.push(queryString.replace(/q=[^&]+&?|page=[^&]+&?|&?render=[^&]+/, ''));
      output.push(queryString.replace(/(^|&)(q|render|page)=[^&]+/g, '').replace(/^&/, ''))
      console.log(output);
    }
    

    This might be potentially supeceded by 🐛 Query string is appended multiple time after each AJAX request Needs work which now attempts to use a URLSearchParams object to remove those same values. But we may also look into using the updated regex provided instead, and leave that as a follow-up.

    I also believe the render query parameter might be legacy code and no longer relevant in D9+, so can probably be removed since I can't find any references to it in core.

  • Merge request !7485Resolve #3323574 "Page index isnt" → (Open) created by phthlaap
  • Pipeline finished with Success
    7 months ago
    Total: 720s
    #146214
  • Status changed to Needs review 7 months ago
  • 🇻🇳Vietnam phthlaap

    I created a merge request and added tests.

  • 🇻🇳Vietnam phthlaap

    I updated the issue summary to the correct format.

  • First commit to issue fork.
  • Pipeline finished with Success
    7 months ago
    Total: 2216s
    #147187
  • Status changed to RTBC 7 months ago
  • 🇺🇸United States smustgrave

    Appears tests have been added

    Added a nitpicky missing void return.

    Verified the issue following the steps and that the MR addresses the issue.

    Hiding unused MR.

  • 🇮🇳India Pravesh_Poonia

    @smustgrave, Applied mr changes are looking good to me, @
    lobodakyrylon you can check it is good to go

  • Status changed to Needs work 7 months ago
  • 🇳🇿New Zealand quietone

    Sorry folks, but this does need a title that better describes what is being fixed and be in proper English.

  • I think the points raised in #15 regarding the regex only working if the paramter is the first entry are still relevant.

  • Status changed to Needs review 7 months ago
  • 🇺🇸United States smustgrave

    @codebymikey do you want to open an MR to try your idea of removing.

  • Status changed to Needs work 7 months ago
  • 🇺🇸United States smustgrave

    Think maybe we should have test coverage for the points in #15.

  • First commit to issue fork.
  • 🇬🇧United Kingdom the_g_bomb

    As mentioned in the MR.
    I have opened an MR using the latest changes introduced by smustgrave in #21, but with the alternate regex supplied by codebymikey in #15.

    I have yet not added the test coverage as requested in #27.

  • Pipeline finished with Failed
    3 months ago
    Total: 2360s
    #267021
  • First commit to issue fork.
  • 🇮🇳India vinmayiswamy

    Hi, I updated the test to cover scenarios with multiple query parameters in the request, specifically adding extra parameters like foo and q alongside the page parameter. This change ensures that pagination resets correctly when there are additional parameters in the URL, which is common in real-world cases.

    Hi, I updated the test to simulate scenarios where multiple query parameters are involved in the request. Specifically, I added test cases that include extra parameters like foo and q alongside the page parameter. This was necessary because, in real-world scenarios, URLs often contain additional parameters, and I wanted to ensure that the pagination reset behaves correctly even with these variations.

    I started by filtering content by title and confirming the correct result. Then, I tested the page with both foo and page parameters, applying the filter again to check that the additional parameters didn’t affect the pagination. Lastly, I included a case where both q and page parameters appear in different positions, ensuring the pagination resets as expected.

    These updates help cover edge cases with query parameters, preventing issues with pagination and ensuring that the filtered content is returned correctly.

    Additionally, I verified the fix in Drupal 11.x fresh install and attached before and after screenshots for reference.

    Kindly review the changes and please let me know if any further adjustments are needed. Your feedback and suggestions for improvements are greatly appreciated.

    Thanks!

  • 🇺🇸United States smustgrave

    Before moving to review please make sure pipeline is passing and tags addressed.

  • Need to reconfirm whether this is still an issue in >= 10.3x since the merging of 🐛 Media Library widget display doesn't return to first page on applying filters Needs work

Production build 0.71.5 2024