kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

Unable to change 'noResults' content at initialization #251

Closed IGaMbLeRI closed 5 years ago

IGaMbLeRI commented 6 years ago

Prerequisites

Steps to reproduce the issue

  1. Setting language property to an array causes an error. Respectfully so, according to the documentation the widget will accept only a string.
  2. Therefore, in order to achieve the objective of changing the appearance (and functionality) of "No results found.", I attached the following JS:
    $(".select2").select2({
    language : {
        noResults : function(params) {
            return "Some other stuff here";
        }
    }
    });

    the following option was also advised in https://github.com/select2/select2/issues/3576 :

    escapeMarkup: function (markup) {
    return markup;
    }

Expected behavior and actual behavior

When I follow those steps, I see:

Although the inserted JS did not trigger besides being correctly attached at the bottom of the page, running the JS from Console produced one aspect of the expected result.

As it turns out, running the JS will effectively reset the existing Select2 along with all data and styles, but it will also change the text of the noResults! This is an expected behaviour for the Select2 plugin, but the plugin also allows to set these variables during initialization so you don't have to change them on the go.

I was expecting:

Frankly, I was expecting the Widget to accept array as language, so I could in some way pass the custom noResults content, such as a button to add the Input to the Dropdown list and select that value. All that without having to recreate the widget in JS.

Environment

Browsers

Operating System

Libraries

Isolating the problem

lordcroci commented 6 years ago

I have the same problem, hope someone is gonna find a solution.. please UP!

kartik-v commented 6 years ago

You need to escape the JS functions via yii\web\JsExpression when setting key values within PHP arrays - so that they are encoded properly:

Example:

'pluginOptions' => [
    'escapeMarkup' => new yii\web\JsExpression('function (markup){return markup}')
]
lordcroci commented 6 years ago

thanks a lot!

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.