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

select2_from_ajax does not show the field value when update ( with entity => false ) #5366

Closed
miquelangeld opened this issue Nov 7, 2023 · 10 comments

Comments

@miquelangeld
Copy link

miquelangeld commented Nov 7, 2023

I'm using this field type

$this->crud->addField([ 'label' => 'Booking', 'type' => 'select2_from_ajax', 'name' => 'booking', 'entity' => false, 'attribute' => 'booking', 'model' => "App\Models\PayByLink", 'data_source' => url('/admin/bookings/ajax-bookings-avalon-options'), ]);
when I create a new entry it works, the info is stored in database, but when I edit the entry the field shows empty.
I'm using entity False because this field is not a relationship, just a text field. I use the same solution in many places in that project and I have the same problem, if i set entity to false the field shows empty

My workaround for now is to show the field as a regular text field in the update view in order to show the stored value, but I would like to use the ajax search in the update view too.

PHP VERSION:

PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies

LARAVEL VERSION:

10.30.1.0

BACKPACK PACKAGE VERSIONS:

backpack/backupmanager: v5.0.0
backpack/basset: 1.2.1
backpack/crud: 6.3.0
backpack/devtools: 2.0.3
backpack/editable-columns: 3.0.2
backpack/generators: v4.0.2
backpack/logmanager: v5.0.1
backpack/permissionmanager: 7.1.1
backpack/pro: 2.0.18
backpack/settings: 3.1.0
backpack/theme-coreuiv2: 1.2.2
backpack/theme-coreuiv4: 1.1.1
backpack/theme-tabler: 1.1.1

@tabacitu
Copy link
Member

tabacitu commented Nov 8, 2023

Hi @miquelangeld ,

Huh... turns out this is a known issue (see #3195 ), don't know why we haven't fixed this yet 🤷‍♂️ Sorry for that.

TLDR:

  • the select2_from_ajax field requires a model
  • when you want a select2 from an API endpoint... you might not have a Model for that column (no relationship, nothing)

Possible solutions:

  • Solution A. We make select2_from_ajax work without a Model. Could be a breaking change though.
  • Solution B. We create a new field, select2_from_api that has zero relationship logic. Doesn't work with relationships at all.

What do you think @pxpm ? Any better solution? Which one do you think we should do?

@miquelangeld
Copy link
Author

Hi @tabacitu In this project in all cases there is a linked model which I need to feed up with external data (sometimes an external api or sometimes an endpoint within the same project but from external databases, in fact the data is inserted correctly for the model PayByLink in the create view. the problem comes when I want to update and the field doesn't shows the stored data.

If I may, the idea of a select2_from_api field type may be a solution less disruptive for you, the need to query external data to fill a model field it's very present in all my projects.

@tabacitu
Copy link
Member

Hmm @miquelangeld then maybe it's how you've defined that Model... sounds like it doesn't get picked up as a "normal" Eloquent model. What did you use for your "fake" models, did you use https://laravel-news.com/laravel-sushi ? I've used it a bunch of times and it's GREAT for creating Fake models that actually get their data from an array/file/api/etc.

@miquelangeld
Copy link
Author

miquelangeld commented Nov 15, 2023

Hi @tabacitu Let me give you more details, maybe I'm doing something wrong or maybe I'm not explaining the issue correctly.

It's not a fake model. I have a Laravel model named PayByLink, one of the fields is a string field named 'booking'. The users needs to search among thousands of booking codes from another system the one they need (that's why I need ajax), but there aren't any relationship, I just want to store the value from the endpoint (That's why i set entity to false)

The CRUD Field is defined like this:

$this->crud->addField([ 'label' => 'Booking', 'type' => 'select2_from_ajax', 'name' => 'booking', 'entity' => false, 'attribute' => 'booking', 'model' => "App\Models\PayByLink", 'data_source' => url('/admin/bookings/ajax-bookings-avalon-options'), ]);

the endpoint return for example this:

[{"id":"MURR23000001-1","booking":"MURR23000001-1 Localizador: 40737RT33"},{"id":"MURR23000002-1","booking":"MURR23000002-1 Localizador: 4073769XC"},{"id":"MURR23000003-1","booking":"MURR23000003-1 Localizador: SHC24_025"},{"id":"MURR23000004-1","booking":"MURR23000004-1 Localizador: OTH7453390"},{"id":"MURR23000005-1","booking":"MURR230044004-1 Localizador: OTB345691"}]

In the dropdown options shows the value of 'booking' and in database is stored the value of 'id'

so, when the user pick the option "MURR230044000-1 Localizador: 40737RT33" In database the data stored is 'MURR23000001-1' So far so good.

But if I try to edit any PayByLink entry, the field not shows the current value, just the empty dropdown.

@miquelangeld
Copy link
Author

Hi @tabacitu I don't know if you have had a chance to take a look at my last update. Thanks in advance

@miquelangeld
Copy link
Author

Hi, any chance to get some news about this? Thanks!

@pxpm
Copy link
Contributor

pxpm commented Dec 6, 2023

@miquelangeld this is a technical limitation of the select2_from_ajax. Sorry, you are not doing nothing wrong.😞

We plan to create select2_from_api to avoid messing (with the already messy) select2 from ajax code that should just fit in your use case.

Can't promisse a date, but it's on our January roadmap if everything goes accordingly.

image

Cheers

@miquelangeld
Copy link
Author

Thanks @pxpm Great!

@miquelangeld
Copy link
Author

Hi @pxpm any news about this feature? Thanks in advance

@miquelangeld
Copy link
Author

I close the issue since select2_json_from_api is released!!

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

No branches or pull requests

3 participants