PHP code example of yii-cms / yii2-typeahead

1. Go to this page and download the library: Download yii-cms/yii2-typeahead library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii-cms / yii2-typeahead example snippets


             echo Typeahead::widget([
                'id' => 'typeahead-id',
                'name' => 'typeahead-name',
                'clientOptions' => [
                    'displayKey' => 'value',
                    'templates' => [
                        'suggestion' => new JsExpression("function(data){ return '<p>' + data.value + '</p>'; }"),
                    ],
                ],
                'events' => [
                    'typeahead:selected' => new JsExpression(
                        'function(obj, datum, name) { window.location = datum.url; }'
                    ),
                ],
                'bloodhoundOptions' => [
                    'remote' => [
                        'url' => Url::to(['controller/action']) . '?q=%QUERY',
                        'ajax' => ['data' => [
                            'id' => $model->id,
                        ]],
                        'replace' => new JsExpression("
                            function(url, query) {
                                return url.replace('%QUERY', query);
                            }
                        "),
                        'filter' => new JsExpression("
                            function(list) {
                                return $.map(list, function(item) { return { value: item.name, url: item.url }; });
                            }
                        "),
                    ],
                ],
                'options' => ['class' => 'form-control', 'placeholder' => 'Title', 'autofocus' => true],
            ]);