ActionColumn继承Column
column源码请点击: http://blog.csdn.net/lmjy102/article/details/55210389
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\grid;
use Yii;
use Closure;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
* ActionColumn是[[GridView]]一列小部件,显示按钮用于查看和操作的项目。
*
* To add an ActionColumn to the gridview, add it to the [[GridView::columns|columns]] configuration as follows:
* 去增加动作列 配置如下:
*
* ```php
* 'columns' => [
* // ...
* [
* 'class' => ActionColumn::className(),
* // you may configure additional properties here
* ],
* ]
* ```
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActionColumn extends Column
{
/**
* @var string the ID of the controller that should handle the actions specified here.
* @var string 控制器处理ID指定的动作
* If not set, it will use the currently active controller. This property is mainly used by
* 如果没有设置, 它将使用当前活动控制器, 这个属性主要使用的
* [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
* [[urlCreator]]创建url不同的动作。这个属性的值将前缀
* to each action name to form the route of the action.
* 每个动作名称形成的路由动作。
*/
public $controller;
/**
* @var string the template used for composing each cell in the action column.
* @var string 模板用于写操作列中的每个单元格。
* Tokens enclosed within curly brackets are treated as controller action IDs (also called *button names*
* in the context of action column). They will be replaced by the corresponding button rendering callbacks
* specified in [[buttons]]. For example, the token `{view}` will be replaced by the result of
* the callback `buttons['view']`. If a callback cannot be found, the token will be replaced with an empty string.
*
* 令牌在花括号被当作控制器动作的id(也称为*按钮名称*的动作列)。
* 他们将被相应的按钮渲染[[button]]中指定的回调。
* 例如,令牌“{view}”将取代了回调的结果”按钮(“view”)”。
* 如果不能找到一个回调,令牌将被替换为一个空字符串。
*
* As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as follows:
* 作为一个例子,只有视图,并更新按钮可以添加ActionColumn显示数据表格列如下:
*
* ```
* ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],
* ```
*
* @see buttons
*/
public $template = '{view} {update} {delete}';
/**
* @var array button rendering callbacks. The array keys are the button names (without curly brackets),
* and the values are the corresponding button rendering callbacks. The callbacks should use the following
* signature:
*@var array button渲染回调,数组键button名称(没有花括号),
* 和值对应的按钮渲染回调。应该使用以下回调
* 签名:
* ```php
* function ($url, $model, $key) {
* // return the button HTML code
* }
* ```
*
* where `$url` is the URL that the column creates for the button, `$model` is the model object
* $url在哪个url列创建按钮,$model是一个对象
* being rendered for the current row, and `$key` is the key of the model in the data provider array.
* 被渲染为当前行,和$key是模型的关键数据提供程序的数组
*
* You can add further conditions to the button, for example only display it, when the model is
* 您可以添加进一步条件按钮,,例如只显示它
* editable (here assuming you have a status field that indicates that):
* 当模型是可编辑(这里假设您有一个状态字段说明):
*
* ```php
* [
* 'update' => function ($url, $model, $key) {
* return $model->status === 'editable' ? Html::a('Update', $url) : '';
* };
* ],
* ```
*/
public $buttons = [];
/**
* @var callable a callback that creates a button URL using the specified model information.
* @var callable 一个回调URL使用指定的模型信息创建一个按钮。
* The signature of the callback should be the same as that of [[createUrl()]].
* 回调的签名应该一样[[createUrl()]]。
* If this property is not set, button URLs will be created using [[createUrl()]].
* 如果不设置这个属性,按钮将创建url使用[[createUrl()]]。
*/
public $urlCreator;
/**
* @var array html options to be applied to the [[initDefaultButtons()|default buttons]].
* @var array html选项适用于[[initDefaultButtons()|默认按钮]]。
* @since 2.0.4
*/
public $buttonOptions = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initDefaultButtons();
}
/**
* Initializes the default button rendering callbacks.
*/
protected function initDefaultButtons()
{
//设置view按钮
if (!isset($this->buttons['view'])) {
$this->buttons['view'] = function ($url, $model, $key) {
$options = array_merge([
'title' => Yii::t('yii', 'View'),
'aria-label' => Yii::t('yii', 'View'),
'data-pjax' => '0',
], $this->buttonOptions);
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
};
}
//设置更新按钮
if (!isset($this->buttons['update'])) {
$this->buttons['update'] = function ($url, $model, $key) {
$options = array_merge([
'title' => Yii::t('yii', 'Update'),
'aria-label' => Yii::t('yii', 'Update'),
'data-pjax' => '0',
], $this->buttonOptions);
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
};
}
//设置删除按钮
if (!isset($this->buttons['delete'])) {
$this->buttons['delete'] = function ($url, $model, $key) {
$options = array_merge([
'title' => Yii::t('yii', 'Delete'),
'aria-label' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'data-pjax' => '0',
], $this->buttonOptions);
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
};
}
}
/**
* Creates a URL for the given action and model.
* 创建一个URL模式为model中的action
* This method is called for each button and each row.
* 调用此方法对于每一个按钮,每一行。
* @param string $action the button name (or action ID)
* @param \yii\db\ActiveRecord $model the data model
* @param mixed $key the key associated with the data model
* 与数据模型相关联的关键
* @param integer $index the current row index
* @return string the created URL
*/
public function createUrl($action, $model, $key, $index)
{
if ($this->urlCreator instanceof Closure) {
return call_user_func($this->urlCreator, $action, $model, $key, $index);
} else {
$params = is_array($key) ? $key : ['id' => (string) $key];
$params[0] = $this->controller ? $this->controller . '/' . $action : $action;
return Url::toRoute($params);
}
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
$name = $matches[1];
if (isset($this->buttons[$name])) {
$url = $this->createUrl($name, $model, $key, $index);
return call_user_func($this->buttons[$name], $url, $model, $key);
} else {
return '';
}
}, $this->template);
}
}