kartik-v / yii2-widget-typeahead

Enhanced Yii2 wrapper for the Twitter Typeahead plugin (sub repo split from yii2-widgets).
Other
45 stars 17 forks source link

Loading Typeahead widget through ajax #26

Closed elvenpath closed 7 years ago

elvenpath commented 7 years ago

I have a typeahead element in a partial loaded with ajax:

<?= $form->field($model, '[' . $model_counter . ']creativeName')
    ->widget(Typeahead::classname(), [
        'options' => ['placeholder' => 'Search...'],
        'dataset' => [
            [
                'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace(\"value\")",
                'display' => 'value',
                'minLength' => 3,
                'remote' => [
                    'url' => Url::to(['user/members']) . '?q=%QUERY',
                    'wildcard' => '%QUERY',
                ],
            ],
        ],
        'pluginEvents' => [
            "typeahead:select" => 'function(event, response) {
            $("#' . Html::getInputId($model, '[' . $model_counter . ']creative_id') . '").val(response.id);

        }',
        ],
        'pluginOptions' => [
            'highlight' => true,
        ],
    ]
) ?>

I'm getting

Uncaught ReferenceError: typeahead_7864e59a is not defined

The reason seems to be that plugin options are not registered (actualy they are registered in head with $this->registerWidgetJs("window.{$this->_hashVar} = {$encOptions};\n", View::POS_HEAD);, but I don't have a layout since I'm using renderAjax()

Latest version of kartik-v/yii2-krajee-base (1.8) seem to have fixed this with hashVarLoadPosition, but widget-typeahead has version 1.7 locked in composer.

Thanks, Vlad

kartik-v commented 7 years ago

Resolved via updates to release v1.0.2.