24

I'm building a webapp with Yii2 framework that will provide users (logged in) the capability to download pre-uploaded files by administrators.

I've created the action actionDownload in the specific controller that call the sendFile() method.

How can I create a button that call the specific action actionDownload on click in a GridView (the list of documents)?

Cimbali
  • 11,012
  • 1
  • 39
  • 68
AleCat83
  • 1,443
  • 3
  • 22
  • 41
  • 1
    Where exactly do you want to place this button? In action column with buttons like view / update / delete? Or in separate column? How the files are downloaded? In archive, separately or one file for each GridView row? Please provide more details. – arogachev Jan 19 '15 at 11:48
  • Hi, thank you for reply. I need to place the button in the action column. Every row in the GridView have one specific file so I need one button for each Grofview row. – AleCat83 Jan 19 '15 at 12:54

3 Answers3

43

Extend declaration of template and buttons like this:

[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{download} {view} {update} {delete}',
    'buttons' => [
        'download' => function ($url) {
            return Html::a(
                '<span class="glyphicon glyphicon-arrow-down"></span>',
                $url, 
                [
                    'title' => 'Download',
                    'data-pjax' => '0',
                ]
            );
        },
    ],
],

The download icon with the url will be added to existing set of icons. You can see for example how default icons are rendered here.

In common case you don't even have to build link manually, it will be constructed based on the button name and model primary key, for example /download?id=1.

In case you want different url special property exists, it's called $urlCreator, but you can also change it right in the button rendering closure, for example:

'download' => function ($url, $model) {
    return Html::a(
        '<span class="glyphicon glyphicon-arrow-download"></span>',
        ['another-controller/anotner-action', 'id' => $model->id], 
        [
            'title' => 'Download',
            'data-pjax' => '0',
        ]
    );
},
arogachev
  • 33,150
  • 7
  • 114
  • 117
  • This works well, just replace glyphicon-arrow-download by glyphicon-download to display the icon. – Ludo Sep 19 '17 at 11:48
2

How to add another action button in kartik-v yii2 grid action column?

Just see following example.I have added copy button.

[
    'class' => 'kartik\grid\ActionColumn',
    'dropdown' => false,
    'vAlign'=>'middle',
    'template' => '{delete} {view} {update} {copy}',
    'urlCreator' => function($action, $model, $key, $index) { 
            return Url::to([$action,'id'=>$key]);
    },
    'buttons'=>[
        'copy' => function ($url, $model, $key) {
            return Html::a('<span class="glyphicon glyphicon-copy"></span>', ['copy', 'id'=>$model->id],['title'=>'Copy']);
        },
    ],      
    'viewOptions'=>['role'=>'modal-remote','title'=>'View','data-toggle'=>'tooltip'],
    'updateOptions'=>['role'=>'modal-remote','title'=>'Update', 'data-toggle'=>'tooltip'],
    'deleteOptions'=>['role'=>'modal-remote','title'=>'Delete', 
                      'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
                      'data-request-method'=>'post',
                      'data-toggle'=>'tooltip',
                      'data-confirm-title'=>'Are you sure?',
                      'data-confirm-message'=>'Are you sure want to delete this item'], 
],
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
2

template attribute is important

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         'id', 
         'title',
        ['class' => 'yii\grid\ActionColumn',
            'buttons' => [
                'additional_icon' => function ($url, $model, $key) {
                    return Html::a ( '<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> ', ['controller/action', 'id' => $model->id] );
                },
            ],
            'template' => '{update} {view} {delete} {additional_icon}'


        ],
    ],
]);
Rafal
  • 573
  • 5
  • 11