init
This commit is contained in:
233
vendor/yiisoft/yii2/grid/ActionColumn.php
vendored
Normal file
233
vendor/yiisoft/yii2/grid/ActionColumn.php
vendored
Normal file
@@ -0,0 +1,233 @@
|
||||
<?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 yii\helpers\Html;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
|
||||
*
|
||||
* 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
|
||||
* ],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* For more details and usage information on ActionColumn, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class ActionColumn extends Column
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $headerOptions = ['class' => 'action-column'];
|
||||
/**
|
||||
* @var string the ID of the controller that should handle the actions specified here.
|
||||
* 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
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as follows:
|
||||
*
|
||||
* ```php
|
||||
* ['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:
|
||||
*
|
||||
* ```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
|
||||
* being rendered for the current row, and `$key` is the key of the model in the data provider array.
|
||||
*
|
||||
* 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 array visibility conditions for each button. The array keys are the button names (without curly brackets),
|
||||
* and the values are the boolean true/false or the anonymous function. When the button name is not specified in
|
||||
* this array it will be shown by default.
|
||||
* The callbacks must use the following signature:
|
||||
*
|
||||
* ```php
|
||||
* function ($model, $key, $index) {
|
||||
* return $model->status === 'editable';
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* Or you can pass a boolean value:
|
||||
*
|
||||
* ```php
|
||||
* [
|
||||
* 'update' => \Yii::$app->user->can('update'),
|
||||
* ],
|
||||
* ```
|
||||
* @since 2.0.7
|
||||
*/
|
||||
public $visibleButtons = [];
|
||||
/**
|
||||
* @var callable a callback that creates a button URL using the specified model information.
|
||||
* The signature of the callback should be the same as that of [[createUrl()]]
|
||||
* Since 2.0.10 it can accept additional parameter, which refers to the column instance itself:
|
||||
*
|
||||
* ```php
|
||||
* function (string $action, mixed $model, mixed $key, integer $index, ActionColumn $this) {
|
||||
* //return string;
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* If this property is not set, button URLs will be created using [[createUrl()]].
|
||||
*/
|
||||
public $urlCreator;
|
||||
/**
|
||||
* @var array html options to be applied to the [[initDefaultButton()|default button]].
|
||||
* @since 2.0.4
|
||||
*/
|
||||
public $buttonOptions = [];
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$this->initDefaultButtons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the default button rendering callbacks.
|
||||
*/
|
||||
protected function initDefaultButtons()
|
||||
{
|
||||
$this->initDefaultButton('view', 'eye-open');
|
||||
$this->initDefaultButton('update', 'pencil');
|
||||
$this->initDefaultButton('delete', 'trash', [
|
||||
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
|
||||
'data-method' => 'post',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
switch ($name) {
|
||||
case 'view':
|
||||
$title = Yii::t('yii', 'View');
|
||||
break;
|
||||
case 'update':
|
||||
$title = Yii::t('yii', 'Update');
|
||||
break;
|
||||
case 'delete':
|
||||
$title = Yii::t('yii', 'Delete');
|
||||
break;
|
||||
default:
|
||||
$title = ucfirst($name);
|
||||
}
|
||||
$options = array_merge([
|
||||
'title' => $title,
|
||||
'aria-label' => $title,
|
||||
'data-pjax' => '0',
|
||||
], $additionalOptions, $this->buttonOptions);
|
||||
$icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
|
||||
return Html::a($icon, $url, $options);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a URL for the given action and model.
|
||||
* This method is called for each button and each row.
|
||||
* @param string $action the button name (or action ID)
|
||||
* @param \yii\db\ActiveRecordInterface $model the data model
|
||||
* @param mixed $key the key associated with the data model
|
||||
* @param int $index the current row index
|
||||
* @return string the created URL
|
||||
*/
|
||||
public function createUrl($action, $model, $key, $index)
|
||||
{
|
||||
if (is_callable($this->urlCreator)) {
|
||||
return call_user_func($this->urlCreator, $action, $model, $key, $index, $this);
|
||||
}
|
||||
|
||||
$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->visibleButtons[$name])) {
|
||||
$isVisible = $this->visibleButtons[$name] instanceof \Closure
|
||||
? call_user_func($this->visibleButtons[$name], $model, $key, $index)
|
||||
: $this->visibleButtons[$name];
|
||||
} else {
|
||||
$isVisible = true;
|
||||
}
|
||||
|
||||
if ($isVisible && isset($this->buttons[$name])) {
|
||||
$url = $this->createUrl($name, $model, $key, $index);
|
||||
return call_user_func($this->buttons[$name], $url, $model, $key);
|
||||
}
|
||||
|
||||
return '';
|
||||
}, $this->template);
|
||||
}
|
||||
}
|
||||
168
vendor/yiisoft/yii2/grid/CheckboxColumn.php
vendored
Normal file
168
vendor/yiisoft/yii2/grid/CheckboxColumn.php
vendored
Normal file
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use Closure;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Json;
|
||||
|
||||
/**
|
||||
* CheckboxColumn displays a column of checkboxes in a grid view.
|
||||
*
|
||||
* To add a CheckboxColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
|
||||
*
|
||||
* ```php
|
||||
* 'columns' => [
|
||||
* // ...
|
||||
* [
|
||||
* 'class' => 'yii\grid\CheckboxColumn',
|
||||
* // you may configure additional properties here
|
||||
* ],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* Users may click on the checkboxes to select rows of the grid. The selected rows may be
|
||||
* obtained by calling the following JavaScript code:
|
||||
*
|
||||
* ```javascript
|
||||
* var keys = $('#grid').yiiGridView('getSelectedRows');
|
||||
* // keys is an array consisting of the keys associated with the selected rows
|
||||
* ```
|
||||
*
|
||||
* For more details and usage information on CheckboxColumn, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class CheckboxColumn extends Column
|
||||
{
|
||||
/**
|
||||
* @var string the name of the input checkbox input fields. This will be appended with `[]` to ensure it is an array.
|
||||
*/
|
||||
public $name = 'selection';
|
||||
/**
|
||||
* @var array|\Closure the HTML attributes for checkboxes. This can either be an array of
|
||||
* attributes or an anonymous function ([[Closure]]) that returns such an array.
|
||||
* The signature of the function should be the following: `function ($model, $key, $index, $column)`.
|
||||
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
|
||||
* and `$column` is a reference to the [[CheckboxColumn]] object.
|
||||
* A function may be used to assign different attributes to different rows based on the data in that row.
|
||||
* Specifically if you want to set a different value for the checkbox
|
||||
* you can use this option in the following way (in this example using the `name` attribute of the model):
|
||||
*
|
||||
* ```php
|
||||
* 'checkboxOptions' => function ($model, $key, $index, $column) {
|
||||
* return ['value' => $model->name];
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $checkboxOptions = [];
|
||||
/**
|
||||
* @var bool whether it is possible to select multiple rows. Defaults to `true`.
|
||||
*/
|
||||
public $multiple = true;
|
||||
/**
|
||||
* @var string the css class that will be used to find the checkboxes.
|
||||
* @since 2.0.9
|
||||
*/
|
||||
public $cssClass;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws \yii\base\InvalidConfigException if [[name]] is not set.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
if (empty($this->name)) {
|
||||
throw new InvalidConfigException('The "name" property must be set.');
|
||||
}
|
||||
if (substr_compare($this->name, '[]', -2, 2)) {
|
||||
$this->name .= '[]';
|
||||
}
|
||||
|
||||
$this->registerClientScript();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the header cell content.
|
||||
* The default implementation simply renders [[header]].
|
||||
* This method may be overridden to customize the rendering of the header cell.
|
||||
* @return string the rendering result
|
||||
*/
|
||||
protected function renderHeaderCellContent()
|
||||
{
|
||||
if ($this->header !== null || !$this->multiple) {
|
||||
return parent::renderHeaderCellContent();
|
||||
}
|
||||
|
||||
return Html::checkbox($this->getHeaderCheckBoxName(), false, ['class' => 'select-on-check-all']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderDataCellContent($model, $key, $index)
|
||||
{
|
||||
if ($this->checkboxOptions instanceof Closure) {
|
||||
$options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
|
||||
} else {
|
||||
$options = $this->checkboxOptions;
|
||||
}
|
||||
|
||||
if (!isset($options['value'])) {
|
||||
$options['value'] = is_array($key) ? Json::encode($key) : $key;
|
||||
}
|
||||
|
||||
if ($this->cssClass !== null) {
|
||||
Html::addCssClass($options, $this->cssClass);
|
||||
}
|
||||
|
||||
return Html::checkbox($this->name, !empty($options['checked']), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns header checkbox name.
|
||||
* @return string header checkbox name
|
||||
* @since 2.0.8
|
||||
*/
|
||||
protected function getHeaderCheckBoxName()
|
||||
{
|
||||
$name = $this->name;
|
||||
if (substr_compare($name, '[]', -2, 2) === 0) {
|
||||
$name = substr($name, 0, -2);
|
||||
}
|
||||
if (substr_compare($name, ']', -1, 1) === 0) {
|
||||
$name = substr($name, 0, -1) . '_all]';
|
||||
} else {
|
||||
$name .= '_all';
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the needed JavaScript.
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public function registerClientScript()
|
||||
{
|
||||
$id = $this->grid->options['id'];
|
||||
$options = Json::encode([
|
||||
'name' => $this->name,
|
||||
'class' => $this->cssClass,
|
||||
'multiple' => $this->multiple,
|
||||
'checkAll' => $this->grid->showHeader ? $this->getHeaderCheckBoxName() : null,
|
||||
]);
|
||||
$this->grid->getView()->registerJs("jQuery('#$id').yiiGridView('setSelectionColumn', $options);");
|
||||
}
|
||||
}
|
||||
181
vendor/yiisoft/yii2/grid/Column.php
vendored
Normal file
181
vendor/yiisoft/yii2/grid/Column.php
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use Closure;
|
||||
use yii\base\BaseObject;
|
||||
use yii\helpers\Html;
|
||||
|
||||
/**
|
||||
* Column is the base class of all [[GridView]] column classes.
|
||||
*
|
||||
* For more details and usage information on Column, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Column extends BaseObject
|
||||
{
|
||||
/**
|
||||
* @var GridView the grid view object that owns this column.
|
||||
*/
|
||||
public $grid;
|
||||
/**
|
||||
* @var string the header cell content. Note that it will not be HTML-encoded.
|
||||
*/
|
||||
public $header;
|
||||
/**
|
||||
* @var string the footer cell content. Note that it will not be HTML-encoded.
|
||||
*/
|
||||
public $footer;
|
||||
/**
|
||||
* @var callable This is a callable that will be used to generate the content of each cell.
|
||||
* The signature of the function should be the following: `function ($model, $key, $index, $column)`.
|
||||
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
|
||||
* and `$column` is a reference to the [[Column]] object.
|
||||
*/
|
||||
public $content;
|
||||
/**
|
||||
* @var bool whether this column is visible. Defaults to true.
|
||||
*/
|
||||
public $visible = true;
|
||||
/**
|
||||
* @var array the HTML attributes for the column group tag.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $options = [];
|
||||
/**
|
||||
* @var array the HTML attributes for the header cell tag.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $headerOptions = [];
|
||||
/**
|
||||
* @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of
|
||||
* attributes or an anonymous function ([[Closure]]) that returns such an array.
|
||||
* The signature of the function should be the following: `function ($model, $key, $index, $column)`.
|
||||
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
|
||||
* and `$column` is a reference to the [[Column]] object.
|
||||
* A function may be used to assign different attributes to different rows based on the data in that row.
|
||||
*
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $contentOptions = [];
|
||||
/**
|
||||
* @var array the HTML attributes for the footer cell tag.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $footerOptions = [];
|
||||
/**
|
||||
* @var array the HTML attributes for the filter cell tag.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $filterOptions = [];
|
||||
|
||||
|
||||
/**
|
||||
* Renders the header cell.
|
||||
*/
|
||||
public function renderHeaderCell()
|
||||
{
|
||||
return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the footer cell.
|
||||
*/
|
||||
public function renderFooterCell()
|
||||
{
|
||||
return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a data cell.
|
||||
* @param mixed $model the data model being rendered
|
||||
* @param mixed $key the key associated with the data model
|
||||
* @param int $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
|
||||
* @return string the rendering result
|
||||
*/
|
||||
public function renderDataCell($model, $key, $index)
|
||||
{
|
||||
if ($this->contentOptions instanceof Closure) {
|
||||
$options = call_user_func($this->contentOptions, $model, $key, $index, $this);
|
||||
} else {
|
||||
$options = $this->contentOptions;
|
||||
}
|
||||
|
||||
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the filter cell.
|
||||
*/
|
||||
public function renderFilterCell()
|
||||
{
|
||||
return Html::tag('td', $this->renderFilterCellContent(), $this->filterOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the header cell content.
|
||||
* The default implementation simply renders [[header]].
|
||||
* This method may be overridden to customize the rendering of the header cell.
|
||||
* @return string the rendering result
|
||||
*/
|
||||
protected function renderHeaderCellContent()
|
||||
{
|
||||
return trim($this->header) !== '' ? $this->header : $this->getHeaderCellLabel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns header cell label.
|
||||
* This method may be overridden to customize the label of the header cell.
|
||||
* @return string label
|
||||
* @since 2.0.8
|
||||
*/
|
||||
protected function getHeaderCellLabel()
|
||||
{
|
||||
return $this->grid->emptyCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the footer cell content.
|
||||
* The default implementation simply renders [[footer]].
|
||||
* This method may be overridden to customize the rendering of the footer cell.
|
||||
* @return string the rendering result
|
||||
*/
|
||||
protected function renderFooterCellContent()
|
||||
{
|
||||
return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the data cell content.
|
||||
* @param mixed $model the data model
|
||||
* @param mixed $key the key associated with the data model
|
||||
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
|
||||
* @return string the rendering result
|
||||
*/
|
||||
protected function renderDataCellContent($model, $key, $index)
|
||||
{
|
||||
if ($this->content !== null) {
|
||||
return call_user_func($this->content, $model, $key, $index, $this);
|
||||
}
|
||||
|
||||
return $this->grid->emptyCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the filter cell content.
|
||||
* The default implementation simply renders a space.
|
||||
* This method may be overridden to customize the rendering of the filter cell (if any).
|
||||
* @return string the rendering result
|
||||
*/
|
||||
protected function renderFilterCellContent()
|
||||
{
|
||||
return $this->grid->emptyCell;
|
||||
}
|
||||
}
|
||||
249
vendor/yiisoft/yii2/grid/DataColumn.php
vendored
Normal file
249
vendor/yiisoft/yii2/grid/DataColumn.php
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use Closure;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use yii\data\ArrayDataProvider;
|
||||
use yii\db\ActiveQueryInterface;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Inflector;
|
||||
|
||||
/**
|
||||
* DataColumn is the default column type for the [[GridView]] widget.
|
||||
*
|
||||
* It is used to show data columns and allows [[enableSorting|sorting]] and [[filter|filtering]] them.
|
||||
*
|
||||
* A simple data column definition refers to an attribute in the data model of the
|
||||
* GridView's data provider. The name of the attribute is specified by [[attribute]].
|
||||
*
|
||||
* By setting [[value]] and [[label]], the header and cell content can be customized.
|
||||
*
|
||||
* A data column differentiates between the [[getDataCellValue|data cell value]] and the
|
||||
* [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that
|
||||
* may be used for calculation, while the actual cell content is a [[format|formatted]] version of that
|
||||
* value which may contain HTML markup.
|
||||
*
|
||||
* For more details and usage information on DataColumn, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class DataColumn extends Column
|
||||
{
|
||||
/**
|
||||
* @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
|
||||
* is specified, the value of the specified attribute will be retrieved from each data model and displayed.
|
||||
*
|
||||
* Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
|
||||
*/
|
||||
public $attribute;
|
||||
/**
|
||||
* @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
|
||||
* link label when sorting is enabled for this column.
|
||||
* If it is not set and the models provided by the GridViews data provider are instances
|
||||
* of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
|
||||
* Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
|
||||
*/
|
||||
public $label;
|
||||
/**
|
||||
* @var bool whether the header label should be HTML-encoded.
|
||||
* @see label
|
||||
* @since 2.0.1
|
||||
*/
|
||||
public $encodeLabel = true;
|
||||
/**
|
||||
* @var string|Closure an anonymous function or a string that is used to determine the value to display in the current column.
|
||||
*
|
||||
* If this is an anonymous function, it will be called for each row and the return value will be used as the value to
|
||||
* display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
|
||||
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
|
||||
* and `$column` is a reference to the [[DataColumn]] object.
|
||||
*
|
||||
* You may also set this property to a string representing the attribute name to be displayed in this column.
|
||||
* This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
|
||||
* sorting and filtering.
|
||||
*
|
||||
* If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
|
||||
*/
|
||||
public $value;
|
||||
/**
|
||||
* @var string|array|Closure in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
|
||||
* `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
|
||||
* the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
|
||||
* [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
|
||||
* @see \yii\i18n\Formatter::format()
|
||||
*/
|
||||
public $format = 'text';
|
||||
/**
|
||||
* @var bool whether to allow sorting by this column. If true and [[attribute]] is found in
|
||||
* the sort definition of [[GridView::dataProvider]], then the header cell of this column
|
||||
* will contain a link that may trigger the sorting when being clicked.
|
||||
*/
|
||||
public $enableSorting = true;
|
||||
/**
|
||||
* @var array the HTML attributes for the link tag in the header cell
|
||||
* generated by [[\yii\data\Sort::link]] when sorting is enabled for this column.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $sortLinkOptions = [];
|
||||
/**
|
||||
* @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list)
|
||||
* that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
|
||||
*
|
||||
* - If this property is not set, a text field will be generated as the filter input with attributes defined
|
||||
* with [[filterInputOptions]]. See [[\yii\helpers\BaseHtml::activeInput]] for details on how an active
|
||||
* input tag is generated.
|
||||
* - If this property is an array, a dropdown list will be generated that uses this property value as
|
||||
* the list options.
|
||||
* - If you don't want a filter for this data column, set this value to be false.
|
||||
*/
|
||||
public $filter;
|
||||
/**
|
||||
* @var array the HTML attributes for the filter input fields. This property is used in combination with
|
||||
* the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
|
||||
* render the HTML attributes for the generated filter input fields.
|
||||
*
|
||||
* Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
|
||||
* providing better performance.
|
||||
*
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $filterInputOptions = ['class' => 'form-control', 'id' => null];
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderHeaderCellContent()
|
||||
{
|
||||
if ($this->header !== null || $this->label === null && $this->attribute === null) {
|
||||
return parent::renderHeaderCellContent();
|
||||
}
|
||||
|
||||
$label = $this->getHeaderCellLabel();
|
||||
if ($this->encodeLabel) {
|
||||
$label = Html::encode($label);
|
||||
}
|
||||
|
||||
if ($this->attribute !== null && $this->enableSorting &&
|
||||
($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
|
||||
return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
|
||||
}
|
||||
|
||||
return $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
* @since 2.0.8
|
||||
*/
|
||||
protected function getHeaderCellLabel()
|
||||
{
|
||||
$provider = $this->grid->dataProvider;
|
||||
|
||||
if ($this->label === null) {
|
||||
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
|
||||
/* @var $modelClass Model */
|
||||
$modelClass = $provider->query->modelClass;
|
||||
$model = $modelClass::instance();
|
||||
$label = $model->getAttributeLabel($this->attribute);
|
||||
} elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
|
||||
/* @var $modelClass Model */
|
||||
$modelClass = $provider->modelClass;
|
||||
$model = $modelClass::instance();
|
||||
$label = $model->getAttributeLabel($this->attribute);
|
||||
} elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
|
||||
$label = $this->grid->filterModel->getAttributeLabel($this->attribute);
|
||||
} else {
|
||||
$models = $provider->getModels();
|
||||
if (($model = reset($models)) instanceof Model) {
|
||||
/* @var $model Model */
|
||||
$label = $model->getAttributeLabel($this->attribute);
|
||||
} else {
|
||||
$label = Inflector::camel2words($this->attribute);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$label = $this->label;
|
||||
}
|
||||
|
||||
return $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderFilterCellContent()
|
||||
{
|
||||
if (is_string($this->filter)) {
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
$model = $this->grid->filterModel;
|
||||
|
||||
if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
|
||||
if ($model->hasErrors($this->attribute)) {
|
||||
Html::addCssClass($this->filterOptions, 'has-error');
|
||||
$error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions);
|
||||
} else {
|
||||
$error = '';
|
||||
}
|
||||
if (is_array($this->filter)) {
|
||||
$options = array_merge(['prompt' => ''], $this->filterInputOptions);
|
||||
return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error;
|
||||
} elseif ($this->format === 'boolean') {
|
||||
$options = array_merge(['prompt' => ''], $this->filterInputOptions);
|
||||
return Html::activeDropDownList($model, $this->attribute, [
|
||||
1 => $this->grid->formatter->booleanFormat[1],
|
||||
0 => $this->grid->formatter->booleanFormat[0],
|
||||
], $options) . $error;
|
||||
}
|
||||
|
||||
return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error;
|
||||
}
|
||||
|
||||
return parent::renderFilterCellContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data cell value.
|
||||
* @param mixed $model the data model
|
||||
* @param mixed $key the key associated with the data model
|
||||
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
|
||||
* @return string the data cell value
|
||||
*/
|
||||
public function getDataCellValue($model, $key, $index)
|
||||
{
|
||||
if ($this->value !== null) {
|
||||
if (is_string($this->value)) {
|
||||
return ArrayHelper::getValue($model, $this->value);
|
||||
}
|
||||
|
||||
return call_user_func($this->value, $model, $key, $index, $this);
|
||||
} elseif ($this->attribute !== null) {
|
||||
return ArrayHelper::getValue($model, $this->attribute);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderDataCellContent($model, $key, $index)
|
||||
{
|
||||
if ($this->content === null) {
|
||||
return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
|
||||
}
|
||||
|
||||
return parent::renderDataCellContent($model, $key, $index);
|
||||
}
|
||||
}
|
||||
594
vendor/yiisoft/yii2/grid/GridView.php
vendored
Normal file
594
vendor/yiisoft/yii2/grid/GridView.php
vendored
Normal file
@@ -0,0 +1,594 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use Closure;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\base\Model;
|
||||
use yii\helpers\Html;
|
||||
use yii\helpers\Json;
|
||||
use yii\helpers\Url;
|
||||
use yii\i18n\Formatter;
|
||||
use yii\widgets\BaseListView;
|
||||
|
||||
/**
|
||||
* The GridView widget is used to display data in a grid.
|
||||
*
|
||||
* It provides features like [[sorter|sorting]], [[pager|paging]] and also [[filterModel|filtering]] the data.
|
||||
*
|
||||
* A basic usage looks like the following:
|
||||
*
|
||||
* ```php
|
||||
* <?= GridView::widget([
|
||||
* 'dataProvider' => $dataProvider,
|
||||
* 'columns' => [
|
||||
* 'id',
|
||||
* 'name',
|
||||
* 'created_at:datetime',
|
||||
* // ...
|
||||
* ],
|
||||
* ]) ?>
|
||||
* ```
|
||||
*
|
||||
* The columns of the grid table are configured in terms of [[Column]] classes,
|
||||
* which are configured via [[columns]].
|
||||
*
|
||||
* The look and feel of a grid view can be customized using the large amount of properties.
|
||||
*
|
||||
* For more details and usage information on GridView, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class GridView extends BaseListView
|
||||
{
|
||||
const FILTER_POS_HEADER = 'header';
|
||||
const FILTER_POS_FOOTER = 'footer';
|
||||
const FILTER_POS_BODY = 'body';
|
||||
|
||||
/**
|
||||
* @var string the default data column class if the class name is not explicitly specified when configuring a data column.
|
||||
* Defaults to 'yii\grid\DataColumn'.
|
||||
*/
|
||||
public $dataColumnClass;
|
||||
/**
|
||||
* @var string the caption of the grid table
|
||||
* @see captionOptions
|
||||
*/
|
||||
public $caption;
|
||||
/**
|
||||
* @var array the HTML attributes for the caption element.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
* @see caption
|
||||
*/
|
||||
public $captionOptions = [];
|
||||
/**
|
||||
* @var array the HTML attributes for the grid table element.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $tableOptions = ['class' => 'table table-striped table-bordered'];
|
||||
/**
|
||||
* @var array the HTML attributes for the container tag of the grid view.
|
||||
* The "tag" element specifies the tag name of the container element and defaults to "div".
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $options = ['class' => 'grid-view'];
|
||||
/**
|
||||
* @var array the HTML attributes for the table header row.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $headerRowOptions = [];
|
||||
/**
|
||||
* @var array the HTML attributes for the table footer row.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $footerRowOptions = [];
|
||||
/**
|
||||
* @var array|Closure the HTML attributes for the table body rows. This can be either an array
|
||||
* specifying the common HTML attributes for all body rows, or an anonymous function that
|
||||
* returns an array of the HTML attributes. The anonymous function will be called once for every
|
||||
* data model returned by [[dataProvider]]. It should have the following signature:
|
||||
*
|
||||
* ```php
|
||||
* function ($model, $key, $index, $grid)
|
||||
* ```
|
||||
*
|
||||
* - `$model`: the current data model being rendered
|
||||
* - `$key`: the key value associated with the current data model
|
||||
* - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]]
|
||||
* - `$grid`: the GridView object
|
||||
*
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $rowOptions = [];
|
||||
/**
|
||||
* @var Closure an anonymous function that is called once BEFORE rendering each data model.
|
||||
* It should have the similar signature as [[rowOptions]]. The return result of the function
|
||||
* will be rendered directly.
|
||||
*/
|
||||
public $beforeRow;
|
||||
/**
|
||||
* @var Closure an anonymous function that is called once AFTER rendering each data model.
|
||||
* It should have the similar signature as [[rowOptions]]. The return result of the function
|
||||
* will be rendered directly.
|
||||
*/
|
||||
public $afterRow;
|
||||
/**
|
||||
* @var bool whether to show the header section of the grid table.
|
||||
*/
|
||||
public $showHeader = true;
|
||||
/**
|
||||
* @var bool whether to show the footer section of the grid table.
|
||||
*/
|
||||
public $showFooter = false;
|
||||
/**
|
||||
* @var bool whether to place footer after body in DOM if $showFooter is true
|
||||
* @since 2.0.14
|
||||
*/
|
||||
public $placeFooterAfterBody = false;
|
||||
/**
|
||||
* @var bool whether to show the grid view if [[dataProvider]] returns no data.
|
||||
*/
|
||||
public $showOnEmpty = true;
|
||||
/**
|
||||
* @var array|Formatter the formatter used to format model attribute values into displayable texts.
|
||||
* This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]]
|
||||
* instance. If this property is not set, the "formatter" application component will be used.
|
||||
*/
|
||||
public $formatter;
|
||||
/**
|
||||
* @var array grid column configuration. Each array element represents the configuration
|
||||
* for one particular grid column. For example,
|
||||
*
|
||||
* ```php
|
||||
* [
|
||||
* ['class' => SerialColumn::className()],
|
||||
* [
|
||||
* 'class' => DataColumn::className(), // this line is optional
|
||||
* 'attribute' => 'name',
|
||||
* 'format' => 'text',
|
||||
* 'label' => 'Name',
|
||||
* ],
|
||||
* ['class' => CheckboxColumn::className()],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* If a column is of class [[DataColumn]], the "class" element can be omitted.
|
||||
*
|
||||
* As a shortcut format, a string may be used to specify the configuration of a data column
|
||||
* which only contains [[DataColumn::attribute|attribute]], [[DataColumn::format|format]],
|
||||
* and/or [[DataColumn::label|label]] options: `"attribute:format:label"`.
|
||||
* For example, the above "name" column can also be specified as: `"name:text:Name"`.
|
||||
* Both "format" and "label" are optional. They will take default values if absent.
|
||||
*
|
||||
* Using the shortcut format the configuration for columns in simple cases would look like this:
|
||||
*
|
||||
* ```php
|
||||
* [
|
||||
* 'id',
|
||||
* 'amount:currency:Total Amount',
|
||||
* 'created_at:datetime',
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* When using a [[dataProvider]] with active records, you can also display values from related records,
|
||||
* e.g. the `name` attribute of the `author` relation:
|
||||
*
|
||||
* ```php
|
||||
* // shortcut syntax
|
||||
* 'author.name',
|
||||
* // full syntax
|
||||
* [
|
||||
* 'attribute' => 'author.name',
|
||||
* // ...
|
||||
* ]
|
||||
* ```
|
||||
*/
|
||||
public $columns = [];
|
||||
/**
|
||||
* @var string the HTML display when the content of a cell is empty.
|
||||
* This property is used to render cells that have no defined content,
|
||||
* e.g. empty footer or filter cells.
|
||||
*
|
||||
* Note that this is not used by the [[DataColumn]] if a data item is `null`. In that case
|
||||
* the [[\yii\i18n\Formatter::nullDisplay|nullDisplay]] property of the [[formatter]] will
|
||||
* be used to indicate an empty data value.
|
||||
*/
|
||||
public $emptyCell = ' ';
|
||||
/**
|
||||
* @var \yii\base\Model the model that keeps the user-entered filter data. When this property is set,
|
||||
* the grid view will enable column-based filtering. Each data column by default will display a text field
|
||||
* at the top that users can fill in to filter the data.
|
||||
*
|
||||
* Note that in order to show an input field for filtering, a column must have its [[DataColumn::attribute]]
|
||||
* property set and the attribute should be active in the current scenario of $filterModel or have
|
||||
* [[DataColumn::filter]] set as the HTML code for the input field.
|
||||
*
|
||||
* When this property is not set (null) the filtering feature is disabled.
|
||||
*/
|
||||
public $filterModel;
|
||||
/**
|
||||
* @var string|array the URL for returning the filtering result. [[Url::to()]] will be called to
|
||||
* normalize the URL. If not set, the current controller action will be used.
|
||||
* When the user makes change to any filter input, the current filtering inputs will be appended
|
||||
* as GET parameters to this URL.
|
||||
*/
|
||||
public $filterUrl;
|
||||
/**
|
||||
* @var string additional jQuery selector for selecting filter input fields
|
||||
*/
|
||||
public $filterSelector;
|
||||
/**
|
||||
* @var string whether the filters should be displayed in the grid view. Valid values include:
|
||||
*
|
||||
* - [[FILTER_POS_HEADER]]: the filters will be displayed on top of each column's header cell.
|
||||
* - [[FILTER_POS_BODY]]: the filters will be displayed right below each column's header cell.
|
||||
* - [[FILTER_POS_FOOTER]]: the filters will be displayed below each column's footer cell.
|
||||
*/
|
||||
public $filterPosition = self::FILTER_POS_BODY;
|
||||
/**
|
||||
* @var array the HTML attributes for the filter row element.
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $filterRowOptions = ['class' => 'filters'];
|
||||
/**
|
||||
* @var array the options for rendering the filter error summary.
|
||||
* Please refer to [[Html::errorSummary()]] for more details about how to specify the options.
|
||||
* @see renderErrors()
|
||||
*/
|
||||
public $filterErrorSummaryOptions = ['class' => 'error-summary'];
|
||||
/**
|
||||
* @var array the options for rendering every filter error message.
|
||||
* This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field.
|
||||
*/
|
||||
public $filterErrorOptions = ['class' => 'help-block'];
|
||||
/**
|
||||
* @var string the layout that determines how different sections of the grid view should be organized.
|
||||
* The following tokens will be replaced with the corresponding section contents:
|
||||
*
|
||||
* - `{summary}`: the summary section. See [[renderSummary()]].
|
||||
* - `{errors}`: the filter model error summary. See [[renderErrors()]].
|
||||
* - `{items}`: the list items. See [[renderItems()]].
|
||||
* - `{sorter}`: the sorter. See [[renderSorter()]].
|
||||
* - `{pager}`: the pager. See [[renderPager()]].
|
||||
*/
|
||||
public $layout = "{summary}\n{items}\n{pager}";
|
||||
|
||||
|
||||
/**
|
||||
* Initializes the grid view.
|
||||
* This method will initialize required property values and instantiate [[columns]] objects.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
if ($this->formatter === null) {
|
||||
$this->formatter = Yii::$app->getFormatter();
|
||||
} elseif (is_array($this->formatter)) {
|
||||
$this->formatter = Yii::createObject($this->formatter);
|
||||
}
|
||||
if (!$this->formatter instanceof Formatter) {
|
||||
throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
|
||||
}
|
||||
if (!isset($this->filterRowOptions['id'])) {
|
||||
$this->filterRowOptions['id'] = $this->options['id'] . '-filters';
|
||||
}
|
||||
|
||||
$this->initColumns();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the widget.
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$id = $this->options['id'];
|
||||
$options = Json::htmlEncode($this->getClientOptions());
|
||||
$view = $this->getView();
|
||||
GridViewAsset::register($view);
|
||||
$view->registerJs("jQuery('#$id').yiiGridView($options);");
|
||||
parent::run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders validator errors of filter model.
|
||||
* @return string the rendering result.
|
||||
*/
|
||||
public function renderErrors()
|
||||
{
|
||||
if ($this->filterModel instanceof Model && $this->filterModel->hasErrors()) {
|
||||
return Html::errorSummary($this->filterModel, $this->filterErrorSummaryOptions);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function renderSection($name)
|
||||
{
|
||||
switch ($name) {
|
||||
case '{errors}':
|
||||
return $this->renderErrors();
|
||||
default:
|
||||
return parent::renderSection($name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the options for the grid view JS widget.
|
||||
* @return array the options
|
||||
*/
|
||||
protected function getClientOptions()
|
||||
{
|
||||
$filterUrl = isset($this->filterUrl) ? $this->filterUrl : Yii::$app->request->url;
|
||||
$id = $this->filterRowOptions['id'];
|
||||
$filterSelector = "#$id input, #$id select";
|
||||
if (isset($this->filterSelector)) {
|
||||
$filterSelector .= ', ' . $this->filterSelector;
|
||||
}
|
||||
|
||||
return [
|
||||
'filterUrl' => Url::to($filterUrl),
|
||||
'filterSelector' => $filterSelector,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the data models for the grid view.
|
||||
* @return string the HTML code of table
|
||||
*/
|
||||
public function renderItems()
|
||||
{
|
||||
$caption = $this->renderCaption();
|
||||
$columnGroup = $this->renderColumnGroup();
|
||||
$tableHeader = $this->showHeader ? $this->renderTableHeader() : false;
|
||||
$tableBody = $this->renderTableBody();
|
||||
|
||||
$tableFooter = false;
|
||||
$tableFooterAfterBody = false;
|
||||
|
||||
if ($this->showFooter) {
|
||||
if ($this->placeFooterAfterBody) {
|
||||
$tableFooterAfterBody = $this->renderTableFooter();
|
||||
} else {
|
||||
$tableFooter = $this->renderTableFooter();
|
||||
}
|
||||
}
|
||||
|
||||
$content = array_filter([
|
||||
$caption,
|
||||
$columnGroup,
|
||||
$tableHeader,
|
||||
$tableFooter,
|
||||
$tableBody,
|
||||
$tableFooterAfterBody,
|
||||
]);
|
||||
|
||||
return Html::tag('table', implode("\n", $content), $this->tableOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the caption element.
|
||||
* @return bool|string the rendered caption element or `false` if no caption element should be rendered.
|
||||
*/
|
||||
public function renderCaption()
|
||||
{
|
||||
if (!empty($this->caption)) {
|
||||
return Html::tag('caption', $this->caption, $this->captionOptions);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the column group HTML.
|
||||
* @return bool|string the column group HTML or `false` if no column group should be rendered.
|
||||
*/
|
||||
public function renderColumnGroup()
|
||||
{
|
||||
foreach ($this->columns as $column) {
|
||||
/* @var $column Column */
|
||||
if (!empty($column->options)) {
|
||||
$cols = [];
|
||||
foreach ($this->columns as $col) {
|
||||
$cols[] = Html::tag('col', '', $col->options);
|
||||
}
|
||||
|
||||
return Html::tag('colgroup', implode("\n", $cols));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the table header.
|
||||
* @return string the rendering result.
|
||||
*/
|
||||
public function renderTableHeader()
|
||||
{
|
||||
$cells = [];
|
||||
foreach ($this->columns as $column) {
|
||||
/* @var $column Column */
|
||||
$cells[] = $column->renderHeaderCell();
|
||||
}
|
||||
$content = Html::tag('tr', implode('', $cells), $this->headerRowOptions);
|
||||
if ($this->filterPosition === self::FILTER_POS_HEADER) {
|
||||
$content = $this->renderFilters() . $content;
|
||||
} elseif ($this->filterPosition === self::FILTER_POS_BODY) {
|
||||
$content .= $this->renderFilters();
|
||||
}
|
||||
|
||||
return "<thead>\n" . $content . "\n</thead>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the table footer.
|
||||
* @return string the rendering result.
|
||||
*/
|
||||
public function renderTableFooter()
|
||||
{
|
||||
$cells = [];
|
||||
foreach ($this->columns as $column) {
|
||||
/* @var $column Column */
|
||||
$cells[] = $column->renderFooterCell();
|
||||
}
|
||||
$content = Html::tag('tr', implode('', $cells), $this->footerRowOptions);
|
||||
if ($this->filterPosition === self::FILTER_POS_FOOTER) {
|
||||
$content .= $this->renderFilters();
|
||||
}
|
||||
|
||||
return "<tfoot>\n" . $content . "\n</tfoot>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the filter.
|
||||
* @return string the rendering result.
|
||||
*/
|
||||
public function renderFilters()
|
||||
{
|
||||
if ($this->filterModel !== null) {
|
||||
$cells = [];
|
||||
foreach ($this->columns as $column) {
|
||||
/* @var $column Column */
|
||||
$cells[] = $column->renderFilterCell();
|
||||
}
|
||||
|
||||
return Html::tag('tr', implode('', $cells), $this->filterRowOptions);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the table body.
|
||||
* @return string the rendering result.
|
||||
*/
|
||||
public function renderTableBody()
|
||||
{
|
||||
$models = array_values($this->dataProvider->getModels());
|
||||
$keys = $this->dataProvider->getKeys();
|
||||
$rows = [];
|
||||
foreach ($models as $index => $model) {
|
||||
$key = $keys[$index];
|
||||
if ($this->beforeRow !== null) {
|
||||
$row = call_user_func($this->beforeRow, $model, $key, $index, $this);
|
||||
if (!empty($row)) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
$rows[] = $this->renderTableRow($model, $key, $index);
|
||||
|
||||
if ($this->afterRow !== null) {
|
||||
$row = call_user_func($this->afterRow, $model, $key, $index, $this);
|
||||
if (!empty($row)) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($rows) && $this->emptyText !== false) {
|
||||
$colspan = count($this->columns);
|
||||
|
||||
return "<tbody>\n<tr><td colspan=\"$colspan\">" . $this->renderEmpty() . "</td></tr>\n</tbody>";
|
||||
}
|
||||
|
||||
return "<tbody>\n" . implode("\n", $rows) . "\n</tbody>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a table row with the given data model and key.
|
||||
* @param mixed $model the data model to be rendered
|
||||
* @param mixed $key the key associated with the data model
|
||||
* @param int $index the zero-based index of the data model among the model array returned by [[dataProvider]].
|
||||
* @return string the rendering result
|
||||
*/
|
||||
public function renderTableRow($model, $key, $index)
|
||||
{
|
||||
$cells = [];
|
||||
/* @var $column Column */
|
||||
foreach ($this->columns as $column) {
|
||||
$cells[] = $column->renderDataCell($model, $key, $index);
|
||||
}
|
||||
if ($this->rowOptions instanceof Closure) {
|
||||
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
|
||||
} else {
|
||||
$options = $this->rowOptions;
|
||||
}
|
||||
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
|
||||
|
||||
return Html::tag('tr', implode('', $cells), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates column objects and initializes them.
|
||||
*/
|
||||
protected function initColumns()
|
||||
{
|
||||
if (empty($this->columns)) {
|
||||
$this->guessColumns();
|
||||
}
|
||||
foreach ($this->columns as $i => $column) {
|
||||
if (is_string($column)) {
|
||||
$column = $this->createDataColumn($column);
|
||||
} else {
|
||||
$column = Yii::createObject(array_merge([
|
||||
'class' => $this->dataColumnClass ?: DataColumn::className(),
|
||||
'grid' => $this,
|
||||
], $column));
|
||||
}
|
||||
if (!$column->visible) {
|
||||
unset($this->columns[$i]);
|
||||
continue;
|
||||
}
|
||||
$this->columns[$i] = $column;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label".
|
||||
* @param string $text the column specification string
|
||||
* @return DataColumn the column instance
|
||||
* @throws InvalidConfigException if the column specification is invalid
|
||||
*/
|
||||
protected function createDataColumn($text)
|
||||
{
|
||||
if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
|
||||
throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
|
||||
}
|
||||
|
||||
return Yii::createObject([
|
||||
'class' => $this->dataColumnClass ?: DataColumn::className(),
|
||||
'grid' => $this,
|
||||
'attribute' => $matches[1],
|
||||
'format' => isset($matches[3]) ? $matches[3] : 'text',
|
||||
'label' => isset($matches[5]) ? $matches[5] : null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tries to guess the columns to show from the given data
|
||||
* if [[columns]] are not explicitly specified.
|
||||
*/
|
||||
protected function guessColumns()
|
||||
{
|
||||
$models = $this->dataProvider->getModels();
|
||||
$model = reset($models);
|
||||
if (is_array($model) || is_object($model)) {
|
||||
foreach ($model as $name => $value) {
|
||||
if ($value === null || is_scalar($value) || is_callable([$value, '__toString'])) {
|
||||
$this->columns[] = (string) $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
vendor/yiisoft/yii2/grid/GridViewAsset.php
vendored
Normal file
27
vendor/yiisoft/yii2/grid/GridViewAsset.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use yii\web\AssetBundle;
|
||||
|
||||
/**
|
||||
* This asset bundle provides the javascript files for the [[GridView]] widget.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class GridViewAsset extends AssetBundle
|
||||
{
|
||||
public $sourcePath = '@yii/assets';
|
||||
public $js = [
|
||||
'yii.gridView.js',
|
||||
];
|
||||
public $depends = [
|
||||
'yii\web\YiiAsset',
|
||||
];
|
||||
}
|
||||
94
vendor/yiisoft/yii2/grid/RadioButtonColumn.php
vendored
Normal file
94
vendor/yiisoft/yii2/grid/RadioButtonColumn.php
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
use Closure;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\Html;
|
||||
|
||||
/**
|
||||
* RadioButtonColumn displays a column of radio buttons in a grid view.
|
||||
*
|
||||
* To add a RadioButtonColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
|
||||
*
|
||||
* ```php
|
||||
* 'columns' => [
|
||||
* // ...
|
||||
* [
|
||||
* 'class' => 'yii\grid\RadioButtonColumn',
|
||||
* 'radioOptions' => function ($model) {
|
||||
* return [
|
||||
* 'value' => $model['value'],
|
||||
* 'checked' => $model['value'] == 2
|
||||
* ];
|
||||
* }
|
||||
* ],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* @author Kirk Hansen <hanski07@luther.edu>
|
||||
* @since 2.0.11
|
||||
*/
|
||||
class RadioButtonColumn extends Column
|
||||
{
|
||||
/**
|
||||
* @var string the name of the input radio button input fields.
|
||||
*/
|
||||
public $name = 'radioButtonSelection';
|
||||
/**
|
||||
* @var array|\Closure the HTML attributes for the radio buttons. This can either be an array of
|
||||
* attributes or an anonymous function ([[Closure]]) returning such an array.
|
||||
*
|
||||
* The signature of the function should be as follows: `function ($model, $key, $index, $column)`
|
||||
* where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
|
||||
* and `$column` is a reference to the [[RadioButtonColumn]] object.
|
||||
*
|
||||
* A function may be used to assign different attributes to different rows based on the data in that row.
|
||||
* Specifically if you want to set a different value for the radio button you can use this option
|
||||
* in the following way (in this example using the `name` attribute of the model):
|
||||
*
|
||||
* ```php
|
||||
* 'radioOptions' => function ($model, $key, $index, $column) {
|
||||
* return ['value' => $model->attribute];
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
||||
*/
|
||||
public $radioOptions = [];
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws \yii\base\InvalidConfigException if [[name]] is not set.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
if (empty($this->name)) {
|
||||
throw new InvalidConfigException('The "name" property must be set.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderDataCellContent($model, $key, $index)
|
||||
{
|
||||
if ($this->radioOptions instanceof Closure) {
|
||||
$options = call_user_func($this->radioOptions, $model, $key, $index, $this);
|
||||
} else {
|
||||
$options = $this->radioOptions;
|
||||
if (!isset($options['value'])) {
|
||||
$options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key;
|
||||
}
|
||||
}
|
||||
$checked = isset($options['checked']) ? $options['checked'] : false;
|
||||
return Html::radio($this->name, $checked, $options);
|
||||
}
|
||||
}
|
||||
50
vendor/yiisoft/yii2/grid/SerialColumn.php
vendored
Normal file
50
vendor/yiisoft/yii2/grid/SerialColumn.php
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\grid;
|
||||
|
||||
/**
|
||||
* SerialColumn displays a column of row numbers (1-based).
|
||||
*
|
||||
* To add a SerialColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
|
||||
*
|
||||
* ```php
|
||||
* 'columns' => [
|
||||
* // ...
|
||||
* [
|
||||
* 'class' => 'yii\grid\SerialColumn',
|
||||
* // you may configure additional properties here
|
||||
* ],
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* For more details and usage information on SerialColumn, see the [guide article on data widgets](guide:output-data-widgets).
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class SerialColumn extends Column
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $header = '#';
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function renderDataCellContent($model, $key, $index)
|
||||
{
|
||||
$pagination = $this->grid->dataProvider->getPagination();
|
||||
if ($pagination !== false) {
|
||||
return $pagination->getOffset() + $index + 1;
|
||||
}
|
||||
|
||||
return $index + 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user