Kartik Yii2 Typeahead Widget事件监听器无法正常工作

时间:2015-05-31 18:31:38

标签: events yii2 twitter-typeahead

我的视图中有一个Kartik Yii2输入小部件:

echo \kartik\typeahead\Typeahead::widget([
    'name' => 'serial_product',
    'options' => [
        'placeholder' => 'Product Name / SKU / Serial Number / Coupon Code',
        'autofocus' => "autofocus",
        'id' => 'serial_product'
    ],
    'scrollable' => TRUE,
    'pluginOptions' => [
        'highlight' => TRUE,
        'minLength' => 3
    ],
    'dataset' => [
        [
            'remote' => [
                'ttl' => 0,
                'url' => Url::to(['invoice/ajaxgetinventoryitemsnew']) . '?search=%QUERY',
                'ajax' => ['complete' => new \yii\web\JsExpression("function(response){jQuery('#serial_product').removeClass('loading');checkresult(response.responseText);return true}")]
            ],
            'limit' => 10
        ]
    ],
    'pluginEvents' => [
        "typeahead:selected" => "function(obj, item) { checkresult2(item); return true; }",
        "typeahead:render" => "function() { console.log('Whatever...'); }",
    ],
]);

第一个"插件事件" (typeahead:selected)正在被触发。 第二个"打字:渲染"未触发,控制台上没有错误。

为什么不触发渲染事件?

版本:typeahead.js 0.10.5 | Kartik Typeahead.php 1.0.0

2 个答案:

答案 0 :(得分:0)

也许:使用kartik \ widgets \ Typeahead

$data = [
    'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 
    'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
    'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana'
];

// Usage with ActiveForm and model (with search term highlighting)
echo \$form->field(\$model, 'state_3')->widget(Typeahead::classname(), [
    'options' => ['placeholder' => 'Filter as you type ...'],
    'pluginOptions' => ['highlight'=>true],
    'dataset' => [
        [
            'local' => $data,
            'limit' => 10
        ]
    ]
]);

答案 1 :(得分:0)

我已在我的项目中测试了您的代码。

use kartik\typeahead\Typeahead;

<?= $form->field($model, 'country')->widget(Typeahead::classname(), [
    'pluginOptions' => ['highlight' => true],
    'options' => [
        'placeholder' => 'Начните ввод страны'
    ],
    'dataset' => [
        [
            'local' => $countries,
            'limit' => 10
        ]
    ],
    'pluginEvents' => [
        "typeahead:selected" => "function(obj, item) { 
            console.log(item); return true; }",
        "typeahead:render" => "function() { 
            console.log('Whatever...'); }",
    ],
    ]);  
?>

效果很好。任何时候我键入一个char控制台响应&#34;无论什么......&#34;。 &#34;选择&#34;活动也有效。

由于 checkresult2(item)功能,您的代码可能无法正常工作。