简体   繁体   中英

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??

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM