yii2-select2 Select2 widget for Yii2 framework

  1. Description
  2. Installation
  3. Usage
  4. License
  5. Resources

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

Installation

The preferred way to install this extension is through composer.

To install, either run

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

or add

"conquer/select2": "*"

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

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']
    ]
);

License

conquer/select2 is released under the MIT License. See the bundled LICENSE for details.

Resources

GitHub

2 0
1 follower
0 downloads
Yii Version: 2.0
License: MIT
Category: User Interface
Developed by: Andrey Borodulin
Created on: Jul 8, 2015
Last updated: 3 years ago