Gridview Button Class Function

In my index GridView I’m trying to set the class of a button which will have a number of option based on the model. As such, I was trying to make it based on a function, but am doing something wrong as Yii reports back

Undefined variable: model

Here is the gridview code in question

       [
        	'label'=>'Invoice',
        	'content' => function($model, $key) {
        		$content = Html::button('<i class="glyphicon glyphicon-share"></i>', 
        		[
        			'id'=>'invoice-'.$model->ProjId,
        			'class' =>  function($model){
        				if(!empty($model->InvoiceId)){
        					return 'btn btn-xs done';
        				}else{
        					if($model->status->Status == 'Cancelled Order'){
        						return 'btn btn-xs cancelled';
        					}else{
        						return 'btn btn-xs notdone';
        					}
        				}
        			},
        			'title '=> Yii::t('app', 'Generate the Invoice for project').' '.$model->ProjNo,
        //...

what I don’t get is the $model is recognized in the line above, I’m passing it in the function, yet it reports undefined?
What’s the proper way to do this? Please keep in mind I still have 3 more conditions to add to the function that is why I did use a standard php ternary operator.

Thank you.

I don’t know if it’s possible to use a closure here, but you may try:

'class' => function() use ($model) {

instead.

When it doesn’t work, then you will need to do it without a closure:

'class' => !empty($model->InvoiceId) ? 
        'btn btn-xs done' :
        ($model->status->Status == 'Cancelled Order' ? 
            'btn btn-xs cancelled' :
            'btn btn-xs notdone'),

Sadly, closure does not work, so nested ternaries it is. Ugly, but works.

One thing you could consider might be a public method in the model which returns the button class:

// view
'class' => $model->getButtonClass(),
...

// model
public function getButtonClass()
{
    if (!empty($this>InvoiceId)) {
        return 'btn btn-xs done';
    } else {
        if ($this->status->Status == 'Cancelled Order') {
            return 'btn btn-xs cancelled';
        } else {
            return 'btn btn-xs notdone';
        }
    }
}

Well, there must be an argument if it’s OK to do this kind of thing, since it looks against MVC design pattern. But personally I don’t mind too much about MVC here, since it’s a simple and effective solution.