2016-11-24 5 views
1

würde Ich mag die Standard-Schaltfläche Name auf YII2So verwenden buttonOptions auf Gridview auf Yii2

auf Yü 1 auf Grid-Ansicht ändern haben wir diese:

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

würde ich wie etwas für Yii2

Für jetzt möchte ich nur das Etikett ändern.

Lesen der Dokumentation für Yii2 Ich habe versucht, dass:

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

Aber es funktioniert nicht.

Ich weiß, ich kann den Knopf von Grund auf neu erstellen mit:

'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']); 
    }, 
], 

Aber ich möchte nicht, dass zu tun.

dank

Antwort

2

buttonOptions wird auf alle Standardtasten angewendet werden, können Sie sie nicht trennen kann, aber es ist möglich, allgemeine Optionen anzuwenden (für alle Tasten):

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

Wenn Sie verwenden möchten, benutzerdefinierte HTML-Optionen, werden Sie neue Klasse anlegen, erweitern ActionColumn und überschreiben (2) geschützte Methoden, zum Beispiel:

<?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); 
      }; 
     } 
    } 
} 

Nein w in GridView müssen Sie nur benutzerdefinierte Klasse angeben, und das ist alles.

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

Sie haben Recht! Danke – ricardo

+0

Ich bin froh, dass dir geholfen hat! :) –