Here’s a handy tip on how to pass variables to the custom code in Gridview for raw formatted rows. I’m embarrassed that I’d never seen this before. I’d never the used the syntax and never previously been able to find out how to do this.

The key part is “function ($model) use ($custom_variable) {}” where $custom_variable is any variable name or names that you choose. I believe it should work with a series of custom variables in commas separated form but I haven’t tested that part.

GridView::widget([
‘dataProvider’ => $dataProvider,
‘filterModel’ => $filterModel,
‘columns’ => [
[
‘attribute’ => ‘fullname’,
‘format’ => ‘raw’,
‘contentOptions’ => [‘class’ => ‘text-wrap’],
‘contentOptions’ => [‘style’ => ‘max-width:180px;overflow: auto;’],
‘value’ => function ($model) use ($custom_variable) {
if ($custom_variable ==’review’) {
$url = ‘candidate/approvenew’;
} else if ($custom_variable ==’update’) {
$url = ‘candidate/approveupdate’;
} else {
$url = ‘candidate/update’;
}
return ‘<div>’.Html::a($model->fullname,[$url,’id’=>$model->id]).'</div>’;
},
],

This apparently is known as a closure.

Support this site, share this page:Tweet about this on Twitter

Twitter

Share on Reddit

Reddit

Share on LinkedIn

Linkedin

Share on Facebook

Facebook

Email this to someone

email