Yii2在dataGrid视图中插入if else语句

时间:2017-06-20 05:07:21

标签: php if-statement gridview yii2-advanced-app

我使用yii2框架创建了一个网站。我也用gii发电机为我的crud。现在我安装了扩展kartik gridview。现在我想要在kartikGridview里面我想创建一个if else语句,但我得到了错误。是的我能做到但是在kartik gridview之外。有什么建议吗? 这是我的index-gridview:

<?=  GridView::widget([
'id'=>'kv-grid-demo',
'dataProvider'=>$dataProvider,
'filterModel'=>$searchModel,
'columns'=> [
        // ['class' => 'yii\grid\SerialColumn'],
        // 'report_id',
        'reference_no',
        'subject',
            [
              'attribute' => 'doc_date',
              'filter'=>DatePicker::widget([
              'model' => $searchModel,
              'attribute' => 'doc_date',
              'removeButton' => false,
               'pluginOptions' => [
                    'autoclose'=>true,
                    'format' => 'yyyy-mm-dd',
                ],
                ])
            ],

            [
                'attribute' => 'for',
                'value' => 'namefor.fullName',
            ],
            [
                'attribute' => 'from',
                'value' => 'namefrom.fullName',
            ],
            [
            'attribute' => 'drawer_id',

            ],
            'user_id',
            'doc_name',
        // 'doc_file',
    [
    'class'    => 'yii\grid\ActionColumn',

    ],
    ],
'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'pjax'=>true, // pjax is set to always true for this demo
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '<h3 class="panel-title"><i class="fa fa-files-o"></i> Documents</h3>',
'responsive'=>true,
'hover'=>true,
// 'after' => true,
// 'showFooter' => true,
 'showFooter' => true,
],
// set your toolbar

'toolbar'=> [

    // Html::button('<i class="glyphicon glyphicon-plus"></i>', ['type'=>'button', 'title'=>Yii::t('kvgrid', 'Add Book'), 'class'=>'btn btn-success']) . ' '.
    '{export}',
    '{toggleData}',
],

// set export properties
'export'=>[
    'fontAwesome'=>true
],
'exportConfig' => [
     GridView::PDF => [
'filename' => 'Documents',
'config' => [
  'methods' => [
    'SetHeader' => [
      ['odd' => $pdfHeader, 'even' => $pdfHeader]
    ],
    'SetFooter' => [
      ['odd' => $pdfFooter, 'even' => $pdfFooter]
    ],
  ],
  'options' => [
    'title' => 'Documents',
    'subject' => 'Documents',
    'keywords' => 'pdf, Documents, export, other, keywords, here'
  ],
]

如何插入if if else代码?

  <?php
     if(Yii::$app->user->identity->isUser()) 
     {
     }else{  ?>
            <p>
            <?=Html::a('<i class="fa fa-plus"></i>&nbsp;Create',['create'],['class' => 'btn btn-success']) ?>
            </p>
      <?php } ?>

到这条线?

'toolbar'=> [

    '{export}',
    '{toggleData}',
],

1 个答案:

答案 0 :(得分:0)

预先构建$toolbar数组:

$toolbar = [];
// Add dynamic values
if (!Yii::$app->user->identity->isUser()) {
    $toolbar[] = '<p>' . Html::a('<i class="fa fa-plus"></i>&nbsp;Create', ['create'], ['class' => 'btn btn-success']) . '</p>';
}
// Add permanent values
$toolbar = array_merge($toolbar, [
    '{export}',
    '{toggleData}',
]);

并将其传递给toolbar中的GridView选项:

<?= GridView::widget([
    // Other options
    'toolbar' => $toolbar,
    // Other options
]) ?>