问题
After filtering for a particular criteria, display total of the sales transaction amounts on top of the page. This should be total of all the pages if pages are more than one.
Can someone guide me how to do this
回答1:
We did it in the following manner and it is working like a charm
Step 1 : Added two methods in orderAdmin
public function getTemplate($name)
{
if($name == 'list') {
return 'AppBundle:OrderAdmin:list.html.twig';
}
return parent::getTemplate($name); stub
}
public function getSumOf($field)
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('SUM( ' . $query->getRootAlias() . '.' . $field . ') as total');
$query->setFirstResult(null);
$query->setMaxResults(null);
$result = $query->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);
return $result;
}
Step 2: Created a template file list.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
{% block list_header %}
<div class="pull-right" style="margin-right: 10px;">
<label for="{{ admin.uniqid }}_sum_of_orders" class="control-label">Grand Total: </label>
<label class="control-label"> S$ {{ admin.getSumOf('orderAmount') }} </label>
</div>
{% endblock %}
来源:https://stackoverflow.com/questions/37537300/show-total-amount-of-orders-on-the-top-of-a-sonata-admin-list