0

Yii2 – Custom ActionColumn

ActionColumn is a column for the yii\grid\GridView widget that displays buttons for viewing and manipulating the items.

To add an ActionColumn to the gridview, add it to the columns configuration as follows: The function parameter should be like this ‘function ($model, $key, $index, $column){}’ and it will return the array with different type of attribute for cells like class, style, data-key, etc

'columns' => [
    // ...
    [
        'class' => ActionColumn::className(),
        // you may configure additional properties here
    ],
]


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

Custom entire project in ActionColumn: In vendor\yiisoft\yii2\grid\ActionColumn.php file

Default:

ActionColumn

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

 

Custom Button Style & Icons:

ActionColumn

 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');
                        $class = 'btn btn-circle btn-dark modelButton';
                        break;
                    case 'update':
                        $title = Yii::t('yii', 'Update');
                        $class = 'btn btn-circle btn-info modelButton';
                        break;
                    case 'delete':
                        $title = Yii::t('yii', 'Delete');
                        $class = 'btn btn-circle btn-success';
                        break;
                    default:
                        $title = ucfirst($name);
                }

                if($name == 'delete') {
                    $options = array_merge([
                        'title' => $title,
                        'aria-label' => $title,
                        'data-pjax' => '0',
                        'class' => $class,
                        'value' => $url
                    ], $additionalOptions, $this->buttonOptions);
                } else {
                    $options = array_merge([
                        'title' => $title,
                        'aria-label' => $title,
                        'data-pjax' => '0',
                        'class' => $class,
                        'data-toggle'=>"modal",
                        'data-target'=>"#model_pop",
                        'value' => $url
                    ], $additionalOptions, $this->buttonOptions);
                }                
                $icon = Html::tag('i', '', ['class' => "fa fa-$iconName"]);
                return Html::button($icon, $options);
            };
        }
    }

 

Hide Actions in Action Column of a Single Row

Hide Actions in Action Column

['class' => 'yii\grid\ActionColumn',
             'header' => 'Action',
             'template' => '{update} {view}',
             'visibleButtons' => [
                    'update' => function ($model, $key, $index) {
                        return $model->updated_by == Yii::$app->user->identity->id ? false : true;
                    },
                    'view' => function ($model, $key, $index) {
                        return $model->updated_by == Yii::$app->user->identity->id ? false : true;
                    }
             ],
]

 

 Add the new button in gridview action template and configure the url, text, title etc.

Add Actions in Action Column

['class' => 'yii\grid\ActionColumn',
             'header' => 'Action',
             'template' => '{status} {update} {view}',
             'visibleButtons' => [
                    'status' => function ($model, $key, $index) {
                        return $model->updated_by == Yii::$app->user->identity->id ? false : true;
                    }
             ],
             'buttons'=>[
                    'status' => function($url, $model) {

                            if($model->status == 1) {
                                //ACTIVE
                                $title = 'deactive';
                                $class = 'btn btn-circle btn-success';
                                $icon  = 'lock';
                                $status= '3';
                            }
                            else if($model->status == 0) {
                                //DEACTIVE
                                $title = 'active';
                                $class = 'btn btn-circle btn-danger';
                                $icon  = 'lock-open';
                                $status= '1';
                            }

                            $options = [
                                //'id' => 'sa-params',
                                'title' => $title,
                                'aria-label' => $title,
                                'data-pjax' => '0',
                                'class' => $class,
                                'onclick'=> 'updateStatus("'.$title.'",'.$model->id.', '.$status.')',
                            ];
                            $icon = Html::tag('i', '', ['class' => "fa fa-$icon"]);
                            return Html::button($icon, $options);
                    }
                ],
            ],

 

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.