Gridview customize cell values

Hello,

I have a Gridview where the number of columns is dynamic, so I do not know the column names ahead of time. My code to display the Gridview looks like this:

echo GridView::widget([
            'dataProvider' => $provider,
            'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => 'X'],
            'options' => [
                'class' => 'YourCustomTableClass',
             ],
            'columns' => $columns,
        ]);

My column values will either be a value of ‘YES’, or null (not set)… If the value is ‘YES’, I want to format the value to be a html check mark, or maybe color that cell a different color.

But how could I do that without specifying out all the columns? Normally I would list out all the columns and do an anonymous function to return a custom value. Would I use some type of anonymous function for ‘columns’ => $columns ?

My output looks like this, its essentially a matrix:

Have you tried to put the function call inside $columns? Not sure if that will work but may be worth a try?

Thanks snathan,

I have confirmed that technique does indeed work!

@bmecler, I was playing around with Gridview today and realized it might have been easier to use the visibility option:
'visible'=>true/false
It’s a much simpler way to control. From the way your question is framed this may not work as it appears the columns are truly dynamic (not just turning them on/off). Anyway, wanted to put it out there.

@snathan Yes, the columns were dynamic and couldn’t be determine ahead of time. Your first response was dead on. I suspected that was going to be the approach, but was too lazy to test it until someone else suggested it :slight_smile: