totocsa/yii2-multiselect

Multi select for grid view with Bootstrap 4

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2020-10-18 22:08 UTC

This package is auto-updated.

Last update: 2024-04-19 05:34:42 UTC


README

Multiselect for grid view with Bootstrap 4

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist totocsa/yii2-multiselect "dev-master"

or add

"totocsa/yii2-multiselect": "dev-master"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

In the grid view:

[
    'attribute' => 'statusid',
    'value' => function($model, $key, $index, $column) {
        /* @var $model app\models\Human */
        return $model->status->name;
    },
    'filter' => MultiSelect::widget([
        'model' => $searchModel,
        'attribute' => 'statusid',
        'items' => ArrayHelper::map(Status::find()
                        ->orderBy(['name' => SORT_ASC])
                        ->all(), 'id', 'name'),
    ]),
],

In the Search file:

public function init() {
    parent::init();

    $this->statusid = ArrayHelper::getColumn(Status::find()
                            ->orderBy(['name' => SORT_ASC])
                            ->all(), 'id');
}

public function rules() {
    return [
    ...
        [['statusid', 'vehicleid', 'operatingsystemid'], 'each', 'rule' => ['integer']],
    ...
    ];
}

public function search($params) {
...
    if (is_array($this->statusid)) {
        if (count($this->statusid) > 0) {
            $query->andFilterWhere(['in', 'human.statusid', $this->statusid]);
        } else {
            $query->andWhere('false');
        }
    } else {
        $query->andWhere('false');
    }
....
}