Skip to content

Instantly share code, notes, and snippets.

@proditis
Last active November 2, 2019 10:14
Show Gist options
  • Save proditis/009d5e51ec914dbc7dccb59187da1590 to your computer and use it in GitHub Desktop.
Save proditis/009d5e51ec914dbc7dccb59187da1590 to your computer and use it in GitHub Desktop.
Yii2 GridView actionColumn customizations

The following snippet modifies

  • the default delete action confirmation message
  • adds 4 new actions (generate-ssl, toggle-academic, ban, mail) to the action column
  • adds a custom header action "ban-filtered" which submits the GridView filter data
            [
              'class' => 'yii\grid\ActionColumn',
              'template' => {generate-ssl} {toggle-academic} {view} {update} {delete} {ban} {mail}',
              'header' => Html::a(
                  '<span class="glyphicon glyphicon-ban-circle"></span>',
                  ['ban-filtered'],
                  [
                      'title' => 'Mass Delete and ban users',
                      'data-pjax' => '0',
                      'data-method' => 'POST',
                      'data'=>[
                        'method'=>'post',
                        'params'=> $searchModel->attributes,
                        'confirm'=>'Are you sure you want to delete and ban the currently visible users?',
                      ],
                  ]
              ),
              'buttons' => [
                  'delete' => function($url, $model){
                      return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['delete', 'id' => $model->id], [
                          'class' => '',
                          'data' => [
                              'confirm' => 'Are you absolutely sure you want to delete ['.Html::encode($model->username).'] ?',
                              'method' => 'post',
                          ],
                      ]);
                  },
                  'generate-ssl' => function ($url) {
                      return Html::a(
                          '<span class="glyphicon glyphicon-lock"></span>',
                          $url,
                          [
                              'title' => 'Generate SSL Certificates',
                              'data-pjax' => '0',
                              'data-method' => 'POST',
                              'data'=>['confirm'=>"Are you sure you want to regenerate the SSL for this user?\nNOTE: No revoke takes place on the previous key."]
                          ]
                      );
                  },
                  'toggle-academic' => function ($url) {
                      return Html::a(
                          '<span class="glyphicon glyphicon-education"></span>',
                          $url,
                          [
                              'title' => 'Toggle user academic flag',
                              'data-pjax' => '0',
                              'data-method' => 'POST',
                              'data'=>['confirm'=>'Are you sure you want to toggle the academic flag for this user?']

                          ]
                      );
                  },
                  'ban' => function ($url) {
                      return Html::a(
                          '<span class="glyphicon glyphicon-ban-circle"></span>',
                          $url,
                          [
                              'title' => 'Delete and ban this user',
                              'data-pjax' => '0',
                              'data-method' => 'POST',
                              'data'=>['confirm'=>'Are you sure you want to delete and ban this user?']
                          ]
                      );
                  },
                  'mail' => function ($url) {
                      return Html::a(
                          '<span class="glyphicon glyphicon-envelope"></span>',
                          $url,
                          [
                              'title' => 'Mail this user activation',
                              'data-pjax' => '0',
                              'data-method' => 'POST',
                              'data'=>['confirm'=>'Are you sure you want to mail this user his activation URL?']
                          ]
                      );
                  },
              ],
            ],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment