用额外的图标扩展GridView ActionColumn [英] Extend GridView ActionColumn with additional icon

查看:120
本文介绍了用额外的图标扩展GridView ActionColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个带有Yii2框架的webapp,它将为用户(已登录)提供下载管理员预先上传的文件的功能。

我创建了在调用 sendFile()方法的特定控制器中的动作 actionDownload



如何在GridView(文档列表)中单击时创建一个调用特定操作 actionDownload 的按钮?

解决方案

扩展模板按钮就像这样:

  [
'class'=> 'yii\grid\ActionColumn',
'template'=> '{download} {view} {update} {delete}',
'buttons'=> [
'download'=>函数($ url){
返回Html :: a(
'< span class =glyphicon glyphicon-arrow-down>< / span>',
$ url,
[
'title'=>'下载',
'data-pjax'=>'0',
]
);
},
],
],

与URL将被添加到现有的一组图标。您可以看到如何呈现默认图标 here
$ b 通常情况下,您甚至不需要手动建立链接,它将基于按钮名称和模型主键构建,例如 / download?id = 1



如果你想要不同的url特殊属性存在,它被称为 $ urlCreator ,但你也可以改变它的权利例如:

 'download'=>函数($ url,$ model){
return Html :: a(
'< span class =glyphicon glyphicon-arrow-download>< / span>',
['another-controller / anotner-action','id'=> $ model-> id],
[
'title'=>'下载',
'数据-pjax'=>'0',
]
);
},


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

解决方案

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',
        ]
    );
},

这篇关于用额外的图标扩展GridView ActionColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆