0

Хочу в GridView отобразить свои значения из цикла

<?php
$model = new Pers();
$result = $model->inputAtr();
$column = [
    ['class' => 'yii\grid\SerialColumn'],
    'id',
    'name:ntext',
    'info:ntext',
    'img:ntext',
    'lvl',
];

for($i = 0; $i < count($result); $i++)
{
    echo $res = $model->ret($result[$i]['id']);
    $myparam = [
        'attribute' => $result[$i]['name'],
        'format' => 'raw',
        'value' => function($model, $res)
          {
          return $model->ret($res);
          }, 
    ];
    $column[] = $myparam;
}

$column[] = ['class' => 'yii\grid\ActionColumn'];
?>
<?=
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $column,
]);
?>

где

public function ret($id)
{ 
        return  $id; 
}

нормально подставляются имена столбцов, а вот значения внутрь 'value' => function($model, $res) не проходят

1 ответ 1

1

использовать use

for($i = 0; $i < count($result); $i++)
{
    echo $res = $result[$i]['id'];
    $myparam = [
        'attribute' => $result[$i]['name'],
        'format' => 'raw',
        'value' => function ($model) use ($res)
          {
            return $model->ret($res);
          }, 
    ];
    $column[] = $myparam;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.