Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.18k stars 897 forks source link

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

Open dankhan-helmet opened 4 weeks ago

dankhan-helmet commented 4 weeks ago

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
welcome[bot] commented 4 weeks ago

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:

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