Issue
I'm new here that I can't comment here
and I have a problem when I try to Get sum in the footer .
my code in controller :
$searchModel = new ReceiptsSearch();
$sum = new ReceiptsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'sum'=>$sum,
]);
my SearchModel Code :
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;
}
my view page
<?= 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'],
],
]);
?>
the message shown is :
Setting unknown property: yii\grid\GridView::0
Solution
Controller
$searchModel = new ReceiptsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
SearchModel
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;
}
View
<?= 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'],
],
]); ?>
Receipts Model
public static function getTotal($provider, $fieldName)
{
$total = 0;
foreach ($provider as $item) {
$total += $item[$fieldName];
}
return $total;
}
Answered By - Insane Skull
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.