How To Change Buttons In Actioncolumn?

Can someone give me an example how to change the default buttons in \yii\grid\ActionColumn? I want to change the span classes to something different.


[

   'class' => 'yii\grid\ActionColumn',

   'buttons' => []

]

Oh. You’ll need to write closure functions for the buttons. Just copy/paste these functions into your definition above and modify as needed.




        protected function initDefaultButtons()

	{

		if (!isset($this->buttons['view'])) {

			$this->buttons['view'] = function ($model, $key, $index, $column) {

				/** @var ActionColumn $column */

				$url = $column->createUrl($model, $key, $index, 'view');

				return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [

					'title' => Yii::t('yii', 'View'),

				]);

			};

		}

		if (!isset($this->buttons['update'])) {

			$this->buttons['update'] = function ($model, $key, $index, $column) {

				/** @var ActionColumn $column */

				$url = $column->createUrl($model, $key, $index, 'update');

				return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [

					'title' => Yii::t('yii', 'Update'),

				]);

			};

		}

		if (!isset($this->buttons['delete'])) {

			$this->buttons['delete'] = function ($model, $key, $index, $column) {

				/** @var ActionColumn $column */

				$url = $column->createUrl($model, $key, $index, 'delete');

				return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [

					'title' => Yii::t('yii', 'Delete'),

					'data-confirm' => Yii::t('yii', 'Are you sure to delete this item?'),

					'data-method' => 'post',

				]);

			};

		}

	}



Yes, thanks, got that working now. Any idea how to align a single td in the gridview to the right (the ActionColumn in this case)? In short, how to add extra htmlOptions for a single <td> in the gridview?

To control HTML options for single [font="Courier New"]<td>[/font] … use [font="Courier New"]contentOptions[/font] or [font="Courier New"]headerOptions[/font] or [font="Courier New"]footerOptions[/font] (depending on whether its the table body, header, or footer… where you need it).

Check the code for the [font="Courier New"]Column[/font] class to get details.

FYI… I had raised a pull request #1466 on github to specifically control yii gridview default buttons better. Till then you can override the complete buttons config.

Thanks! :)

Hello Kartik,

How do I add an Action Button on the Grid, with a new URL Action.

Thnx

Dinesh

I guess I cud figure it out…


			['class' => 'yii\grid\ActionColumn',

                          'template'=>'{view}{create}',

                            'buttons'=>[

                              'create' => function ($url, $model) {	

                                return Html::a('<span class="glyphicon glyphicon-plus"></span>', $url, [

					'title' => Yii::t('yii', 'Create'),

				]);                                

            

                              }

                          ]                            

                            ],

Yes, you need to override the buttons configuration like you did. You should also be able to add your own custom button action like the ‘info’ example below. Note if you need a custom url action (pass the urlCreator as Closure):




$actionCol = ['class' => 'yii\grid\ActionColumn',

    'template' => '{view}{info}',

    'buttons' => [

        'info' => function ($url, $model) {

            return Html::a('<span class="glyphicon glyphicon-info-sign"></span>', $url, [

                        'title' => Yii::t('app', 'Info'),

            ]);

        }

    ],

    'urlCreator' => function ($action, $model, $key, $index) {

        if ($action === 'info') {

            $url = Yii::$app->controller->createUrl('xx'); // your own url generation logic

            return $url;

        }

    }

];



Either I’m missing something obvious or your example with using urlCreator is not working for default buttons. I have used it and I can clearly see, that it generates proper URL only for custom button (“info” in your example). It leaves default buttons without any URL.

I posted details and code examples to the very same question at Stack Overflow.

Can you tell me, what I’m doing wrong?