1

How we can display sales_order grid in custom module with only pending status.

For now I display sales_order_grid using ui component. I only want to display pending orders in this grid

2 Answers 2

1

Using This link you can create Admin grid. Click here Or You can Download Module and modify it.

You can change You table name and primary key from below file.

Emipro/Custom/Model/ResourceModel

Job.php

<?php

namespace Emipro\Custom\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Job extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('Table_Name', 'Primary_Key_Name');
    }
}

After In emipro_custom_job_listing.xml file at Emipro/Custom/view/adminhtml/ui_component you need to change column name also.

Now your Grid is ready now you need only modify one file which is give below:

Update Collection.php at Emipro/Custom/Model/ResourceModel/Job/Grid

<?php

namespace Emipro\Custom\Model\ResourceModel\Job\Grid;

use Emipro\Custom\Model\ResourceModel\Job\Collection as JobCollection;
use Magento\Framework\View\Element\UiComponent\DataProvider\Document as JobModel;

class Collection extends JobCollection implements \Magento\Framework\Api\Search\SearchResultInterface
{
    protected $aggregations;

    // @codingStandardsIgnoreStart
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = JobModel::class,
        $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }
    // @codingStandardsIgnoreEnd

    public function getAggregations()
    {
        return $this->aggregations;
    }
    public function setAggregations($aggregations)
    {
        $this->aggregations = $aggregations;
    }
    public function getAllIds($limit = null, $offset = null)
    {
        return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
    }
    
    public function getSearchCriteria()
    {
        return null;
    }
    
    public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
    {
        return $this;
    }
    
    public function getTotalCount()
    {
        return $this->getSize();
    }
    
    public function setTotalCount($totalCount)
    {
        return $this;
    }
    
    public function setItems(array $items = null)
    {
        return $this;
    }

    public function _beforeLoad()
    {
        parent::_beforeLoad();
        $this->addFieldToFilter("status", "pending");
        return $this;
    }
}

I Hope This Helps You.

6
  • Thanks for your suggestion but for now I create custom uicomponent but getting all orders. So is there a way to add condition in custom ResoureModel ? I have created custom ResourceModel and call core sales_order_grid table Apr 7, 2021 at 7:30
  • you can refere my above module and update file it's works. Emipro/Custom/Model/ResourceModel/Job/Grid/Collection.php file are add into di.xml also please check my module.
    – Msquare
    Apr 7, 2021 at 7:31
  • IN _beforeLoad method is responsible for filter you grid data.
    – Msquare
    Apr 7, 2021 at 7:32
  • Not filtering status=pending, get all the table data Apr 7, 2021 at 8:50
  • Please share your di.xml and collection.php file.
    – Msquare
    Apr 7, 2021 at 9:22
1

Add filter in ui component dataSource tag uicomponent.xml

<dataSource name="vtn_unexportedorder_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
            <argument name="name" xsi:type="string">vtn_unexportedorder_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="filter_url_params" xsi:type="array">
                        <item name="status" xsi:type="string">pending</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>

In above code added filter_url_params and apply filter where status equals to pending

<item name="filter_url_params" xsi:type="array">
   <item name="status" xsi:type="string">pending</item>
</item>

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.