在Kartik中添加自定义按钮Yii2 Kartik DetailView

时间:2017-11-13 23:35:36

标签: yii2 detailview kartik-v

我已经搜索并尝试了每个论坛和示例。

我在Yii2中使用Kartik \ DetailViwew并且我无法在buttons1选项中放置一个自定义按钮。

我正在处理的代码:

    echo DetailView::widget([
        'model' => $model,
        'attributes' => $attributes,
        'mode'=>Yii::$app->request->get('edit')=='t' ? DetailView::MODE_EDIT : DetailView::MODE_VIEW,
            'panel'=>[
            'heading'=>$this->title,
            'type'=>DetailView::TYPE_INFO,
            ],
        'buttons1' => '{update}',
        'bordered' => 'true',
        'striped' => $striped,
        'condensed' => $condensed,
        'responsive' => $responsive,
        'hover' => $hover,
        'hAlign'=>$hAlign,
        'vAlign'=>$vAlign,
        'fadeDelay'=>$fadeDelay,
        'deleteOptions'=>[ // your ajax delete parameters
          'params' => ['id' => $model->p_id, 'custom_param' => true],
          'url'=>['delete', 'id' => $model->p_id],
        ]

    ]);

        'buttons1' => '{update}',

根据示例http://demos.krajee.com/detail-view,有一种自定义方法。 但没有例子。并且文档没有解释如何执行此操作。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

经过研究和一些尝试,我通过这种方式做到了,你必须启用编辑模式(显示按钮),并修改defatul模板({buttons} {title}),因为这个模板被替换,如果模板存在于vender grideview常量中,只需注入代码即可添加任何你想要的按钮。不是严格的解决方案,而是一种运作良好的解决方法

<?php
$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
                   'title' => 'Eliminar',
                   'class' => 'pull-right detail-button',
                   'data' => [
                       'confirm' => '¿Realmente deseas eliminar este elemento?',            
                       'method' => 'post',
                   ]
           ]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
                   'title' => 'Actualizar',
                   'class' => 'pull-right detail-button',
           ]);
?>
<?=
DetailView::widget([
                   'model' => $model,
                   'hover' => true,
                   'hideAlerts' => true,
                   'enableEditMode' => true,
                   'mode' => DetailView::MODE_VIEW,
                   'hAlign' => 'left',
                   'panel' => [
                       'heading' => '&nbsp',
                       'type' => DetailView::TYPE_DEFAULT,
                       'headingOptions' => [
                           'template' => "$button1 $button2 {title}"
                       ]
                   ],
                   'attributes' => [
                       'id',
                       'identificacion',
                       'nombre',    
                   ],
         ])
?>

答案 1 :(得分:0)

尝试这样:

'buttons1' => Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
                   'title' => 'Eliminar',
                   'class' => 'pull-right detail-button',
                   'data' => [
                       'confirm' => '¿Realmente deseas eliminar este elemento?',            
                       'method' => 'post',
                   ]
           ]),

如果您有多个按钮,请尝试以下操作:

$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
                   'title' => 'Eliminar',
                   'class' => 'pull-right detail-button',
                   'data' => [
                       'confirm' => '¿Realmente deseas eliminar este elemento?',            
                       'method' => 'post',
                   ]
           ]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
                   'title' => 'Actualizar',
                   'class' => 'pull-right detail-button',
           ]);


    'buttons1' => "{$button1} {$button2}",