Crud: select2_from_ajax impossible to select option

Created on 13 Jan 2020  Β·  7Comments  Β·  Source: Laravel-Backpack/CRUD

Bug report

What I did

https://youtu.be/LRMZx5YQxhg

What I expected to happen

Select option

What happened

impossible to select option

Code

// CRUD field
$this->crud->addField([
            // 1-n relationship
            'label' => "Π’ΠΈΠΏ отобраТСния", // Table column heading
            'type' => "select2_from_ajax_multiple",
            'name' => 'view', // the column that contains the ID of that connected entity
            'entity' => 'viewModel', // the method that defines the relationship in your Model
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => PropertiesViewTypes::class, // foreign key model
            'data_source' => url("api/propertyViews"), // url to controller search function (with /{id} should return model)
            'placeholder' => "Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ Ρ‚ΠΈΠΏ отобраТСния", // placeholder for the select
            'minimum_input_length' => 0, // minimum characters to type before querying results
            'dependencies' => ['type'], // when a dependency changes, this select2 is reset to null
            'method' => 'GET',
             'include_all_form_fields'  => false, // optional - only send the current field through AJAX (for a smaller payload if you're not using multiple chained select2s)
        ]);

// Property.class relation

public function viewModel()
    {
        return $this->belongsTo(PropertiesViewTypes::class, 'view', 'view');
    }

// Ajax Controller

public function index(Request $request) {
        $form = $request->input('form');

        foreach ($form as $f) {
            if($f['name'] === 'type') {
                return PropertiesViewTypes::where('property_type', '=', $f['value'])->paginate(10);
            }
        }

        return PropertiesViewTypes::paginate(10);
    }

    public function view(Request $request, $id) {
        return PropertiesViewTypes::find($id);
    }

Backpack, Laravel, PHP, DB version

PHP VERSION:

PHP 7.3.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:48:32) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.13, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.13-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

LARAVEL VERSION:

laravel/framework v6.10.1 The Laravel Framework.

BACKPACK VERSION:

backpack/crud 4.0.31 Quickly build an admin in...
backpack/generators 2.0.6 Generate files for larave...

needs-more-info

All 7 comments

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 mediums:

  • 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;

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

Hey @danbka33

Thanks for reporting in. Any JS error in console ?

At first glance i don't think it's from your controller. Can you also share the field definition of type select you use in dependencies ?

Best,
Pedro

$this->crud->addField([
            'label' => "Π’ΠΈΠΏ", // Table column heading
            'type' => "select2",
            'name' => 'type', // the column that contains the ID of that connected entity;
            'entity' => 'typeModel', // the method that defines the relationship in your Model
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => PropertiesType::class, // foreign key model,
            'pivot' => false,
        ]);

        $this->crud->addField([
            // 1-n relationship
            'label' => "Π’ΠΈΠΏ отобраТСния", // Table column heading
            'type' => "select2_from_ajax_multiple",
            'name' => 'view', // the column that contains the ID of that connected entity
            'entity' => 'viewModel', // the method that defines the relationship in your Model
            'attribute' => "name", // foreign key attribute that is shown to user
            'model' => PropertiesViewTypes::class, // foreign key model
            'data_source' => url("api/propertyViews"), // url to controller search function (with /{id} should return model)
            'placeholder' => "Π’Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ Ρ‚ΠΈΠΏ отобраТСния", // placeholder for the select
            'minimum_input_length' => 0, // minimum characters to type before querying results
            'dependencies' => ['type'], // when a dependency changes, this select2 is reset to null
            'method' => 'GET',
             'include_all_form_fields'  => false, // optional - only send the current field through AJAX (for a smaller payload if you're not using multiple chained select2s)
        ]);

if there was a simple js error, instead of issue there would be a PR with a fix :)

Hahaha thanks @danbka33 for the extra info.

I will give it a try in my end, i just wanted to make sure my field definition was the same of yours.

I will let you know my tests and ping you with feedback :)

Thanks again.

Best,
Pedro

Hello @danbka33

I went for it this morning and could not reproduce your issue. https://recordit.co/t87zYm42km

Are you overwritting any of those two files (select2 and select2_from_ajax_multiple) ?

$this->crud->addField([
            'name'  => 'field_3',
            'label' => 'testing',
            'type'  => 'select2',
            'model' => "Backpack\NewsCRUD\app\Models\Article",
            'entity' => 'article',
            'attribute' => 'title',
            'pivot' => false
        ]);
        $this->crud->addField([ // select2_from_ajax: 1-n relationship
            'label'                => "int ajax not null", // Table column heading
            'type'                 => 'select2_from_ajax_multiple',
            'name'                 => 'field_2', // the column that contains the ID of that connected entity;
            'entity'               => 'articles', // the method that defines the relationship in your Model
            'attribute'            => 'title', // foreign key attribute that is shown to user
            'model'                => "Backpack\NewsCRUD\app\Models\Article", // foreign key model
            'data_source'          => url('api/article'), // url to controller search function (with /{id} should return model)
            'placeholder'          => 'Select an article', // placeholder for the select
            'minimum_input_length' => 0, // minimum characters to type before querying results
            'dependencies' => ['field_3'],
            'method' => 'GET',
             'include_all_form_fields'  => false,

        ]);

Best,
Pedro

PropertiesViewTypes Database
Π‘Π½ΠΈΠΌΠΎΠΊ экрана 2020-01-19 Π² 14 12 44

PropertiesViewTypes Model

/**
 * App\Models\PropertiesViewTypes
 *
 * @property string $view
 * @property string $name
 */
class PropertiesViewTypes extends Model
{
    protected $table = 'properties_view_types';
    public $timestamps = false;
    protected $fillable = [
        'name'
    ];
}

I'm add

protected $primaryKey = 'view';

after that it worked.

I think it’s worth adding a comment to the documentation that when using a non-standard primary key, you must specify this explicitly.

Thanks @pxpm for participating

Hey @danbka33 .

Very happy you found the issue.
Indeed it's worth to add but it is already on laravel docs.

Also you might want to add the primary key type if it's not integer.

Best,
Pedro

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorgepires picture jorgepires  Β·  3Comments

M0H3N picture M0H3N  Β·  3Comments

mikael1000 picture mikael1000  Β·  3Comments

packytagliaferro picture packytagliaferro  Β·  3Comments

sonoftheweb picture sonoftheweb  Β·  3Comments