Answer the question
In order to leave comments, you need to log in
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>
<?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);;
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question