Class rootlocal\widgets\sortable\SortableGridColumnWidget

Inheritancerootlocal\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
    ],
]

Protected Methods

Hide inherited 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

Hide inherited properties

$buttonOptions public property

Html options to be applied to the default button.

public array $buttonOptions = []
$buttons public property

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:

function ($model, $key) {
    // return the button HTML code
}
[
    'sort' => function ($model, $key) {
        return $model->status === $model::STATUS_SORTING ? Html::tag('span', 'sort') : '';
    },
],
public array $buttons = []
$headerOptions public property

The HTML attributes for the header cell tag.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $headerOptions = [
    
'class' => 'sortable-column',
]
$icons public property

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.

public array $icons = [
    
'sort' => '<i class="fa fa-arrows" aria-hidden="true"></i>',
    
'up' => '<i class="fa fa-arrow-up"></i>',
    
'down' => '<i class="fa fa-arrow-down"></i>',
]
$sortableWidgetOptions public property
$template public property

The template used for composing each cell in the action column.

[
     'class' => \rootlocal\widgets\sortable\SortableGridColumnWidget::class,
     'template' => '{sort}'
],

See also $buttons.

public string $template '{sort} {up} {down}'
$visibleButtons public property

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'),
],
public array $visibleButtons = []

Method Details

Hide inherited methods

init() public method

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

            
initDefaultButton() protected method

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

            
initDefaultButtons() protected method

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

            
renderDataCellContent() protected method

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