Yii2 kartik typeahead - 获得建议数量

时间:2015-04-14 05:33:45

标签: widget yii2 twitter-typeahead

我在视图中使用kartik的Ya2预先输入小部件:

echo \kartik\typeahead\Typeahead::widget([
    'name'          => 'serial_product',
    'options'       => [
        'placeholder' => 'Filter as you type ...',
        'autofocus'   => "autofocus"
    ],
    'scrollable'    => TRUE,
    'pluginOptions' => [
        'highlight' => TRUE,
        'minLength' => 3
    ],
    'dataset'       => [
        [
            'remote' => Url::to(['transfers/ajaxgetinventoryitemsnew']) . '?search=%QUERY',
            'limit'  => 10
        ]
    ],
    'pluginEvents'  => [
        "typeahead:selected" => "function(obj, item) { add_item(item.id); return false;}",
    ],
]);

如何在检索远程数据集以执行javascript函数后获取已加载建议的数量,如:

displaynumber(NUMBEROFSUGGESTIONS);

1 个答案:

答案 0 :(得分:0)

在检查了kartiks小部件的来源后,我提出了以下解决方案:

echo \kartik\typeahead\Typeahead::widget([
    'name'          => 'serial_product',
    'options'       => [
        'placeholder' => 'Filter as you type ...',
        'autofocus'   => "autofocus",
        'id'          => 'serial_product'
    ],
    'scrollable'    => TRUE,
    'pluginOptions' => [
        'highlight' => TRUE,
        'minLength' => 3
    ],
    'dataset'       => [
        [
            'remote' => [
                'url'  => Url::to(['transfers/ajaxgetinventoryitemsnew']) . '?search=%QUERY',
                'ajax' => ['complete' => new JsExpression("function(response)
                    {
                        jQuery('#serial_product').removeClass('loading');
                        checkresult(response.responseText);
                    }")]
            ],
            'limit'  => 10
        ]
    ],
    'pluginEvents'  => [
        "typeahead:selected" => "function(obj, item) { checkresult2(item); return false;}",
    ],
]);

其中response.responseText包含来自server(json)的响应。

function checkresult(response) {
    var arr = $.parseJSON(response);
    console.log(arr.length);
}

使用此功能,我可以得到服务器提供的建议数量。