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 !== '') {

Issue fork drupal-3323574

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

lobodacyril created an issue. See original summary.

lobodakyrylo’s picture

I suppose we need to exclude page query param from URL string from AJAX request.

lobodakyrylo’s picture

Status: Active » Needs review
mitthukumawat’s picture

StatusFileSize
new57.07 KB
new73.76 KB

I have applied the patch #2 successfully and the issue is resolved. The views ajax is now filtering the specific content from entire view results instead from only specific page.
Adding screenshots for reference.
RTBC +1

lendude’s picture

Issue tags: +Needs tests

@lobodacyril thanks for reporting this.

Yeah this does lead to weird results, but then people having that URL is a bit edge casey I think.

If you use Ajax to go to the last page, the page gets reset correctly, so there should already be logic for this somewhere, we should probably expand it to cover page numbers coming from the URL? Not sure hacking this into the Javascript is the best place to deal with this.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new173 bytes

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.

lobodakyrylo’s picture

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.

lobodakyrylo’s picture

StatusFileSize
new1.34 KB

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

lobodakyrylo’s picture

StatusFileSize
new1.19 KB

Sorry wrong patch

lendude’s picture

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.

lobodakyrylo’s picture

Version: 9.5.x-dev » 10.1.x-dev
StatusFileSize
new602 bytes

This is a patch for 10.1.x version

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

p-neyens’s picture

This is a patch for 10.2.x version.

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

codebymikey’s picture

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 superseded by #2508796: Query string is appended multiple time after each AJAX request 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.

phthlaap’s picture

Status: Needs work » Needs review

I created a merge request and added tests.

phthlaap’s picture

Issue summary: View changes
phthlaap’s picture

I updated the issue summary to the correct format.

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests +Needs Review Queue Initiative

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.

pravesh_poonia’s picture

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

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs title update

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

codebymikey’s picture

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

phthlaap’s picture

Title: Page index isn't reset when use AJAX for Views module » Pagination not working correctly in AJAX view with exposed filters
Status: Needs work » Needs review
smustgrave’s picture

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

smustgrave’s picture

Status: Needs review » Needs work

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

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

the_g_bomb’s picture

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.

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

vinmayiswamy’s picture

Status: Needs work » Needs review
StatusFileSize
new309.7 KB
new299.59 KB

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.
Before:
Before-fix-3323574

After:
After-fix-3323574

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!

smustgrave’s picture

Status: Needs review » Needs work

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

codebymikey’s picture

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

oily’s picture

Rebased both MR's. Both pipelines all green. Test-only test fails as it should for MR !7485 but it passes as it should not for the other MR. Hiding that MR.

Now need to determine which of the 2x versions of the regex in the MR's is the 'correct' or 'best' version. Also need to manually test the issue (via UI) this issue in Drupal 11.x to see if it has been fixed in core RE: #34.

oily changed the visibility of the branch 3323574-Remove-pagination-in-ajax-15 to hidden.

johnv’s picture

Title: Pagination not working correctly in AJAX view with exposed filters » Views pager not working correctly in AJAX view with exposed filters
Related issues: +#3351226: Views pager next item lead to the first page always with the filled exposed filter and enabled ajax
the_g_bomb’s picture

After the comment in #34 by codebymikey, I re-tested the 11.x branch and created the view as described.

Using the pager works fine as the pager also uses ajax.

The issue still exists if you visit the page:
/test?page=4 and then using the title filter. The page=4 URL slug remains after the filtering by ajax happens, so there are no results.

oily’s picture

Looks like we can move to 'Needs review' after #39. Except perhaps #36: 'Now need to determine which of the 2x versions of the regex in the MR's is the 'correct' or 'best' version.'

the_g_bomb’s picture

Using:

composer config --json --merge extra.patches.drupal/core '{"Issue #3323574: Fixing regex": "https://git.drupalcode.org/project/drupal/-/merge_requests/9355.diff"}'

The view works and shows the articles with test in the name, even though the ?page=4 remains.

the_g_bomb’s picture

Also using:

composer config --json --merge extra.patches.drupal/core '{"Issue #3323574: Fixing regex": "https://git.drupalcode.org/project/drupal/-/merge_requests/7485.diff"}'

The view still works and shows the articles with test in the name, even though the ?page=4 remains.

the_g_bomb’s picture

Status: Needs work » Needs review

Could do with a confirmation and a decision on which regex to use

johnv’s picture

Title: Views pager not working correctly in AJAX view with exposed filters » Pager not working correctly in AJAX view with exposed filters
Related issues: +#2921519: Pager doesn't return to previous page when deleting the content from last page

Please confirm that this issue is about 'Pager shows empty/nonexistent page N after applying an exposed filter, so new result has less then N pages'?
And that it is an identical/similar problem to #2921519: Pager doesn't return to previous page when deleting the content from last page, which has a solution in core/modules/views/src/Plugin/views/query/Sql.php.

Should both use cases benefit from the same solution?

kumudb’s picture

StatusFileSize
new5.71 MB

After enabling AJAX in a Views page in Drupal 11.x-dev version that lists Articles with an exposed Article Title filter, pagination behaves unexpectedly. The issue occurs under the following conditions:

  1. The pager is set to display 10 items per page.
  2. Navigating through multiple pages (e.g., reaching the 4th or 5th page)
  3. Performing a search for a unique item results in a blank page instead of displaying the expected results.

A screen recording has been provided to demonstrate the issue.

https://www.drupal.org/files/issues/2025-03-05/pagination_search_issue_w...

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs title update

Believe feedback has been addressed

  • catch committed e81a3e5b on 10.4.x
    Issue #3323574 by lobodakyrylo, the_g_bomb, phthlaap, oily, vinmayiswamy...

  • catch committed f56de53c on 10.5.x
    Issue #3323574 by lobodakyrylo, the_g_bomb, phthlaap, oily, vinmayiswamy...

  • catch committed 082069a1 on 11.1.x
    Issue #3323574 by lobodakyrylo, the_g_bomb, phthlaap, oily, vinmayiswamy...

  • catch committed c8c538e7 on 11.x
    Issue #3323574 by lobodakyrylo, the_g_bomb, phthlaap, oily, vinmayiswamy...
catch’s picture

Version: 11.x-dev » 10.4.x-dev
Status: Reviewed & tested by the community » Fixed

Makes sense to do the quick fix here, the additional test coverage will ensure #2508796: Query string is appended multiple time after each AJAX request covers everything.

Committed/pushed to 11.x, cherry-picked to 11.1.x, 10.5.x, 10.4.x, thanks!

prudloff’s picture

I merged this in #343535: Enable bookmarking of AJAX views and I believe this failing PaginationAJAXTest shows the new regex might cut query parameters that contain "page": https://git.drupalcode.org/issue/drupal-343535/-/jobs/4772946
For example '?items_per_page=gezgez&foo=bar'.replace(/q=[^&]+&?|page=[^&]+&?|&?render=[^&]+/, '') will return "?items_per_foo=bar".
Should I open a followup?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

rtnilesh.gupta’s picture

Hi @everyone,
I am using drupal 10.4.4 and still facing the paging issue when it loads the url than it shows the default 48 results and when i click on page 2 than i can see correct page result is coming for me, and when again going to first page number than it is showing the correct result, what ever is set in full pager count, and ajax is on for that views,
also I have tried the patch to apply, but no patch works for me.
please suggest.

chartmann’s picture

For the record, I just noticed this issue on one of my client sites. It was on Core 10.4.5. I updated to Core 10.4.6 and I'm still seeing the same issue. I will report back once I figure it out. I wanted to add this comment in case anyone is looking through issue queues.