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

Error when want to clear selection in Select2 widget #378

Open
mavelar opened this issue Mar 29, 2019 · 1 comment
Open

Error when want to clear selection in Select2 widget #378

mavelar opened this issue Mar 29, 2019 · 1 comment

Comments

@mavelar
Copy link

mavelar commented Mar 29, 2019

My current code is like this:

<?=$form->field($model,'customer_id')->widget(Select2::classname(), [ 'data' => ArrayHelper::map($customer,'id','customer_name'), 'theme' => 'bootstrap', 'options' => [ 'placeholder' => Yii::t('app','Select customer...'), ], 'pluginOptions' => [ 'allowClear' => true ], ])->label(Yii::t('app','Customer'));?>

As you can see allowClear is set true, but when I try to clear my selection a javascript error is trigger:

TypeError: $el.data(...) is undefined

And refer to line 91

initS2Unselect = function () { var $el = $(this), opts = $el.data('select2').options; opts.set('disabled', true); setTimeout(function() { opts.set('disabled', false); $el.trigger('krajeeselect2:cleared'); }, 1); };

Another to add to this post, is that this form is an ajax call:

Sketch


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@mavelar mavelar changed the title Error when want to clear selection Error when want to clear selection in Select2 widget Mar 29, 2019
@Lachee
Copy link

Lachee commented Mar 24, 2020

I have the same issue, however it is Cannot read property 'id' of undefined instead.
failure

I am using it as a filter with the gridview:

[
            'attribute'         => 'repairer.name',
            'label'             => 'Repairer',
            'filterType'        => GridView::FILTER_SELECT2,
            'filterWidgetOptions'   => [
                'pluginOptions' => [
                    'allowClear' => true,
                    'ajax' => [
                        'url' => Url::to(['/contact/list', 'mode' => 'repairer']),
                        'dataType' => 'json',
                        'delay'=> 10,              
                        'data' => new JsExpression('function(params) { return { q: params.term, page: params.page}; }'),
                        'cache' => true
                    ]
                ]
            ]
        ],

My solution was to add a placeholder. This seems to have fixed it:

  'options' => ['placeholder' => ''],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants