Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

yii2 - Extend GridView ActionColumn with additional icon

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Extend declaration of template and buttons like this:

[
    'class' => 'yiigridActionColumn',
    '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',
        ]
    );
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...