Extend GridView ActionColumn with additional icon

前端 未结 3 939
轻奢々
轻奢々 2020-12-05 05:18

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 act

3条回答
  •  醉梦人生
    2020-12-05 05:46

    Extend declaration of template and buttons like this:

    [
        'class' => 'yii\grid\ActionColumn',
        'template' => '{download} {view} {update} {delete}',
        'buttons' => [
            'download' => function ($url) {
                return Html::a(
                    '',
                    $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(
            '',
            ['another-controller/anotner-action', 'id' => $model->id], 
            [
                'title' => 'Download',
                'data-pjax' => '0',
            ]
        );
    },
    

提交回复
热议问题