AutoComplete.php 1.4 KB
Newer Older
Alexander Kochetov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\jui;

use yii\helpers\Html;

/**
 * AutoComplete renders an autocomplete jQuery UI widget.
 *
 * For example:
 *
 * ```php
Alexander Makarov committed
18
 * echo AutoComplete::widget([
Alexander Kochetov committed
19 20
 *     'model' => $model,
 *     'attribute' => 'country',
Alexander Makarov committed
21 22 23 24
 *     'clientOptions' => [
 *         'source' => ['USA', 'RUS'],
 *     ],
 * ]);
Alexander Kochetov committed
25 26 27 28 29
 * ```
 *
 * The following example will use the name property instead:
 *
 * ```php
Alexander Makarov committed
30 31 32 33 34 35
 * echo AutoComplete::widget([
 *     'name' => 'country',
 *     'clientOptions' => [
 *         'source' => ['USA', 'RUS'],
 *     ],
 * ]);
Alexander Kochetov committed
36 37 38 39 40 41
 *```
 *
 * @see http://api.jqueryui.com/autocomplete/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
42
class AutoComplete extends InputWidget
Alexander Kochetov committed
43
{
44 45 46 47 48 49 50 51
    /**
     * Renders the widget.
     */
    public function run()
    {
        echo $this->renderWidget();
        $this->registerWidget('autocomplete', AutoCompleteAsset::className());
    }
Alexander Kochetov committed
52

53 54 55 56 57 58 59 60 61 62 63 64
    /**
     * Renders the AutoComplete widget.
     * @return string the rendering result.
     */
    public function renderWidget()
    {
        if ($this->hasModel()) {
            return Html::activeTextInput($this->model, $this->attribute, $this->options);
        } else {
            return Html::textInput($this->name, $this->value, $this->options);
        }
    }
Alexander Kochetov committed
65
}