【问题标题】:Yii2 Insert if else statement inside the dataGrid viewYii2 在 dataGrid 视图中插入 if else 语句
【发布时间】:2017-06-20 05:07:20
【问题描述】:

我使用 yii2 框架创建了一个网站。我也使用 gii 生成器作为我的 crud。现在我安装了一个扩展kartik gridview。现在我想要在我想创建一个 if else 语句的 kartikGridview 里面,但我得到了错误。是的,我可以做到,但在 kartik 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 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}',
],

【问题讨论】:

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


    【解决方案1】:

    预先构造$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}',
    ]);
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多