Yii2:得到总和在gridview的页脚

时间:2016-11-16 10:11:48

标签: gridview yii yii2 sum yii2-advanced-app

我是新来的,我无法发表评论here

当我尝试在页脚中获得总和时,我遇到了问题。

我在控制器中的代码:

$searchModel = new ReceiptsSearch();
$sum = new ReceiptsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 return $this->render('index', [
   'searchModel' => $searchModel,
   'dataProvider' => $dataProvider,
   'sum'=>$sum,
   ]);

我的SearchModel代码:

public function search($params)
{
    $query = Receipts::find();
    $sum = $query->sum('price');
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }
    $query->joinWith('patient'); 
    $query->andFilterWhere([
       'id' => $this->id,
       'price' => $this->price,
       'reg_date' => $this->reg_date,
    ]);
    $query->andFilterWhere(['like','patient.patient_name',$this->patient_id]);

    return $dataProvider;$sum;
}

我的观看页面

<?= GridView::widget([

    'dataProvider' => $dataProvider,$sum,

    'filterModel' => $searchModel,
    'showFooter' => true,
    'columns' => [

        ['class' => 'yii\grid\SerialColumn'],

        [
        'attribute'=>'patient_id',
        'value'=>'patient.patient_name'
        ],
        'price',
        ],
        [
        'attribute' => 'sum',
        'footer' => 'sum',
        ],
        ['class' => 'yii\grid\ActionColumn'],
    ],
    ]); 
?>

显示的信息是:

  

设置未知属性:yii \ grid \ GridView :: 0

2 个答案:

答案 0 :(得分:1)

您可以使用use kartik\grid\GridView;达到相同的效果,而无需使用辅助功能。

只需将'showPageSummary' => true添加到GridView配置,将pageSummary' => true添加到需要求和的列。

查看

use kartik\grid\GridView;

echo GridView::widget([
       'dataProvider' => $dataProvider,
       'filterModel' => $searchModel,
       'showPageSummary' => true,
       'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'patient_id',
                'value' => 'patient.patient_name'
            ],
            [
                'attribute' => 'price',
                'pageSummary' => true
            ],
            ['class' => 'yii\grid\ActionColumn'],
       ],
]); ?>

答案 1 :(得分:0)

页脚通常用于列的总结果(不是用于分隔列)

假设$sum是您想要的值,请参阅下面的列价格

 <?= GridView::widget([
  'dataProvider' => $dataProvider
  'filterModel' => $searchModel,
  'showFooter' => true,
  'columns' => [
      ['class' => 'yii\grid\SerialColumn'],
      [
        'attribute'=>'patient_id',
        'value'=>'patient.patient_name'
      ],
      [
        'attribute'=>'price',
        'footer' => $sum,       
      ],
      ['class' => 'yii\grid\ActionColumn'],
    ],
  ]); 
?>

但是看看你的代码你有几个错误..

在搜索中这是错误的

 return $dataProvider;$sum;

您只能返回一个值

  return $dataProvider;

在你的控制器中这是错误的

 $searchModel = new ReceiptsSearch();
 $sum = new ReceiptsSearch();

$ sum的结果与$ searcModel的结果相同,$ sum不返回新值

你应该以这种方式执行$ sum的查询

$sum = Receipts::find()->sum('price');



$searchModel = new ReceiptsSearch();
$sum = Receipts::find()->sum('price');
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
  'searchModel' => $searchModel,
  'dataProvider' => $dataProvider,
   'sum'=>$sum,
  ]);