gpt4 book ai didi

gridview - Yii2 : Get Sum In footer of gridview

转载 作者:行者123 更新时间:2023-12-03 22:58:04 25 4
gpt4 key购买 nike

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

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

我在 Controller 中的代码:

$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'],
],
]);
?>

显示的消息是:

Setting unknown property: yii\grid\GridView::0

最佳答案

Controller

$searchModel = new ReceiptsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);

搜索型号
public function search($params)
{
$query = Receipts::find()->joinWith('patient');

$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

$this->load($params);

if (!$this->validate()) {
return $dataProvider;
}

$query->andFilterWhere([
'id' => $this->id,
'price' => $this->price,
'reg_date' => $this->reg_date,
]);

$query->andFilterWhere(['like','patient.patient_name',$this->patient_id]);

return $dataProvider;
}

查看
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showFooter' => true,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

[
'attribute' => 'patient_id',
'value' => 'patient.patient_name'
],
[
'attribute' => 'price',
'footer' => Receipts::getTotal($dataProvider->models, 'price'),
],

['class' => 'yii\grid\ActionColumn'],
],
]); ?>

收据型号
public static function getTotal($provider, $fieldName)
{
$total = 0;

foreach ($provider as $item) {
$total += $item[$fieldName];
}

return $total;
}

关于gridview - Yii2 : Get Sum In footer of gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40629352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com