-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyAutocomplete.php
131 lines (121 loc) · 3.05 KB
/
EasyAutocomplete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace gitrequests\easyautocomplete;
use yii\bootstrap\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* EasyAutocomplete jquery widget.
*
* ```php
* echo EasyAutocomplete::widget([
* 'pluginOptions' => [
* 'url' => Url::to('data/countries.json'),
* 'getValue' => 'name'
* ]
* ]);
* ```
*
* You can also use this widget in an [[ActiveForm]] using the [[ActiveField::widget()|widget()]]
* method, for example like this:
*
* ```php
* <?= $form->field($model, 'address')->widget(EasyAutocomplete::className(), [
* 'pluginOptions' => [
* 'url' => Url::to('data/countries.json'),
* 'getValue' => 'name'
* ]
* ]);
* ```
* For use with list events||functions (custom match function for example):
*
* ```php
* <?= $form->field($model, 'address')->widget(EasyAutocomplete::className(), [
* 'pluginOptions' => [
* 'url' => Url::to('data/countries.json'),
* 'getValue' => 'name'
* 'list' => [
* 'maxNumberOfElements' => 10,
* 'match' => [
* 'enabled' => true,
* 'method' => new JsExpression(<<<JavaScript
* function(element, phrase) {
*
* var searches = phrase.split(' ')
* var count = 0;
*
* for (var search of searches) {
* if (element.search(search) > -1) {
* count++;
* }
* }
*
* return searches.length === count
* }
* JavaScript
* )
* ]
* ]
* ]
* ]);
* ```
*
* The Autocomplete text field is implemented based on the
* [jQuery EasyAutocomplete plugin](https://github.com/pawelczak/EasyAutocomplete).
*
* @author Dmitry Bankousky <[email protected]>
* @since 2.0
*/
class EasyAutocomplete extends InputWidget
{
/**
* The name of the jQuery plugin to use for this widget.
*/
const PLUGIN_NAME = 'easyAutocomplete';
/**
* @var array the HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'form-control'];
/**
* @var string the type of the input tag.
*/
public $inputType = 'text';
/**
* @var array the JQuery plugin options for plugin.
*/
public $pluginOptions = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->initPluginOptions();
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();
if ($this->hasModel()) {
echo Html::activeInput($this->inputType, $this->model, $this->attribute, $this->options);
} else {
echo Html::input($this->inputType, $this->name, $this->value, $this->options);
}
}
protected function initPluginOptions()
{
$this->pluginOptions = empty($this->pluginOptions) ? '{}' : Json::htmlEncode($this->pluginOptions);
}
/**
* Registers the needed client script and options.
*/
public function registerAssets()
{
$view = $this->getView();
EasyAutocompleteAsset::register($view);
$js = 'jQuery("#' . $this->options['id'] . '").' . self::PLUGIN_NAME . '(' . $this->pluginOptions . ');';
$view->registerJs($js);
}
}