Class yii\jui\AutoComplete
Inheritance | yii\jui\AutoComplete » yii\jui\InputWidget » yii\jui\Widget » yii\base\Widget |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-jui/blob/master/AutoComplete.php |
AutoComplete renders an autocomplete jQuery UI widget.
For example:
echo AutoComplete::widget([
'model' => $model,
'attribute' => 'country',
'clientOptions' => [
'source' => ['USA', 'RUS'],
],
]);
The following example will use the name property instead:
echo AutoComplete::widget([
'name' => 'country',
'clientOptions' => [
'source' => ['USA', 'RUS'],
],
]);
You can also use this widget in an yii\widgets\ActiveForm using the yii\widgets\ActiveField::widget() method, for example like this:
<?= $form->field($model, 'from_date')->widget(\yii\jui\AutoComplete::classname(), [
'clientOptions' => [
'source' => ['USA', 'RUS'],
],
]) ?>
See also http://api.jqueryui.com/autocomplete/.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$attribute | string | The model attribute that this widget is associated with. | yii\jui\InputWidget |
$clientEventMap | array | Event names mapped to what should be specified in .on() . |
yii\jui\Widget |
$clientEvents | array | The event handlers for the underlying jQuery UI widget. | yii\jui\Widget |
$clientOptions | array | The options for the underlying jQuery UI widget. | yii\jui\Widget |
$model | \yii\base\Model | The data model that this widget is associated with. | yii\jui\InputWidget |
$name | string | The input name. | yii\jui\InputWidget |
$options | array | The HTML attributes for the widget container tag. | yii\jui\Widget |
$value | string | The input value. | yii\jui\InputWidget |
Public Methods
Method | Description | Defined By |
---|---|---|
init() | Initializes the widget. | yii\jui\InputWidget |
renderWidget() | Renders the AutoComplete widget. | yii\jui\AutoComplete |
run() | yii\jui\AutoComplete |
Protected Methods
Method | Description | Defined By |
---|---|---|
hasModel() | yii\jui\InputWidget | |
registerClientEvents() | Registers a specific jQuery UI widget events | yii\jui\Widget |
registerClientOptions() | Registers a specific jQuery UI widget options | yii\jui\Widget |
registerWidget() | Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events | yii\jui\Widget |
Method Details
Defined in: yii\jui\InputWidget::hasModel()
protected boolean hasModel ( ) | ||
return | boolean |
Whether this widget is associated with a data model. |
---|
protected function hasModel()
{
return $this->model instanceof Model && $this->attribute !== null;
}
Defined in: yii\jui\InputWidget::init()
Initializes the widget.
If you override this method, make sure you call the parent implementation first.
public void init ( ) |
public function init()
{
if ($this->hasModel() && !isset($this->options['id'])) {
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
}
parent::init();
}
Defined in: yii\jui\Widget::registerClientEvents()
Registers a specific jQuery UI widget events
protected void registerClientEvents ( $name, $id ) | ||
$name | string |
The name of the jQuery UI widget |
$id | string |
The ID of the widget |
protected function registerClientEvents($name, $id)
{
if (!empty($this->clientEvents)) {
$js = [];
foreach ($this->clientEvents as $event => $handler) {
if (isset($this->clientEventMap[$event])) {
$eventName = $this->clientEventMap[$event];
} else {
$eventName = strtolower($name . $event);
}
$js[] = "jQuery('#$id').on('$eventName', $handler);";
}
$this->getView()->registerJs(implode("\n", $js));
}
}
Defined in: yii\jui\Widget::registerClientOptions()
Registers a specific jQuery UI widget options
protected void registerClientOptions ( $name, $id ) | ||
$name | string |
The name of the jQuery UI widget |
$id | string |
The ID of the widget |
protected function registerClientOptions($name, $id)
{
if ($this->clientOptions !== false) {
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
$js = "jQuery('#$id').$name($options);";
$this->getView()->registerJs($js);
}
}
Defined in: yii\jui\Widget::registerWidget()
Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events
protected void registerWidget ( $name, $id = null ) | ||
$name | string |
The name of the jQuery UI widget |
$id | string |
The ID of the widget. If null, it will use the |
protected function registerWidget($name, $id = null)
{
if ($id === null) {
$id = $this->options['id'];
}
JuiAsset::register($this->getView());
$this->registerClientEvents($name, $id);
$this->registerClientOptions($name, $id);
}
Renders the AutoComplete widget.
public string renderWidget ( ) | ||
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);
}
}
public void run ( ) |
public function run()
{
$this->registerWidget('autocomplete');
return $this->renderWidget();
}