Cgridview: Dynamically Showing Buttons Per Row?

This seems like something so common that it must have been done before but my searches have found nothing. I have a table which lists data in a CGridView and it includes a datetime field. I’ve made the controller throw an error if the user tries to edit\delete a record which has a value in that field which is before today’s date. While this is needed in case someone uses an old link (or types in their own address) to access the update page; it would be much better if they didn’t have a link on the grid which is just going to error out when clicked. I don’t want to remove the edit\delete buttons from all rows but I’d like to remove them from all rows which contain a date before today. Is this possible? If so where can I get information on doing this? Thanks for any help.

Well I assume the CButtonColumn buttons’ visible property should help. Something like this:


<?php $this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'your-grid',

    'dataProvider' => $model->search(),

    'filter' => $model, // or not…

    'columns' => array(

        …

        /* your other columns go here */

        …

        array(

            'class'=>'CButtonColumn',

            'template' => '{view} {update} {delete}',

            'buttons'=>array(

                'update' => array(

                    'visible'  => '!is_null($data->dateTimeField)', // here you may complete the expression that should be evaluated to display the button 

                ),

                'delete' => array(

                    'visible'  => '!is_null($data->dateTimeField)', // same like above 

                ),

            ),

        ),

    ),

)); ?>

Thanks a lot, that worked perfectly. Here is my final code in case it helps someone else:




'columns' => array(

// ... column definitions go here...

  array(

    'class' => 'EButtonColumnWithClearFilters', // Derived from CButtonColumn

// ... other EButtonColumnWithClearFilters definitions go here...

    'buttons'=>array(

      'update' => array('visible'=>'strtotime(date(\'Y-m-d\', strtotime($data->local_in)))>mktime(0,0,0)'),

      'delete' => array('visible'=>'strtotime(date(\'Y-m-d\', strtotime($data->local_in)))>mktime(0,0,0)'),

    ),

  ),

),



It may be helpful to know that $data->local_in is actually a function in my model which returns the time offset to the local timezone (because the DB stores in GMT only). We are first converting each datetime into a date only value because I actually wanted to compare dates and not exact times.