2016-11-24 33 views
1

我想在Yii的1更改默認的按鈕名稱網格視圖在YII2如何使用buttonOptions在GridView控件上Yii2

,我們有這樣的:

http://www.yiiframew...s-in-cgridview/

array 
(
    'class'=>'CButtonColumn', 
    'template'=>'{email}{down}{delete}', 
    'buttons'=>array 
    (
     'email' => array 
     (
      'label'=>'Send an e-mail to this user', 
      'imageUrl'=>Yii::app()->request->baseUrl.'/images/email.png', 
      'url'=>'Yii::app()->createUrl("users/email", array("id"=>$data->id))', 
     ), 
     'down' => array 
     (
      'label'=>'[-]', 
      'url'=>'"#"', 
      'visible'=>'$data->score > 0', 
      'click'=>'function(){alert("Going down!");}', 
     ), 
    ), 
), 

我會像Yii2那樣的東西

現在我只想改變標籤。

閱讀Yii2的文件我試過了:

[ 
    'class' => 'yii\grid\ActionColumn', 
    'buttonOptions' => [ 
     [ 
      'name' => 'update', 
      'additionalOptions' => [ 
       'label' => 'Super Update', 
      ] 
     ], 
     [ 
      'name' => 'delete', 
      'additionalOptions' => [ 
       'label' => 'Super Delete', 
      ] 
     ], 
    ], 
], 

但它不工作。

我知道我可以從頭開始重新使用該按鈕:

'buttons' => [ 
    'update' => function ($url, $model) { 
      $t = 'index.php?r=site/update&id='.$model->id; 
      return Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs']); 
    }, 
], 

但我並不想這樣做。

感謝

回答

2

buttonOptions將應用於所有默認的按鈕,你不能把它們分開,但它可以應用常規選項(所有的按鈕):如果你想使用

'class' => 'yii\grid\ActionColumn', 
'buttonOptions' => [ 
    'title' => 'This is custom title for default 3 buttons', 
], 

自定義HTML選項,你就必須創建新類,擴展ActionColumn和覆蓋(2)保護的方法,例如:

<?php 

namespace app\models; 

use yii\grid\ActionColumn; 
use yii\helpers\Html; 
use Yii; 

class customActionColumn extends ActionColumn 
{ 
    /** 
    * Initializes the default button rendering callbacks. 
    */ 
    protected function initDefaultButtons() 
    { 
     $this->initDefaultButton('view', 'eye-open', [ 
      'title' => 'Super View', 
     ]); 
     $this->initDefaultButton('update', 'pencil', [ 
      'title' => 'Super Update', 
     ]); 
     $this->initDefaultButton('delete', 'trash', [ 
      'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 
      'data-method' => 'post', 
      'title' => 'Super Delete' 
     ]); 
    } 

    /** 
    * Initializes the default button rendering callback for single button 
    * @param string $name Button name as it's written in template 
    * @param string $iconName The part of Bootstrap glyphicon class that makes it unique 
    * @param array $additionalOptions Array of additional options 
    * @since 2.0.11 
    */ 
    protected function initDefaultButton($name, $iconName, $additionalOptions = []) 
    { 
     if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) { 
      $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) { 
       $title = Yii::t('yii', ucfirst($name)); 
       $options = array_merge([ 
        'title' => $title, 
        'aria-label' => $title, 
        'data-pjax' => '0', 
        'title' => 'atata' 
       ], $additionalOptions, $this->buttonOptions); 
       $icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]); 
       return Html::a($icon, $url, $options); 
      }; 
     } 
    } 
} 

無w在GridView中,您只需指定自定義類即可。

[ 
    'class' => app\models\customActionColumn::className(), 
], 
+1

你是對的!謝謝 – ricardo

+0

我很高興幫助你! :) –