使用附加图标扩展 GridView ActionColumn

Extend GridView ActionColumn with additional icon

我正在使用 Yii2 框架构建一个网络应用程序,它将为用户(登录)提供下载管理员预先上传的文件的能力。

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

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

像这样扩展 template and buttons 的声明:

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

带有 url 的下载图标将添加到现有图标集中。例如,您可以看到默认图标是如何呈现的 here.

一般情况下你甚至不必手动构建link,它会根据按钮名称和模型主键构建,例如/download?id=1

如果你想要不同的 url 特殊的 属性 存在,它被称为 $urlCreator,但你也可以在按钮渲染闭包中更改它,例如:

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

如何在kartik-v yii2 grid action栏中添加另一个action按钮?

刚看到下面example.I添加了复制按钮。

[
    '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'], 
],

模板属性很重要

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}'


        ],
    ],
]);