Search code examples
phpyiicgridviewcbuttoncolumn

How can I change cbuttoncolumn label dynamically in yii cgridview


I am trying to change cbuttoncolumn label dyanamically. But somehow it does not work. My code is

array(
      'class'=>'CButtonColumn',
      'template'=>'{publish}',
      'buttons'=>array(
          'publish'=>array(
             //'type'=>'raw',
             'label'=>'$data->content_type == 1 ? "View & Publish" : "Publish"',
             'icon'=>'ok',
             'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
           ),
       ),
),

How can i do this??


Solution

  • You can just create a new column with custom links, something like this:

    In your model :

    public function getMyValue(){
        $linkOne = CHtml::link("$this->labelOne", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
        $linkTwo = CHtml::link("$this->labelTwo", Yii::app()->createUrl("model/action",array("id"=>$this->id)));
        return $linkOne.' '.$linkTwo;
    }
    

    And in your CGridView :

    'columns'=>array(
            'labelOne',
            'labelTwo',
            array(
                'type' => 'raw',
                'header' => 'Manage',
                'value' => '$data->getMyValue()',
                ),
    
        ),
    

    Or, you can use the visible attribute in CButtonColumn:

    array(
          'class'=>'CButtonColumn',
          'template'=>'{publish}{viewPublish}',
          'buttons'=>array(
              'publish'=>array(
                 //'type'=>'raw',
                 'label'=>'Publish',
                 'visible' => '$data->content_type != "1"',
                 'icon'=>'ok',
                 'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
               ),
              'viewPublish'=>array(
                 //'type'=>'raw',
                 'label'=>'View & Publish',
                 'visible' => '$data->content_type == "1"',
                 'icon'=>'ok',
                 'url'=>'Yii::app()->createUrl("/admin/contentToPublish/publish")',
               ),
           ),
    ),
    

    Hope that helps :)