【问题标题】:Yii2 Gridview: Which dataProvider am I currently using?Yii2 Gridview:我目前使用的是哪个 dataProvider?
【发布时间】:2017-07-12 10:21:10
【问题描述】:

我在同一页面上有两个 gridView 小部件,每个小部件都有自己的数据提供者,如下所示:

<?= GridView::widget([
    'dataProvider' => $userModel,
    'columns' => $columns,
]); ?>

<?= GridView::widget([
    'dataProvider' => $companyModel,
    'columns' => $columns,
]); ?>

两个视图中的列大体相似,但前三列存在一些差异。我想做的是确定当前正在编译哪个小部件,以便我可以为该小部件动态创建适当的列。例如:

<?php     
$columns=[];

// here is where the problem is. I would like to do something along these lines:  
if(GridView->dataProvider->id == 'userModel')
  {    
     // add custom columns    
     $columns[] = 'userName'; 
  } else {    
     // etc... 
  }

// common columns used by both grids here 
$columns[] = [
    'totalCount',
    'totalCost',
 ];

非常感谢任何帮助。

【问题讨论】:

    标签: gridview yii2 dataprovider


    【解决方案1】:

    解决了。

    首先创建您的常用列:

    // common columns used by both grids here 
    $columns = [];
    $columns[] = [
        'totalCount',
        'totalCost',
     ];
    

    然后按如下方式创建并合并自定义列:

     // user specific columns
    $userColumns = [];
    $userColumns[] = //... 
    // now merge with common columns
    $userColumns = array_merge($userColumns,$columns);
    

    现在从网格小部件调用相应的列:

    <?= GridView::widget([
        'dataProvider' => $userModel,
        'columns' => $userColumns,
    ]); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多