2

Is anyone has created custom Report grid in Magento 2 Admin panel?

We have started to create a report but we are facing an issue with display data and while we click on "Refresh" button entire page is messed up.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="reports_report_grid"/>
<body>
    <referenceBlock name="adminhtml.report.grid">
        <arguments>
            <argument name="id" xsi:type="string">gridMaterialReport</argument>
            <argument name="dataSource" xsi:type="object">Commercepundit\CoreAdminOrder\Model\ResourceModel\Material\Collection\Initial</argument>
        </arguments>
    </referenceBlock>
    <referenceBlock name="adminhtml.report.grid.export">
        <arguments>
            <argument name="exportTypes" xsi:type="array">
                <item name="csv" xsi:type="array">
                    <item name="urlPath" xsi:type="string">*/*/exportOrdersCsv</item>
                </item>
                <item name="excel" xsi:type="array">
                    <item name="urlPath" xsi:type="string">*/*/exportOrdersExcel</item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
    <referenceBlock name="adminhtml.report.grid.columnSet">
        <arguments>
            <argument name="count_totals" xsi:type="string">1</argument>
        </arguments>
        <block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.report.grid.columnSet.order_id" as="order_id">
            <arguments>
                <argument name="header" xsi:type="string" translate="true">Order Id</argument>
                <argument name="sortable" xsi:type="string">0</argument>
                <argument name="type" xsi:type="string">text</argument>
                <argument name="index" xsi:type="string">order_id</argument>
                <argument name="id" xsi:type="string">order_id</argument>
                <argument name="column_css_class" xsi:type="string">col-name</argument>
                <argument name="header_css_class" xsi:type="string">col-name</argument>
            </arguments>
        </block>
        <block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.report.grid.columnSet.sqft" as="sqft">
            <arguments>
                <argument name="header" xsi:type="string" translate="true">Sqft</argument>
                <argument name="sortable" xsi:type="string">0</argument>
                <argument name="type" xsi:type="string">number</argument>
                <argument name="index" xsi:type="string">sqft</argument>
                <argument name="total" xsi:type="string">sum</argument>
                <argument name="id" xsi:type="string">sqft</argument>
                <argument name="column_css_class" xsi:type="string">col-qty</argument>
                <argument name="header_css_class" xsi:type="string">col-qty</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

+++++++++++++++++

enter image description here

While we can click on Refresh entire page is look like below.

enter image description here

Can anyone help me on this??

Thanks,

6
  • can you remove <arguments> <argument name="count_totals" xsi:type="string">1</argument> </arguments> Commented Dec 7, 2018 at 13:14
  • @PritamInfo24: I have resolved that issue, there was a collection issue in the Resource model I have added _joinFields and setDateRange function. now, i am getting "ReportJsObject is not defined". Please find link for more information: prnt.sc/lrz56b Do you have any idea?? Commented Dec 7, 2018 at 13:36
  • Did you use $this->getSelect() in your collection class to check the select query is ok or not. Commented Dec 7, 2018 at 13:50
  • Yes it's working fine. $this->getSelect()->addFieldToFilter( 'created_at', ['from' => $fromDate, 'to' => $toDate, 'datetime' => true] ); Commented Dec 7, 2018 at 13:51
  • Can you try this $this->getSelect(); $this->addAttributeToFilter( 'created_at', ['from' => $fromDate, 'to' => $toDate, 'datetime' => true] ); Commented Dec 7, 2018 at 14:08

1 Answer 1

2

I have resolved an issue for Report Grid.

I have added below function in the Resource model

/**
 * Join fields
 *
 * @param string $fromDate
 * @param string $toDate
 * @return $this
 */
protected function _joinFields($fromDate = '', $toDate = '')
{
    $this->addFieldToFilter(
        'created_at',
        ['from' => $fromDate, 'to' => $toDate, 'datetime' => true]
    );
    //echo $this->getSelect()->__toString();
    return $this;
}

/**
 * Set date range
 *
 * @param string $fromDate
 * @param string $toDate
 * @return $this
 */
public function setDateRange($fromDate, $toDate)
{
    $this->_reset()->_joinFields($fromDate, $toDate);
    return $this;
}

/**
 * Set store filter to collection
 *
 * @param array $storeIds
 * @return $this
 */
public function setStoreIds($storeIds)
{
    if ($storeIds) {
        $this->addFieldToFilter('store_id', ['in' => (array)$storeIds]);
    } 
    return $this;
}

now its working fine :)

2
  • how to use for <update handle="reports_sales"/>? Commented Feb 17, 2020 at 13:30
  • can you share the full code, i am facing the same issue. Thanks Commented Mar 28, 2020 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.