在yii2中为网格视图添加一个按钮

时间:2016-09-27 05:23:28

标签: php web yii2 php-5.3

我是一名新的yii2开发人员! 我制作了一个GridView,代码如下所示:

<?php Pjax::begin(); ?>    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\ActionColumn'],
            ['class' => 'yii\grid\CheckboxColumn'],
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'countryCode',
            'countryName',
            'currencyCode',
        ],
    ]); ?>
    <?php Pjax::end(); ?>

输出截图: OUTPUT

现在我希望有一个列包含一些按钮,该按钮例如打开一个页面或其他东西! 我的问题是如何创建该列?

3 个答案:

答案 0 :(得分:7)

您也可以将按钮(或任意多个)添加到现有的操作列中,如下所示

<?= GridView::widget([
    ::
    ::
    'columns' => [
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{view} {update} {delete} {myButton}',  // the default buttons + your custom button
            'buttons' => [
                'myButton' => function($url, $model, $key) {     // render your custom button
                    return Html::a(..);
                }
            ]
        ]
        ::
        ::
        'currencyCode'
    ]   
]); ?>

答案 1 :(得分:0)

示例:

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\ActionColumn'],
        ['class' => 'yii\grid\CheckboxColumn'],
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        'countryCode',
        'countryName',
        'currencyCode',
        [
            'label' => 'My Label',
            'format' => 'raw',
            'value' => Html::a('Click me', ['site/index'], ['class' => 'btn btn-success btn-xs', 'data-pjax' => 0])
        ]
    ],
]); ?>
<?php Pjax::end(); ?>

答案 2 :(得分:0)

尝试这种方式:

[
            'header' => 'Button',
            'content' => function($model) {
                return Html::a(..);
            }           
],

More Info