Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Datatable/AbstractDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sg\DatatablesBundle\Datatable\Column\ColumnBuilder;
use Sg\DatatablesBundle\Response\DatatableQueryBuilder;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Routing\RouterInterface;
Expand Down Expand Up @@ -301,6 +302,35 @@ public function getUniqueName()
return $this->getName().($this->getUniqueId() > 1 ? '-'.$this->getUniqueId() : '');
}

/**
* {@inheritdoc}
*/
public function getDisplayTotals()
{
foreach ($this->getColumnBuilder()->getColumns() as $column) {
if ($column->getComputeTotal()) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function computeTotals()
{
if ($this->getDisplayTotals()) {
$datatableQueryBuilder = new DatatableQueryBuilder(array(), $this);
$totals = $datatableQueryBuilder->getComputedTotals();

foreach ($totals as $key => $value) {
$this->getColumnBuilder()->setColumnTotal($key, $value);
}
}
}

//-------------------------------------------------
// Private
//-------------------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions Datatable/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ public function configureOptions(OptionsResolver $resolver)
'data_source' => null,
]);

$resolver->setAllowedTypes('cell_type', ['null', 'string']);
$resolver->setAllowedTypes('class_name', ['null', 'string']);
$resolver->setAllowedTypes('content_padding', ['null', 'string']);
$resolver->setAllowedTypes('dql', ['null', 'string']);
Expand All @@ -380,15 +379,12 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('visible', 'bool');
$resolver->setAllowedTypes('width', ['null', 'string']);
$resolver->setAllowedTypes('add_if', ['null', 'Closure']);
$resolver->setAllowedTypes('join_type', 'string');
$resolver->setAllowedTypes('type_of_field', ['null', 'string']);
$resolver->setAllowedTypes('responsive_priority', ['null', 'int']);
$resolver->setAllowedTypes('sent_in_response', ['bool']);
$resolver->setAllowedTypes('compute_total', ['bool']);
$resolver->setAllowedTypes('mapped', ['bool']);
$resolver->setAllowedTypes('is_association', ['bool']);
$resolver->setAllowedTypes('data_source', ['string', 'null']);

$resolver->setAllowedValues('cell_type', [null, 'th', 'td']);
$resolver->setAllowedValues('join_type', [null, 'join', 'leftJoin', 'innerJoin']);
$resolver->setAllowedValues('type_of_field', array_merge([null], array_keys(DoctrineType::getTypesMap())));
Expand Down Expand Up @@ -1107,7 +1103,7 @@ public function getTotal()
/**
* Set total.
*
* @param $total
* @param mixed $total
*
* @return $this
*/
Expand Down
10 changes: 10 additions & 0 deletions Datatable/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ public function getUniqueColumn($columnType)
return \array_key_exists($columnType, $this->uniqueColumns) ? $this->uniqueColumns[$columnType] : null;
}

public function setColumnTotal($dql, $total)
{
foreach ($this->columns as $column) {
if ($column->getDql() == $dql) {
$column->setTotal($total);
break;
}
}
}

//-------------------------------------------------
// Helper
//-------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions Datatable/DatatableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,18 @@ public function getUniqueId();
* @return string
*/
public function getUniqueName();

/**
* Returns if a total line in the header should appear
*
* @return bool
*/
public function getDisplayTotals();

/**
* Compute totals before rendering the datatable HTML code
*
* @return void
*/
public function computeTotals();
}
2 changes: 2 additions & 0 deletions Resources/doc/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ With 'null' initialized options uses the default value of the DataTables plugin.
| mapped | bool | true | | Allow to create columns not mapped in entity neither defined by DQL. |
| is_association | bool | false | | Allow to force if the column is an association, usally used with `mapped` and `data_source`. |
| data_source | null or string | null | | Specify the column source of the data to manipulate in the current column. Usally used with `mapped` and `data_source`. |
| compute_total | bool | false | | Compute the total of numbers of all the row selected by the query. Totals will be displayed on a header row. |

### Example

Expand Down Expand Up @@ -761,6 +762,7 @@ public function buildDatatable(array $options = array())
}
```


## 10. Link column

Represents a column, with a link in each cell.
Expand Down
13 changes: 13 additions & 0 deletions Resources/views/datatable/datatable_html.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
{% endif %}
{% endfor %}
</tr>
{% if sg_datatables_view.displayTotals %}
<tr>
{% for column in sg_datatables_view.columnBuilder.columns %}
{% if column.sentInResponse %}
<th>
{% if column.computeTotal %}
TOTAL: {{ column.total }}
{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
</thead>
{% if true == individual_filtering %}
{% if 'foot' == sg_datatables_view.options.individualFilteringPosition or 'both' == sg_datatables_view.options.individualFilteringPosition%}
Expand Down
23 changes: 23 additions & 0 deletions Response/DatatableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,29 @@ private function setLimit(QueryBuilder $qb)
return $this;
}

public function getComputedTotals()
{
$qb = clone $this->qb;

$queryString = "";

foreach ($this->columns as $column) {
if ($column->getComputeTotal()) {
$queryString .= 'SUM(' . $this->entityShortName . '.' . $column->getDql() . ') AS ' . $column->getDql() . ' ';
}
}

$qb->select($queryString);
$qb->resetDQLPart('orderBy');
$this->setJoins($qb);

$query = $qb->getQuery();
$query->useQueryCache($this->useCountQueryCache);
call_user_func_array([$query, 'useResultCache'], $this->useCountResultCacheArgs);

return $query->getOneOrNullResult();
}

//-------------------------------------------------
// Private - Helper
//-------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions Twig/DatatableTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public function getFilters()
*/
public function datatablesRender(Twig_Environment $twig, DatatableInterface $datatable)
{
$datatable->computeTotals();

return $twig->render(
'@SgDatatables/datatable/datatable.html.twig',
[
Expand All @@ -119,6 +121,8 @@ public function datatablesRender(Twig_Environment $twig, DatatableInterface $dat
*/
public function datatablesRenderHtml(Twig_Environment $twig, DatatableInterface $datatable)
{
$datatable->computeTotals();

return $twig->render(
'@SgDatatables/datatable/datatable_html.html.twig',
[
Expand Down