Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Pagination not working on Select2 field with morphOptions when only two pages of results #5708

Open
dankhan-helmet opened this issue Oct 27, 2024 · 1 comment
Labels

Comments

@dankhan-helmet
Copy link

Bug report

What I did

Created a CRUD::Field on an morphable relation, defining an ajax datasource for the results:

CRUD::field([
            'name' => 'morphable',
            'label' => 'Morphable Field',
            'morphOptions' => [
                ['App\Models\User', 'User, [
                    'attribute' => 'email',
                    'method' => 'POST',
                    'delay' => 500,
                    'data_source' => backpack_url('model/fetch/users'),
                ]],
            ],
        ]);

The fetch method is defined as:

protected function fetchUsers()
    {
        return $this->fetch([
            'model' => \App\Models\Users::class,

            // Custom search to allow us to search full name
            'query' => function ($model) {
                $searchTerm = request()->input('q');
                return $model->WhereRaw('CONCAT(`firstname`," ",`lastname`) LIKE "%'.$searchTerm.'%"');
            },
            'paginate' => 2,      // <== just to highlight the problem
        ]);
    }

What I expected to happen

The results list would show 2 records, and the link to loading more results if there is another results page

What happened

It didn't show the loading more results link, it only showed two results, even though there were more results on another page

What I've already tried to fix it

I noticed that in select2_from_ajax.blade.php the code for deciding to paginate looks like this:

paginate = data.next_page_url !== null;

But in morphTo_select_ajax.blade.php the code incorrectly uses:

paginate = data.current_page < data.last_page;

But because data.current_page returns 1 for the first page, not 0, the pagination decision will always fail if there's only two pages as the last_page index will be the same as the current_page index.

Here's the data parameter js console output from morphTo_select_ajax.blade.php@processResult#200 if there's only two pages of data to return:

{
  current_page: 1,
  data: {
    0: {...},
    1: {...}
  },
  first_page_url: "http://127.0.0.1:8001/...?page=1",
  from: 1,
  next_page_url: "http://127.0.0.1:8001/...?page=2",
  path: "http://127.0.0.1:8001/...",
  per_page: 2,
  prev_page_url: null,
  to: 2
}

The code to decide whether to paginate obviously fails as it thinks the current page is the last page.

I fixed this by copying to the code from select2_from_ajax.blade.php in processResults: function (data, params) at line 208:

//paginate = data.current_page < data.last_page;
paginate = data.next_page_url !== null;

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yup, still there in:
"backpack/crud": "^6.7",
"backpack/pro": "^2.2",

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
8.3.12

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dom, hash, fileinfo, filter, ftp, gd, gettext, json, iconv, intl, SPL, ldap, mbstring, session, standard, pcntl, exif, mysqlnd, PDO, pdo_mysql, pdo_sqlite, Phar, posix, random, readline, Reflection, mysqli, SimpleXML, soap, sockets, sodium, tokenizer, xml, xmlreader, xmlwriter, xsl, zip, imap, pgsql, pdo_pgsql, xdebug

### LARAVEL VERSION:
11.29.0.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.6
backpack/crud: 6.7.41
backpack/generators: v4.0.7
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.21
backpack/theme-tabler: 1.2.14
Copy link

welcome bot commented Oct 27, 2024

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use GitHub Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - GitHub Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant