AutoComplete.php 1.31 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
{
	/**
	 * Renders the widget.
	 */
	public function run()
	{
49
		echo $this->renderWidget();
Qiang Xue committed
50
		$this->registerWidget('autocomplete', AutoCompleteAsset::className());
Alexander Kochetov committed
51 52 53
	}

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