使用附加图标扩展 GridView ActionColumn [英] Extend GridView ActionColumn with additional icon

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

问题描述

我正在使用 Yii2 框架构建一个 web 应用程序,该应用程序将为用户(已登录)提供下载管理员预先上传的文件的功能.

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

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

解决方案

模板按钮 像这样:

<预><代码>['类' =>'yiigridActionColumn','模板' =>'{下载} {查看} {更新} {删除}','按钮' =>['下载' =>函数($url){返回 HTML::a('<span class="glyphicon glyphicon-arrow-down"></span>',$网址,['标题' =>'下载','数据-pjax' =>'0',]);},],],

带有 url 的下载图标将被添加到现有的图标集中.例如,您可以在here.

通常情况下,您甚至不必手动构建链接,它会根据按钮名称和模型主键构建,例如 /download?id=1.

如果您希望存在不同的 url 特殊属性,则称为 $urlCreator,但你也可以直接在按钮渲染闭包中更改它,例如:

'下载' =>函数($url,$model){返回 HTML::a('<span class="glyphicon glyphicon-arrow-download"></span>',['another-controller/anotner-action', 'id' =>$model->id],['标题' =>'下载','数据-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' => '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',
        ]
    );
},

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

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