kartik-v / yii2-grid

Enhanced GridView with various utilities for Yii Framework 2.0
http://demos.krajee.com/grid
Other
557 stars 303 forks source link

the value from GridView is not displayed in Select2 kartik\grid\EditableColumn #1041

Closed ma6e4kaa closed 2 years ago

ma6e4kaa commented 2 years ago

I want to use multiple select2, it works actually, but only after choosing the value from the dropdown list.

If I set multiple false, it works: image image

But if I use multiple, at first it will be like this: image image

How should I submit this value for input with json_decode?

Here's my code GridView:

        <div class="box-body">

        <?php
            echo GridView::widget([
                'dataProvider' => $provider,
                'filterModel' => $searchModel,
                'pjax' => true,
                'options' => ['style' => 'font-size:12px;'],
                'columns' => [
                    ['class' => 'kartik\grid\EditableColumn',
                    'attribute' => 'code',
                    'width' => '200px',
                        'editableOptions'=>[
                            'asPopover' => false,
                        ],
                    ],
                    ['class' => 'kartik\grid\EditableColumn',
                    'attribute' => 'oks',
                        'editableOptions'=>[
                            'asPopover' => false,
                        ],
                    ],
                    ['class' => 'kartik\grid\EditableColumn',
                    'attribute' => 'contractor_id',
                    'editableOptions'=>[
                            'inputType'=> Editable::INPUT_SELECT2,
                            'options' => [
                                'data'=> ArrayHelper::map($contractor_id, 'id', 'contractor'),
                                'options' => [
                                    'multiple' => true,
                                ],
                            ],
                            'asPopover' => false,
                        ],
                    ],
                ],
                ]);
        ?>
</div>

Controller:

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

    $oks = SafOkss::find()->where(['project_id' => $id])->orderBy(['code' => SORT_ASC]);
    $modelsOks=$oks->all();

    $searchModel = new SafOkssSearch();
    $provider = $searchModel->searchInProject($id, Yii::$app->request->queryParams);

    if (Yii::$app->request->post('hasEditable')) {
        $oks_id = Yii::$app->request->post('editableKey');
        $oks = SafOkss::find()->where(['id'=>$oks_id])->one();

        $out = Json::encode(['output'=>'','message'=>'']);
        $post = [];
        $posted = current($_POST['SafOkss']);
        $post['SafOkss'] = $posted;
        if ($oks->load($post)){
            $oks->save();
            $output = '';
            if (isset($posted['oks'])) {
                $output = $posted['oks'];
            } elseif (isset($posted['oks'])) {
                $output = $posted['code'];
            }
            $out = Json::encode(['output'=>$output,'message'=>'']);
        }
        return $out;
    }

    return $this->render('view', [
        'model' => $model,
        'oks' => $modelsOks,
        'provider' => $provider,
        'searchModel' => $searchModel,
    ]);
}
ma6e4kaa commented 2 years ago

I have found the solution:

It should be written in $beforeRow like this:

            echo GridView::widget([
            'dataProvider' => $provider,
            'filterModel' => $searchModel,
            'beforeRow' => function($model) {
            $model->contractor_id = json_decode($model->contractor_id,TRUE);}
            'pjax' => true,
            'options' => ['style' => 'font-size:12px;'],
            'columns' => [
                ['class' => 'kartik\grid\EditableColumn',
                'attribute' => 'code',
                'width' => '200px',
                    'editableOptions'=>[
                        'asPopover' => false,
                    ],
                ],
                ['class' => 'kartik\grid\EditableColumn',
                'attribute' => 'oks',
                    'editableOptions'=>[
                        'asPopover' => false,
                    ],
                ],
                ['class' => 'kartik\grid\EditableColumn',
                'attribute' => 'contractor_id',
                'editableOptions'=>[
                        'inputType'=> Editable::INPUT_SELECT2,
                        'options' => [
                            'data'=> ArrayHelper::map($contractor_id, 'id', 'contractor'),
                            'options' => [
                                'multiple' => true,
                            ],
                        ],
                        'asPopover' => false,
                    ],
                ],
            ],
            ]);