1

this is the code for a button, i want to change the label or in other words the title tag content.

        'assignParent' => array(
            'label' => 'Assign Parent',
            'url' => '$data->parentId ? Yii::app()->controller->createUrl("updateParent", array("id" => $data->parentId)): Yii::app()->controller->createUrl("assignParent", array("imei" => $data->imei))',
            'imageUrl' => Yii::app()->baseUrl . '/media/images/parent-btn.png',
            'visible' => 'Yii::app()->user->checkAccess("oDeviceDeviceAssignParent") ? true : false',
            'options' => array('style' => 'padding: 0px 3%'),
        ),

this is the view source code,

<td class="button-column"><a href="/qelasysecurity_12/index.php/device/device/view/id/18" rel="tooltip" title="View" style="padding: 0px 3%"><i class="icon-eye-open"></i></a><a href="/qelasysecurity_12/index.php/device/device/update/id/18" rel="tooltip" title="Update" style="padding: 0px 3%"><i class="icon-pencil"></i></a><a href="/qelasysecurity_12/index.php/device/device/delete/id/18" rel="tooltip" title="Delete" class="delete" style="padding: 0px 3%"><i class="icon-trash"></i></a><a href="/qelasysecurity_12/index.php/device/device/updateParent/id/36" rel="tooltip" title="Assign Parent" style="padding: 0px 3%"><img alt="Assign Parent" src="/qelasysecurity_12/media/images/parent-btn.png"></a><a href="#" rel="tooltip" title="$data-&gt;parentId ? Assign Student : Update Student" style="padding: 0px 3%"><img alt="Assign Student" src="/qelasysecurity_12/media/images/student-btn.png"></a></td>

this is how that exact part comes,

<a href="#" rel="tooltip" title="$data-&gt;parentId ? Assign Student : Update Student" style="padding: 0px 3%">

i want to make this kind of a logic to change the label name,

'options' => array('style' => 'padding: 0px 3%', 'title'=>'$data->parentId ? Assign Student : Update Student'),

$data->parentId ? Assign Student : Update Student'

any suggestion to accomplish this?

3 Answers3

0

i think you cant do it directly but you can do it using property cssClassExpression

In it you can write a valid PHP expression that will be calculated. Make two css classes one with content "assign student" and other with "update Student". You can use it like,
1. Write a function in your model named checkStudent like

public function checkStudent()
{  
   if($this->id)
      {
return "class name"
}
else
{
return "class name"}

} 


2.Now you can use it like

"cssClassExpression"=>'$data->checkStudent()'


Now question arises that how can u right content using Css class. Here is good answer to that question.

Community
  • 1
  • 1
Rafay Zia Mir
  • 2,116
  • 6
  • 23
  • 49
  • i actually want to have a way to insert the content in to html title tag of an element. :before or :after want work in my case. can you provide a proper example pls if u dnt mind. –  Aug 01 '14 at 03:47
  • this does not work, in my case i need to replace the content of title tag not to put a content in front of after an element. –  Aug 01 '14 at 04:15
0

Here is the solution. With help from http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in-the-options-of-a-button-i-e-evaluate-options-attribute/

in /protected/components create a new extension "ButtonColumn.php"

<?php
/**
 * ButtonColumn class file.
 * Extends {@link CButtonColumn}
 */

class ButtonColumn extends CButtonColumn
{


    /**
     * Renders a link button.
     * @param string $id the ID of the button
     * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
     * See {@link buttons} for more details.
     * @param integer $row the row number (zero-based)
     * @param mixed $data the data object associated with the row
     */
    protected function renderButton($id,$button,$row,$data)
    {
            if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
                    return;
            $label=isset($button['label']) ? $button['label'] : $id;
            $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
            $options=isset($button['options']) ? $button['options'] : array();
            if(!isset($options['title']))
                    $options['title']=$label;

            // Start of modification
            if( isset ( $button['evaluateLabel'] ) ) 
            {
                    $label = $this->evaluateExpression($label,array('data'=>$data,'row'=>$row));
                    $label = $button['evaluateLabel'][$label];
                    unset($options['evaluateLabel']);
            }
            // END of modifications

            if(isset($button['imageUrl']) && is_string($button['imageUrl']))
                    echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);
            else
                    echo CHtml::link($label,$url,$options);
    }
}

Note the modifications line. This checks for a new variable 'evaluateLabel'. If it exits it will parse the 'label' variable through the as an array key to find the desired new value.

My new CGridView code looks as follows

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'users-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'username',
        'email',
...
        array(
            'class' => 'ButtonColumn',
            'template' => '{update} {delete} {switch}',
            'buttons'=>array (
                'delete'=>array(
                    'label'=>'$data->custom_variable',
                    'imageUrl'=>false,
                    'options'=>array( 'class'=>'cbutton delete' ),
                    'evaluateLabel' => array(0=>"Suspend",1=>"Reactivate"),
                ),

So in my case '$data->custom_variable is a variable tied to the model that will always be 0 or 1. So the text loaded will be as paired in the evaluateLabel key.

Also note the class has changed from CButtonColumn to ButtonColumn

user36388
  • 188
  • 1
  • 2
  • 9
0

You can use my code

 'link'=>array(
        'header'=>Yii::t('main', 'login'),
        'type'=>'raw',
        'value'=> 'CHtml::button($data->islogin==1?"可":"不可",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("user/changelogin",array("id"=>$data->id))."\'", "class"=>"btn-link"))',
    ),