PHPackages                             conquer/select2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. conquer/select2

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

conquer/select2
===============

Yii2 Select2 widget

1.4.4(9y ago)1678.0k↓19.6%64MITPHPPHP &gt;=5.4.0

Since Jul 8Pushed 8y ago4 watchersCompare

[ Source](https://github.com/borodulin/yii2-select2)[ Packagist](https://packagist.org/packages/conquer/select2)[ Docs](https://github.com/borodulin/yii2-select2)[ RSS](/packages/conquer-select2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)DependenciesVersions (16)Used By (4)

Select2 widget for Yii2 framework
=================================

[](#select2-widget-for-yii2-framework)

Description
-----------

[](#description)

Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options. For more information please visit [Select2](https://select2.github.io/)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

To install, either run

```
$ php composer.phar require conquer/select2 "*"

```

or add

```
"conquer/select2": "*"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

Basic usage:

```
// Form edit view
use conquer\select2\Select2Widget;
use yii\helpers\ArrayHelper;

$form->field($model, 'attribute')->widget(
    Select2Widget::className(),
    [
        'items'=>ArrayHelper::map(Catalog::find()->all(), 'id', 'name')
    ]
);
```

Ajax:

```
use conquer\select2\Select2Action;
...

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'ajax' => [
                'class' => Select2Action::className(),
                'dataCallback' => [$this, 'dataCallback'],
            ],
        ];
    }
    /**
     *
     * @param string $q
     * @return array
     */
    public function dataCallback($q)
    {
        $query = new ActiveQuery(Catalog::className());
        return [
            'results' =>  $query->select([
                    'catalog_id as id',
                    'catalog_name as text',
                ])
                ->filterWhere(['like', 'catalog_name', $q])
                ->asArray()
                ->limit(20)
                ->all(),
        ];
    }
}

// Form edit view:

$form->field($model, 'attribute')->widget(
    Select2Widget::className(),
    [
        'ajax' => ['site/ajax']
    ]
);
```

Jquery Events:

Array the Select2 JQuery events. You must define events in event-name =&gt; event-function format. All events will be stacked in the sequence. Refer the [plugin options documentation ](https://select2.github.io/options.html) for details.

For example:

```
$form->field($model, 'attribute')->widget(
    Select2Widget::className(),
    [
        'events' => [
            'select2:open' => "function() { log('open'); }",
        ]
    ]
);
```

Initialization of multiple selection in case of using ajax and custom templates.

```
