extra buttons to gridview

Is it possible to add extra buttons to gridview (or are there any ready extentions already available for this purpose)? The purpose is to add e.g. up/down buttons that could be used to change the row order in the gridview (=order backlog in this case).

Like this?

http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$template-detail

I guess so :slight_smile: Any practical examples?

Something like this


<?= GridView::widget([

	'dataProvider' => $dataProvider,

	'filterModel' => $searchModel,

	'columns' => 

		'id',

		'exampleColumn',

		[

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

			'template' => '{add}',

			'buttons' => [

				'add' => function ($action, $model) {

						return Html::a('Add', ['yourController/action', 'id' => $model->id], ['data-pjax' => '0']);

				},

			]

		],

]); ?>

Thanks, here’s now my version:




array_push($gridColumns,

[

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

    'template' => '{moveup} {movedown}',

    'buttons' =>

    [

        'moveup' => function ($action, $model)

        {

            if ($model->status == ORDER_STATUS_CREATED)

            {

                return Html::a('<span class="glyphicon glyphicon-chevron-up"', ['moveup', 'id' => $model->orderid], ['data-pjax' => '0']);

            }

        },

        'movedown' => function ($action, $model)

        {

            if ($model->status == ORDER_STATUS_CREATED)

            {

                return Html::a('<span class="glyphicon glyphicon-chevron-down"', ['movedown', 'id' => $model->orderid], ['data-pjax' => '0']);

            }

        },

    ]

]);



By the way, one tweak needed. I don’t want the up button on the first row and down button on the last row. How can this be achieved in an effective way? Thanks.

Thanks for sharing the useful information.