platx/yii2-relative-select2 Relative Select2 Widget for Yii2 Framework

extensionselect2relativekartik

Yii2 Relative Select2 Widget

Relative Select2 Widget for Yii2 Framework. It allows to obtain dynamically data via AJAX depending on the parent attribute. Widget extends and requires kartik-v Select2 Widget (https://github.com/kartik-v/yii2-widget-select2).

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist platx/yii2-relative-select2 "*"

or add

"platx/yii2-relative-select2": "*"

to the require section of your composer.json file.

Usage

Usage with ActiveForm and model

<?= \$form->field($model, 'state_1')->widget(\platx\relativeselect2\RelativeSelect2::classname(), [
     'parentName' => 'parent_attribute_name',
     'url' => Url::to(['/ajax/list-items']),
     'options' => ['placeholder' => 'Select a state ...'],
     'pluginOptions' => [
         'allowClear' => true
     ],
]) ?>

With a model and without ActiveForm

<?= \platx\relativeselect2\RelativeSelect2::widget([
     'model' => $model,
     'attribute' => 'state_2',
     'parentName' => 'parent_attribute_name',
     'url' => Url::to(['/ajax/list-items']),
     'options' => ['placeholder' => 'Select a state ...'],
     'pluginOptions' => [
         'allowClear' => true
     ],
]) ?>

Without model and implementing a multiple select

<?= \platx\relativeselect2\RelativeSelect2::widget([
     'name' => 'state_2',
     'parentName' => 'parent_attribute_name',
     'url' => Url::to(['/ajax/list-items']),
     'options' => ['placeholder' => 'Select a state ...'],
     'pluginOptions' => [
         'allowClear' => true,
         'multiple' => true
     ],
]) ?>

Controller example for data obtaining

<?php
class AjaxController extends \yii\web\Controller
{
    public function actionListItems($query = null)
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!is_null($query)) {
            $models = Model::find()
                ->andWhere(['parent_id' => $query])
                ->all();
            $items = [];
            foreach($models as $model) {
                $items[] = ['id' => $model->id, 'text' => $model->name];
            }
            if(!empty($items)) {
                $out['results'] = $items;
            }
        }
        return $out;
    }
}

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

No stable releases.

Comments



No stable releases.



MIT license
Stats
0 github stars & 1 github forks
0 downloads in the last day
0 downloads in the last 30 days
124 total downloads