簡體   English   中英

yii cgridview按鈕標簽或標題更改

[英]yii cgridview button label or title change

這是按鈕的代碼,我想更改標簽或換句話說,標題標簽的內容。

        '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%'),
        ),

這是查看源代碼,

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

這就是確切的部分,

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

我想通過這種邏輯來更改標簽名稱,

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

$ data-> parentId? 分配學生:更新學生

有什么建議可以做到這一點?

我認為你不能直接做,但是你可以使用屬性cssClassExpression來做

在其中,您可以編寫將要計算的有效PHP表達式。 制作兩個css類,一類的內容為“分配學生”,另一類的內容為“更新學生”。 您可以像這樣使用它
1.在模型中編寫一個名為checkStudent的函數,例如

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

} 


2.現在您可以像使用它了

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


現在出現了一個問題,即如何使用Css類來糾正內容。 是該問題的好答案。

這是解決方案。 http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in-the-options-of-a-button-ie-evaluate-options的幫助下-屬性/

在/ protected / components中創建一個新的擴展名“ 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);
    }
}

注意修改行。 這將檢查新變量“ evaluateLabel”。 如果退出,它將通過作為數組鍵解析“ label”變量以找到所需的新值。

我的新CGridView代碼如下所示

$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"),
                ),

因此,在我的案例中,'$ data-> custom_variable是與模型綁定的變量,始終為0或1。因此,加載的文本將與對valuateLabel鍵中的配對相同。

還要注意,該類已從CButtonColumn更改為ButtonColumn

你可以用我的代碼

 '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"))',
    ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM