第九区

一个程序员的经验笔记

Yii2自定义表单按钮

| 暂无评论

Yii2的GridView里有时需要自定义操作按钮,比如对目录的操作,如果该目录有子目录,则不显示删除按钮,以下代码对此进行了演示:

[
    'class' => 'yii\grid\ActionColumn',
    'header' => '操作',
    'buttons' => [
        'delete' =>
        function ($url, $model, $key) {
            if ($model->has_child) {
                return '';//如果有子目录不显示删除按钮 
            } else {
                $options = array_merge(
    [
        'title' => Yii::t('yii', 'Delete'),
        'aria-label' => Yii::t('yii', 'Delete'),
        'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
        'data-method' => 'post',
        'data-pjax' => '0',
    ]
                );
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
            }
        }
            ]
        ]

 

 

发表评论

*为必填字段!