Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 7244a65

Browse files
Add compute_total parameter to create a header row with totals of defined columns
1 parent 593c3a2 commit 7244a65

File tree

7 files changed

+94
-4
lines changed

7 files changed

+94
-4
lines changed

Datatable/AbstractDatatable.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\EntityManagerInterface;
1515
use Exception;
1616
use Sg\DatatablesBundle\Datatable\Column\ColumnBuilder;
17+
use Sg\DatatablesBundle\Response\DatatableQueryBuilder;
1718
use Symfony\Component\PropertyAccess\PropertyAccess;
1819
use Symfony\Component\PropertyAccess\PropertyAccessor;
1920
use Symfony\Component\Routing\RouterInterface;
@@ -301,6 +302,35 @@ public function getUniqueName()
301302
return $this->getName().($this->getUniqueId() > 1 ? '-'.$this->getUniqueId() : '');
302303
}
303304

305+
/**
306+
* {@inheritdoc}
307+
*/
308+
public function getDisplayTotals()
309+
{
310+
foreach ($this->getColumnBuilder()->getColumns() as $column) {
311+
if ($column->getComputeTotal()) {
312+
return true;
313+
}
314+
}
315+
316+
return false;
317+
}
318+
319+
/**
320+
* {@inheritdoc}
321+
*/
322+
public function computeTotals()
323+
{
324+
if ($this->getDisplayTotals()) {
325+
$datatableQueryBuilder = new DatatableQueryBuilder(array(), $this);
326+
$totals = $datatableQueryBuilder->getComputedTotals();
327+
328+
foreach ($totals as $key => $value) {
329+
$this->getColumnBuilder()->setColumnTotal($key, $value);
330+
}
331+
}
332+
}
333+
304334
//-------------------------------------------------
305335
// Private
306336
//-------------------------------------------------

Datatable/Column/AbstractColumn.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ public function configureOptions(OptionsResolver $resolver)
365365
'data_source' => null,
366366
]);
367367

368-
$resolver->setAllowedTypes('cell_type', ['null', 'string']);
369368
$resolver->setAllowedTypes('class_name', ['null', 'string']);
370369
$resolver->setAllowedTypes('content_padding', ['null', 'string']);
371370
$resolver->setAllowedTypes('dql', ['null', 'string']);
@@ -380,15 +379,12 @@ public function configureOptions(OptionsResolver $resolver)
380379
$resolver->setAllowedTypes('visible', 'bool');
381380
$resolver->setAllowedTypes('width', ['null', 'string']);
382381
$resolver->setAllowedTypes('add_if', ['null', 'Closure']);
383-
$resolver->setAllowedTypes('join_type', 'string');
384-
$resolver->setAllowedTypes('type_of_field', ['null', 'string']);
385382
$resolver->setAllowedTypes('responsive_priority', ['null', 'int']);
386383
$resolver->setAllowedTypes('sent_in_response', ['bool']);
387384
$resolver->setAllowedTypes('compute_total', ['bool']);
388385
$resolver->setAllowedTypes('mapped', ['bool']);
389386
$resolver->setAllowedTypes('is_association', ['bool']);
390387
$resolver->setAllowedTypes('data_source', ['string', 'null']);
391-
392388
$resolver->setAllowedValues('cell_type', [null, 'th', 'td']);
393389
$resolver->setAllowedValues('join_type', [null, 'join', 'leftJoin', 'innerJoin']);
394390
$resolver->setAllowedValues('type_of_field', array_merge([null], array_keys(DoctrineType::getTypesMap())));

Datatable/Column/ColumnBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ public function getUniqueColumn($columnType)
185185
return \array_key_exists($columnType, $this->uniqueColumns) ? $this->uniqueColumns[$columnType] : null;
186186
}
187187

188+
public function setColumnTotal($dql, $total)
189+
{
190+
foreach ($this->columns as $column) {
191+
if ($column->getDql() == $dql) {
192+
$column->setTotal($total);
193+
break;
194+
}
195+
}
196+
}
197+
188198
//-------------------------------------------------
189199
// Helper
190200
//-------------------------------------------------

Datatable/DatatableInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,18 @@ public function getUniqueId();
132132
* @return string
133133
*/
134134
public function getUniqueName();
135+
136+
/**
137+
* Returns if a total line in the header should appear
138+
*
139+
* @return bool
140+
*/
141+
public function getDisplayTotals();
142+
143+
/**
144+
* Compute totals before rendering the datatable HTML code
145+
*
146+
* @return void
147+
*/
148+
public function computeTotals();
135149
}

Resources/views/datatable/datatable_html.html.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@
4545
{% endif %}
4646
{% endfor %}
4747
</tr>
48+
{% if sg_datatables_view.displayTotals %}
49+
<tr>
50+
{% for column in sg_datatables_view.columnBuilder.columns %}
51+
{% if column.sentInResponse %}
52+
<th>
53+
{% if column.computeTotal %}
54+
TOTAL: {{ column.total }}
55+
{% endif %}
56+
</th>
57+
{% endif %}
58+
{% endfor %}
59+
</tr>
60+
{% endif %}
4861
</thead>
4962
{% if true == individual_filtering %}
5063
{% if 'foot' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%}

Response/DatatableQueryBuilder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,29 @@ private function setLimit(QueryBuilder $qb)
598598
return $this;
599599
}
600600

601+
public function getComputedTotals()
602+
{
603+
$qb = clone $this->qb;
604+
605+
$queryString = "";
606+
607+
foreach ($this->columns as $column) {
608+
if ($column->getComputeTotal()) {
609+
$queryString .= 'SUM(' . $this->entityShortName . '.' . $column->getDql() . ') AS ' . $column->getDql() . ' ';
610+
}
611+
}
612+
613+
$qb->select($queryString);
614+
$qb->resetDQLPart('orderBy');
615+
$this->setJoins($qb);
616+
617+
$query = $qb->getQuery();
618+
$query->useQueryCache($this->useCountQueryCache);
619+
call_user_func_array([$query, 'useResultCache'], $this->useCountResultCacheArgs);
620+
621+
return $query->getOneOrNullResult();
622+
}
623+
601624
//-------------------------------------------------
602625
// Private - Helper
603626
//-------------------------------------------------

Twig/DatatableTwigExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function getFilters()
104104
*/
105105
public function datatablesRender(Twig_Environment $twig, DatatableInterface $datatable)
106106
{
107+
$datatable->computeTotals();
108+
107109
return $twig->render(
108110
'@SgDatatables/datatable/datatable.html.twig',
109111
[
@@ -119,6 +121,8 @@ public function datatablesRender(Twig_Environment $twig, DatatableInterface $dat
119121
*/
120122
public function datatablesRenderHtml(Twig_Environment $twig, DatatableInterface $datatable)
121123
{
124+
$datatable->computeTotals();
125+
122126
return $twig->render(
123127
'@SgDatatables/datatable/datatable_html.html.twig',
124128
[

0 commit comments

Comments
 (0)