|
1 | | -# yii2-widget-easyautocomplete |
| 1 | +## yii2-widget-easyautocomplete |
2 | 2 | Yii2 widget for the EasyAutocomplete plugin (https://github.com/pawelczak/EasyAutocomplete). |
| 3 | + |
| 4 | +## Install |
| 5 | + |
| 6 | +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). Check the [composer.json](https://github.com/gitrequests/yii2-widget-easyautocomplete/composer.json) for this extension's requirements and dependencies. |
| 7 | + |
| 8 | +To install, either run |
| 9 | + |
| 10 | +``` |
| 11 | +$ php composer.phar require gitrequests/yii2-widget-easyautocomplete "@dev" |
| 12 | +``` |
| 13 | + |
| 14 | +or add |
| 15 | + |
| 16 | +``` |
| 17 | +"gitrequests/yii2-widget-easyautocomplete": "@dev" |
| 18 | +``` |
| 19 | + |
| 20 | +to the ```require``` section of your `composer.json` file. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +```php |
| 25 | +echo EasyAutocomplete::widget([ |
| 26 | + 'pluginOptions' => [ |
| 27 | + 'url' => Url::to('data/countries.json'), |
| 28 | + 'getValue' => 'name' |
| 29 | + ] |
| 30 | +]); |
| 31 | +``` |
| 32 | + |
| 33 | +You can also use this widget in an [[ActiveForm]] using the [[ActiveField::widget()|widget()]] |
| 34 | +method, for example like this: |
| 35 | + |
| 36 | +```php |
| 37 | +<?= $form->field($model, 'address')->widget(EasyAutocomplete::className(), [ |
| 38 | + 'pluginOptions' => [ |
| 39 | + 'url' => Url::to('data/countries.json'), |
| 40 | + 'getValue' => 'name' |
| 41 | + ] |
| 42 | +]); |
| 43 | +``` |
| 44 | +For use with list events||functions (custom match function for example): |
| 45 | + |
| 46 | +```php |
| 47 | +<?= $form->field($model, 'address')->widget(EasyAutocomplete::className(), [ |
| 48 | + 'pluginOptions' => [ |
| 49 | + 'url' => Url::to('data/countries.json'), |
| 50 | + 'getValue' => 'name' |
| 51 | + 'list' => [ |
| 52 | + 'maxNumberOfElements' => 10, |
| 53 | + 'match' => [ |
| 54 | + 'enabled' => true, |
| 55 | + 'method' => new JsExpression(<<<JavaScript |
| 56 | + function(element, phrase) { |
| 57 | + |
| 58 | + var searches = phrase.split(' ') |
| 59 | + var count = 0; |
| 60 | + |
| 61 | + for (var search of searches) { |
| 62 | + if (element.search(search) > -1) { |
| 63 | + count++; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return searches.length === count |
| 68 | + } |
| 69 | + JavaScript |
| 70 | + ) |
| 71 | + ] |
| 72 | + ] |
| 73 | + ] |
| 74 | +]); |
| 75 | +``` |
0 commit comments