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

"Select All" / "Unselect All" not shown when property hashVarLoadPosition is View::POS_READY (some options does not pass from PHP to JS) #350

Closed
10 of 19 tasks
dmitry-kulikov opened this issue Aug 12, 2022 · 0 comments

Comments

@dmitry-kulikov
Copy link
Contributor

dmitry-kulikov commented Aug 12, 2022

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • The issue still exists against the latest master branch of yii2-widget-select2.
  • This is not an usage question. I confirm having read the widget documentation and demos.
  • This is not a general programming / coding question. (Those should be directed to the webtips Q & A forum).
  • This is not a source plugin (select2) issue. (Those should be directed to the plugin issues repo).
  • I have attempted to find the simplest possible steps to reproduce the issue.
  • I have included a failing test as a pull request (Optional).

Steps to reproduce the issue

  1. Execute code:

    <?php
    
    use kartik\select2\Select2;
    use yii\web\View;
    
    echo Select2::widget([
        'name' => 'colors',
        'data' => ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue'],
        'hashVarLoadPosition' => View::POS_READY,
        'options' => [
            'placeholder' => 'Select colors ...',
            'multiple' => true,
        ],
    ]);
  2. Click on Select2 input to open it.

Expected behavior and actual behavior

When I follow those steps, I don't see "Select All" or "Unselect All" buttons when Select2 opened.

I was expecting to see "Select All" or "Unselect All" buttons when Select2 opened.

Environment

Browsers

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer
  • Safari

Operating System

  • Windows
  • Mac OS X
  • Linux
  • Mobile

Libraries

  • jQuery version: 2.2.4
  • yii2-widget-select2 version: 2.2.3

Isolating the problem

  • This bug happens on the widget demos page
  • The bug happens consistently across all tested browsers
  • This bug happens when using yii2-widget-select2 without other plugins
  • I can reproduce this bug in a jsbin

The problem is 'hashVarLoadPosition' => View::POS_READY and this change 6a6840c#diff-2891a29e6f4f80976e580d1b4c555a1ad7283461a1ca565cae435633bfe4f6bcL435-R440
(which is correct I guess, because it fixes issue #325).
If 'hashVarLoadPosition' => View::POS_HEAD (by default) then code

$view->registerJs("var {$this->_s2OptionsVar} = {$options};", $this->hashVarLoadPosition);

registers variable in the global namespace and it is available in

var opts = window[optVar] || {}, themeCss = opts.themeCss, sizeCss = opts.sizeCss, doOrder = opts.doOrder,

but if 'hashVarLoadPosition' => View::POS_READY then the same code registers variable as local, enclosed by

jQuery(function ($) {
    // some code
});

and aforementioned

var opts = window[optVar] || {}, // other code

always silently results in empty object.
I think that

$view->registerJs("var {$this->_s2OptionsVar} = {$options};", $this->hashVarLoadPosition);

can be safely replaced on

$view->registerJs("window.{$this->_s2OptionsVar} = {$options};", $this->hashVarLoadPosition);

I'll prepare PR soon.

dmitry-kulikov added a commit to dmitry-kulikov/yii2-widget-select2 that referenced this issue Aug 12, 2022
…ty hashVarLoadPosition is View::POS_READY (some options does not pass from PHP to JS)
kartik-v added a commit that referenced this issue Aug 29, 2022
Fix #350: "Select All" / "Unselect All" not shown when property hashVarLoadPosition is View::POS_READY (some options does not pass from PHP to JS)
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

1 participant