1

I want to show some rows in yii2 gridview. one of column's type is BIT(correspondingAuthorFlag) in mysql table. now I want to display checkbox(checked for 1, unchecked for 0) instead of thier values. for this reason I wrote follow codes:

  $widget = Yii::createObject([
                'class' => 'yii\grid\GridView',
                'dataProvider' => $dataprovider,
                'columns' => [
                    'PersonName',
                    'PersonType',
                    'order',
                    [
                     'value' => 'correspondingAuthorFlag',
                     'class' => 'yii\grid\CheckboxColumn',
                    ],
                ],

            ]
        );

but it throws an error. how can I do it?

2
  • 1
    show the error message Commented Aug 19, 2018 at 14:37
  • What do you want to do with these checkboxes? Should they be editable? Are you want to use these values for form submitting? Commented Aug 19, 2018 at 19:01

2 Answers 2

2

You can use checkboxOptions :

echo GridView::widget([
    'dataProvider' => $dataprovider,
    'columns' => [
        'PersonName',
        'PersonType',
        'order',
        [
            'class' => 'yii\grid\CheckboxColumn',
            'checkboxOptions' => function($model) {
                return ['checked' => $model->correspondingAuthorFlag == 1 ? true : false];
            }
        ],
    ],
]);
Sign up to request clarification or add additional context in comments.

2 Comments

why not return ['checked' => $model->correspondingAuthorFlag == 1];
@kusanagi It works fine that way, I added the full statement for more readability
0

I've found it and done by these codes:

     $widget = Yii::createObject([
                'class' => 'yii\grid\GridView',
                'dataProvider' => $dataprovider,
                'columns' => [
                    'PersonName',
                    'PersonType',
                    'order',

                    ['attribute' => 'correspondingAuthorFlag',
                        'value' => function ($data) {
                            if ($data['correspondingAuthorFlag'] == '1') {
                              return Html::checkbox('correspondingAuthorFlag',1,['disabled' => true]);
                            } else {
                                return Html::checkbox('correspondingAuthorFlag',0,['disabled' => true]);

                            }

                        }
                        , 'format' => 'raw'
                    ]

                ],

            ]
        );

using this way, we can add any html control to gridview. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.