Menus

Showing posts with label Yii2 dropdown multiple selected values. Show all posts
Showing posts with label Yii2 dropdown multiple selected values. Show all posts

Saturday, 13 January 2018

Yii2 dropdown multiple selected



MultiSelect Widget for Yii2

It supports searching, remote data sets, and infinite scrolling of results.

https://github.com/kartik-v/yii2-widget-select2

Kartik Multiselect widget


in _form.php

use kartik\select2\Select2;



<?php
    echo Select2::widget([
        'model' => $model,
        'name' => 'certificates',
        'attribute' => 'certificates',
        'data' => ArrayHelper::map(Mulcertificates::find()->orderBy('description')->all(),'id','description'),  //['1'=>'1','2'=>2],
        'options' => [
            'placeholder' => 'Select certificates ...',
            'multiple' => true
    ],
    ]);
    ?>


Controller.php

public function actionCreate()
    {
        $model = new Mulselect();

        if ($model->load(Yii::$app->request->post())) {
         
            $model->certificates = implode(",",$model->certificates);
            
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
         
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }


 public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $model->certificates= explode(',', $model->certificates);
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }


Links

Select2 Widget