Search code examples
buttongridviewyii2cell

yii2 show/display various buttons in gridview


I have a problem with Yii2 (as usual). I'm trying to show many buttons in a cell in GridView. I have one ticket with four possible states:

  • 1: OPEN
  • 2: IN PROCESS
  • 3: CLOSED
  • 4: EDIT

In a column (ActionColumn?) I want to display many buttons to change it's state like this:

  • If the state is '1', I want to show two buttons: 'In process' and 'Close Ticket'.
  • If state is 2, I want to show only 'Close Ticket' button -> That's OK
  • If it's closed (state = 3), I want to show 'Edit Ticket' button -> That's OK
  • If state is 4, the I'll show 'Close Ticket' button -> That's OK

This is my code of that column in my GridView. I'm only able to show one button, don't know how to do it for showing more buttons, as you can't return more than one element, or an array:

[
               'label' => 'Change State:',
               'format' => 'raw',
               'value' => function($dataProvider){
                            if($dataProvider->state== '1'){
                               return Html::a('In Process', ['/tickets/inprocessticket', 'id' => $dataProvider->id], ['class'=>'btn btn-warning', 'id' => 'btn_inProcessTicket']);
                            }else if($dataProvider->state== '2'){
                               return Html::a('Close Ticket', ['/tickets/closeticket', 'id' => $dataProvider->id], ['class'=>'btn btn-danger', 'id' => 'btn_closeTicket']);
                            }else if($dataProvider->state== '3'){
                               return Html::a('Edit Ticket', ['/tickets/editticket', 'id' => $dataProvider->id], ['class'=>'btn btn-info', 'id' => 'btn_editTicket']);
                            }else if($dataProvider->state== '4'){
                               return Html::a('Close Ticket', ['/tickets/closeticket', 'id' => $dataProvider->id], ['class'=>'btn btn-danger', 'id' => 'btn_closeTicket']);
                            }
                          },
           ],

Thank you all for your help!


Solution

  • If you want to use yii\grid\ActionColumn you should define the buttons in $buttons property and use $visibleButtons callbacks to determine which buttons should be displayed. You can use $template property to set their order.

    [
        'class' => \yii\grid\ActionColumn::class,
        'template' => '{process} {edit} {close}' //here will be all posible buttons
        'buttons' => [
            'process' => function($url, $model, $key) {
                return Html::a(
                    'In Process',
                    [
                        '/tickets/inprocessticket',
                        'id' => $model->id
                    ],
                    [
                        'class'=>'btn btn-warning',
                        'id' => 'btn_inProcessTicket'
                    ]
                );
            },
            'edit' => function($url, $model, $key) {
                return Html::a(
                    'Edit Ticket',
                    [
                        '/tickets/editticket',
                        'id' => $dataProvider->id
                    ], 
                    [
                        'class'=>'btn btn-info',
                        'id' => 'btn_editTicket'
                    ]
                );
            },
            'close' => function ($url, $model, $key) {
                return Html::a(
                    'Close Ticket',
                    [
                        '/tickets/closeticket',
                        'id' => $dataProvider->id
                    ],
                    [
                        'class'=>'btn btn-danger',
                        'id' => 'btn_closeTicket'
                    ]
                );
            },
        ],
        'visibleButtons' => [
            'process' => function($model, $key, $index) {
                  //the in process button should only be shown if state == 1
                  return $model->state == 1;
            },
            'edit' => function($model, $key, $index) {
                  //the edit button should only be shown if state == 3
                  return $model->state == 3;
            },
            'close' => function($model, $key, $index) {
                  //the close button should be shown in each state except of state == 3
                  return $model->state != 3;
            },
        ],
    ]
    

    You can find more information about ActionColumn and it's callbacks in documentation.