My VALUE column is:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'footer' => ???
],
How use totals of rows on FOOTER property ?
My VALUE column is:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'footer' => ???
],
How use totals of rows on FOOTER property ?
it's works 1. create class then 2.create column array, 3.configure column, 4.configure grid
namespace app\components;
class PTotal {
public static function pageTotal($provider, $fieldName)
{
$total=0;
foreach($provider as $item){
$total+=$item[$fieldName];
}
return $total;
}
$provider = new ActiveDataProvider([
'query' => $query,
'sort' => $sort,
]);
$grid_columns=[
[
'attribute' => 'saldo_in',
'footer'=>PTotal::pageTotal($provider->models,'saldo_in'),
]
]
echo GridView::widget([
'dataProvider' => $provider,
'showFooter'=>TRUE,
'footerRowOptions'=>['style'=>'font-weight:bold;text-decoration: underline;'],
'columns' =>$grid_columns,
]);
in the view file above calculate the footer sum by following code
$amount = 0;
if (!empty($dataProvider->getModels())) {
foreach ($dataProvider->getModels() as $key => $val) {
$amount += $val->amount;
}
}
now in your gridview use just pass the amount variable as follow
[
'attribute' => 'amount', 'label' => 'Charged Amount',
'value' => function ($model, $key, $index, $widget) {
return Yii::$app->formatter->asCurrency($model->amount, 'INR');
},
'footer' => $amount,
],
i have tried it will work...
I have not tried this but try defining the column like this.
On the top of the file define
$total = 0;
Afterwards define the column like this:
[
'attribute' => 'value',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'value'=>function ($model, $key, $index, $widget) use ($total) {
$total += $model->value;
return $model->value;
},
'footer' => function () use ($total)
{
//format the total here
return $total;
},
],
Now there are several problems with this as they are with the http://demos.krajee.com/grid meaning it will only add what it is shown, if you have pagination it will just show the total on that page.
If you want a total for all the records you should add it by hand using the dataprovider without pagination.
Again I have not really tried this, just give it a shot.
i try as you say but show me the error: trim() expects parameter 1 to be string, object given
protected function renderFooterCellContent()
{
return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
}
In Yii 1 works fine, i just create this function in Model
public function getTotals($ids)
{
if($ids){
$ids = implode(",",$ids);
$connection=Yii::app()->db;
$command=$connection->createCommand("SELECT SUM(value) FROM `tb_cashbook` where id in ($ids)");
$amount = $command->queryScalar();
return "R$ ".Yii::app()->format->formatNumber($amount);
}
else
return null;
}
And in View, this way in footer property:
'footer'=>"<strong>".$model->getTotals($model->search()->getKeys())." </strong>",