1
votes

I have the problem that I cant get the necessary values into the checkbox value field of my gridview. The checkboxfield can be checked or unchecked. Therfore in the database there is a boolean field called member, for datagrid values with 0 is not checked and 1 is checked.

Usually the gridview has four columns. For testing purpose I print the column 'member' of the database table in an extra field in front of the checkbox column to see whether the values of the tablecolumn 'member' are right.

Because the view has two different dataProvdiders, $dataProvider and $dataProviderMembers, I cannot use the $model object. The $model object belongs to the content of $dataProvider. The dataviewgrid will be filled by the second dataprovider $dataProviderMembers.

<?= GridView::widget([
        'dataProvider' => $dataProviderMembers,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'firstname',
            'lastname',
            'member',
            ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $column){
                return ['checked' => 'member'];
            }, 'header' => 'Mitglied'
            ],
            ]
    ]); ?>

Controller methode:

$searchModel = new PeriodeClassSubjectMembersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

$modelPeriodeClassSubjectMembers = new PeriodeClassSubjectMembers();
        $dataProviderMembers = $modelPeriodeClassSubjectMembers->getPeriodeClassMembers($class_id, $periode_id, false);
        $dataProviderMembers->key = 'id'; //set this to make the id column as value columns in the gridview for javascript getselectedrows instead of the gridrow number
        return $this->render('viewmysubject', [
            'model' => $this->findModel($id),
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'dataProviderMembers' => $dataProviderMembers
        ]);

Model methode for $dataProviderMembers:

$query = new Query();
        $query->select(..........);

$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);

return $dataProvider;

With this method, the checkbox field is not filled correkt.

Normal the checkbox field will be filled by the segment

return ['checked' => $model->member};

In this case there is no $model with values for member. $model belongs to the data of $dataProvider.

But I get the values for member from $dataProviderMembers. As I can see in the column 'member', which is before the checkbox field of the datagrid, the values from the database are right filled into the grid. How can I fill the checkbox field without $model, instead with values of the 'member' column (member values of $dataProviderMembers) of the sql query in behind? Which means something like :

return ['checked' => 'member'];
1

1 Answers

1
votes

In your gridview the dataProvider is equal to $dataProviderMembers then the $model param in anonymous function for checkboxOptions is related the models that should contain the member values So You could get to value using $model->member

        [   'class' => 'yii\grid\CheckboxColumn', 
            'checkboxOptions' => function($model, $key, $index, $column){
            return ['value' => $model->member];
           }, 
        ],

or if the dataProvider is an array

      [   'class' => 'yii\grid\CheckboxColumn', 
            'checkboxOptions' => function($model, $key, $index, $column){
            return ['value' => $model['member']];
           }, 
        ],