【问题标题】:Yii2 kartik typeahead - get number of suggestionsYii2 kartik typeahead - 获取建议数量
【发布时间】:2015-04-14 05:33:44
【问题描述】:

我在视图中为 Yii2 使用 Kartik 的 typeahead 小部件:

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);

【问题讨论】:

    标签: widget yii2 twitter-typeahead


    【解决方案1】:

    在检查了 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 包含来自服务器的响应 (json)。

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

    使用此功能,我可以获取从服务器发送的建议的数量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      相关资源
      最近更新 更多