show total amount of orders on the top of a sonata admin list

浪子不回头ぞ 提交于 2019-12-13 08:58:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!