kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
558 stars 175 forks source link

minimuminputlength = 0 is ignored #349

Open alexjohnp opened 7 years ago

alexjohnp commented 7 years ago

Hello, I'm trying to show results from a select2 with ajax, but minimuminputlength parameter is ignored when set to 0. What I need to do is to run the ajax call when the select2 is clicked (i.e. with no need for the user to type any key).

Here the view's code

$form->field($model, 'PROVINCIA')->widget(Select2::classname(), [

'initValueText' => RegioniProvince::getProvinciaById($model->PROVINCIA),
'value' => RegioniProvince::getProvinciaById($model->PROVINCIA),// set the initial display text
'options' => ['placeholder' => 'Scegli una provincia ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'language' => [
'errorLoading' => new JsExpression("function () { return 'In attesa dei risultati...'; }"),
],
'ajax' => [
'url' => $urlProvincia,
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(PROVINCIA) { return PROVINCIA.text; }'),
'templateSelection' => new JsExpression('function (PROVINCIA) { return PROVINCIA.text; }'),
],
]);

$urlProvincia is resolved in controller as follows:

public static function getProvinciaForSelect($q = null, $id = null) {
        $query = new Query;
        $query->select('id, NOME_PROVINCIA AS text')
        ->from('REGIONI_PROVINCE')
        ->where(['like', 'NOME_PROVINCIA', $q])
        ->limit(20);
        $command = $query->createCommand();
        $data = $command->queryAll();

        return $data;
    }

I couldn't find any hint by reading the documentation, or even elsewhere. Is this the way select2 is supposed to be used, or is there anything wrong?

Thank you in advance. A.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/51415016-minimuminputlength-0-is-ignored?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github).
alexjohnp commented 7 years ago

Sorry, the final part of the message got truncated and I didn't notice it.

The previous code shows minimumInputLength = 3 chars, and it works perfectly. If i try to set minimumInputLength = 0, so that the controller should be fired immediately (i.e. without any other user input), nothing happens. Just as that parameter is completely ignored.

I couldn't find any hint by reading the documentation, or even elsewhere. Is this the way select2 is supposed to be used, or is there anything wrong?

Thank you in advance. A.