S
S
Silverviql2018-04-23 11:37:57
Yii
Silverviql, 2018-04-23 11:37:57

How to make a filter in GridView to display the received revenue for a date?

I currently display a table with the sum of all displayed elements on the page. That is, if I display 'pageSize' => 20, then I will get the amount of revenue from 20 displayed items. How to withdraw the amount for one date? I'm still not well versed in yii2, if someone offers an example, I will be grateful.
view.

<?php

/* @var $this yii\web\View */

use yii\helpers\Html;
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
use frontend\models\NumberColumn;


$dataProvider = new ActiveDataProvider([
    'query' => \app\models\Zakaz::find(),
    'pagination' => [
        'pageSize' => 20,
    ],
]);

$this->title = 'Аналитика';
$this->params['breadcrumbs'][] = $this->title;

print_r ($arrayInView);

?>
<div class="site-about">
    <h1><?= Html::encode($this->title) ?></h1>
<?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'showFooter' => true,
    'columns' => [
        'id_zakaz',
         [
        'attribute' => 'data',
        'format' =>  ['date', ' dd.MM.YYYY'],
        'options' => ['width' => '200']
         ],
        [
            'class' => NumberColumn::className(),
            'attribute' => 'fact_oplata',
        ],
    ],
]);?>


</div>

NumberColumn - to get the sum
<?php

namespace frontend\models;

use yii\grid\DataColumn;

class NumberColumn extends DataColumn
{
    private $_total = 0;

    public function getDataCellValue($model, $key, $index)
    {
        $value = parent::getDataCellValue($model, $key, $index);
        $this->_total += $value;
return $value;
}

protected function renderFooterCellContent()
{
    return $this->grid->formatter->format($this->_total, $this->format);;
}
}

Here's what I get.
5add9acfe23c4328031469.png
Here is what I would like to get for each date
5add9ae418e51807148184.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-04-23
@Silverviql

I think it’s worth making your own dataProvider, inheriting from the standard activeDataProvider and implementing methods in it that will take the amount with a separate request. Something like:

public $sumQuery = MyModel::find();

public function getSum($col){
return $this->sumQuery->sum($col);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question