Status Image For Cdetailview

Hi All,

At this article http://www.yiiframework.com/wiki/535/display-status-image-on-cgridview-column/

I found how to show status image dependently on status value. For gridview it fine.

But I tried to make similar for CDetailView and it shows code as it is, but not image. Can I make it for CDetailView ?

Thanks!

Did you set the type to ‘raw’?




 'attributes'=>array(

         ...

        array(              

            'label'=>'Status',

            'type'=>'raw',

            'value'=>... your image tag ...,

        ),

    ),




Yes,


	array(

		'name'=>'status',

		'filter'=>array('1'=>'Active', '0'=>'status'),

		'type'=>'raw',

		'value'=>'CHtml::tag("div", array(

            "style"=>"text-align: center",

        ),

        CHtml::tag("img", array(

            "title"=>Appfuncs::getImagetitle(GxHtml::valueEx($data, "status")),

            "src"=>Appfuncs::getStatusImage(GxHtml::valueEx($data, "status")),

        ))

    )',

		'htmlOptions'=>array('width'=>"80px"),

	)



CDetailView does not evaluate a string expression like CGridView.

But you can use a closure (PHP > 5.3)




 array(

                'header'=>'Status',

                'value'=> function($data){

                    CHtml::tag("div", array(

            "style"=>"text-align: center",

        ),

        CHtml::tag("img", array(

            "title"=>Appfuncs::getImagetitle(GxHtml::valueEx($data, "status")),

            "src"=>Appfuncs::getStatusImage(GxHtml::valueEx($data, "status")),

        ))

    )

                },

                'type'=>'raw',

            ),




EDIT: missing return in closure function($data){ return …}

In my opinion this is not a good wiki tutorial.

It will be more clear and simple to add a method ‘getStatusImage()’ that returns the wrapped img tag to your model than to include Appfuncs.

Add a function similar to below to your model:





public function getStatusImage() 

{

   $title = $this->status == 1 ? 'Active' : 'Inactive';

   $src = $this->status == 1 ? '/images/checked.png' : '/images/unchecked.png';

   $img = CHtml::image(Yii::app()->request->baseUrl . $src,$title,array('title'=>$title)); 

   return CHtml::tag('div',array(),$img); 

}








 array(

                'header'=>'Status',

                'value'=> function($data){return $data->getStatusImage();},

                'type'=>'raw',

            ),




And in CGridView you can use the same code / closure for the value.

Joblo,

In this way I see html code in CDetailView cell anyway,

code looks a bit different :


	array(

		'label' => 'Status',

		'type'=>'raw',

		'value'=> function($data) {

			return Appfuncs::getStatusImage( $data->status );

		},

	)



But anyway Appfuncs::getStatusImage returns image path on status value.

Seems to be impossible: type = ‘raw’ will lead to a ‘echo’ of the (unchanged) value.

What’s about:





array(

                'label' => 'Status',

                'type'=>'raw',

                'value'=> '<h1>Hello World</h1>';

        )



Thanks Joblo,

I wrote :


		 'value'=> function($data) {

			return '<img src="'.Appfuncs::getStatusImage( $data->status ) .'" >';

		},



and it works!