Class rootlocal\widgets\sortable\SortableGridColumnWidget
Inheritance | rootlocal\widgets\sortable\SortableGridColumnWidget » yii\grid\Column |
---|
Class SortableGridColumn Sortable Column for Yii2 Grid widget
To add an SortableGridColumn to the gridview. Configuration as follows:
'columns' => [
// ...
[
'class' => SortableGridColumnWidget::class,
// you may configure additional properties here
],
]
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$buttonOptions | array | Html options to be applied to the default button. | rootlocal\widgets\sortable\SortableGridColumnWidget |
$buttons | array | Button rendering callbacks. | rootlocal\widgets\sortable\SortableGridColumnWidget |
$headerOptions | array | The HTML attributes for the header cell tag. | rootlocal\widgets\sortable\SortableGridColumnWidget |
$icons | array | Button icons. | rootlocal\widgets\sortable\SortableGridColumnWidget |
$sortableWidgetOptions | array | rootlocal\widgets\sortable\SortableWidget Sortable JQuery Widget | rootlocal\widgets\sortable\SortableGridColumnWidget |
$template | string | The template used for composing each cell in the action column. | rootlocal\widgets\sortable\SortableGridColumnWidget |
$visibleButtons | array | Visibility conditions for each button. | rootlocal\widgets\sortable\SortableGridColumnWidget |
Public Methods
Method | Description | Defined By |
---|---|---|
init() | rootlocal\widgets\sortable\SortableGridColumnWidget |
Protected Methods
Method | Description | Defined By |
---|---|---|
initDefaultButton() | Initializes the default button rendering callback for single button. | rootlocal\widgets\sortable\SortableGridColumnWidget |
initDefaultButtons() | Initializes the default button rendering callbacks. | rootlocal\widgets\sortable\SortableGridColumnWidget |
renderDataCellContent() | Renders the data cell content. | rootlocal\widgets\sortable\SortableGridColumnWidget |
Property Details
The HTML attributes for the header cell tag.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Button icons. The array keys are the icon names and the values the corresponding html:
`
php
[
'sort' => '<i class="fa fa-sort" aria-hidden="true"></i>',
]
`
Defaults to FontAwesome
See also https://fontawesome.com.
rootlocal\widgets\sortable\SortableWidget Sortable JQuery Widget
The template used for composing each cell in the action column.
[
'class' => \rootlocal\widgets\sortable\SortableGridColumnWidget::class,
'template' => '{sort}'
],
See also $buttons.
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:
function ($model, $key, $index) {
return $model->status === $model::STATUS_SORTING;
}
Or you can pass a boolean value:
[
'sort' => \Yii::$app->user->can('sort'),
],
Method Details
public void init ( ) |
public function init()
{
parent::init();
$defaultContentOptions = ['class' => 'sortable-td'];
$this->contentOptions = ArrayHelper::merge($defaultContentOptions, $this->contentOptions);
$this->initDefaultButtons();
$objClass = $this->sortableWidgetOptions;
$objClass['class'] = SortableWidget::class;
Yii::createObject($objClass);
}
Initializes the default button rendering callback for single button.
protected void initDefaultButton ( string $name, string $iconName, array $additionalOptions = [] ) | ||
$name | string |
Button name as it's written in template |
$iconName | string |
The part of Bootstrap font awesome class that makes it unique |
$additionalOptions | array |
Array of additional HTML tag options |
protected function initDefaultButton(string $name, string $iconName, array $additionalOptions = [])
{
if (!isset($this->buttons[$name]) && str_contains($this->template, '{' . $name . '}')) {
$this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
switch ($name) {
case 'sort':
$title = Yii::t('rootlocal-sort', 'Sorting');
break;
case 'up':
$title = Yii::t('rootlocal-sort', 'Up');
break;
case 'down':
$title = Yii::t('rootlocal-sort', 'Down');
break;
default:
$title = ucfirst($name);
break;
}
$options = array_merge(['title' => $title, 'aria-label' => $title], $additionalOptions, $this->buttonOptions);
$icon = $this->icons[$iconName] ?? Html::tag('i', '', ['class' => "fa fa-$iconName"]);
return Html::tag('span', $icon, $options);
};
}
}
Initializes the default button rendering callbacks.
protected void initDefaultButtons ( ) |
protected function initDefaultButtons(): void
{
$this->initDefaultButton('sort', 'sort', [
'class' => 'sortable-column-btn sortable-column-btn-sort',
]);
$this->initDefaultButton('up', 'up', [
'class' => 'sortable-column-btn sortable-column-btn-up',
]);
$this->initDefaultButton('down', 'down', [
'class' => 'sortable-column-btn sortable-column-btn-down',
]);
}
Renders the data cell content.
protected string renderDataCellContent ( $model, $key, $index ) | ||
$model | mixed |
The data model |
$key | mixed |
The key associated with the data model |
$index | integer |
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)
{
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])) {
return call_user_func($this->buttons[$name], $model, $key, $this);
}
return '';
}, $this->template);
}