cancel
Showing results for 
Search instead for 
Did you mean: 

How to filter admin grid collection based on certain condition in Ui component xml file New method

How to filter admin grid collection based on certain condition in Ui component xml file New method

I have a grid which is replicated from the sales_order_grid. I want to show only the processing orders in the grid.So what can be done to filter that?

4 REPLIES 4

Re: How to filter admin grid collection based on certain condition in Ui component xml file New meth

This plugin can work for you
di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
      <plugin name="sales_order_pending" type="Vendor\Module\Plugin\CustomSalesOrderGridCollection" sortOrder="100" />
</type>

CustomSalesOrderGridCollection.php

 <?php namespace Vendor\Module\Plugin;

    use Magento\Framework\Message\ManagerInterface as MessageManager;
    use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as SalesOrderGridCollection;

    class CustomSalesOrderGridCollection
    {
        private $messageManager;
        private $collection;
        protected  $adminSession;
        protected $logger;

        public function __construct(MessageManager $messageManager,
            SalesOrderGridCollection $collection,
            \Magento\Backend\Model\Auth\Session $adminSession,
            \Psr\Log\LoggerInterface $logger
        ) {

            $this->messageManager = $messageManager;
            $this->collection = $collection;
            $this->adminSession = $adminSession;
            $this->logger = $logger;
        }

        public function aroundGetReport(            \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $subject,
            \Closure $proceed,
            $requestName
        ) {
           $current_adminuser =   $this->adminSession->getUser()->getAclRole();
           $this->logger->addDebug("admin user");
           $this->logger->addDebug($current_adminuser);

           if(12 == $current_adminuser){

             $result = $proceed($requestName);
             if ($requestName == 'sales_order_grid_data_source') {
                 if ($result instanceof $this->collection) {

                     $this->collection->addFieldToFilter('status', array('in' => array('pending')));
                 }

             }
           }
            return $this->collection;
        }
    }

After Clear Cache and Login that user, then its will show only pending Order in sale_order_grid.

Further you can change the order status in the file according to your requirement.

Re: How to filter admin grid collection based on certain condition in Ui component xml file New meth

We can add a filterUrlParam to filter the grid.

<dataSource name="grid_record_grid_list_data_source" component="Magento_Ui/js/grid/provider">
<settings>
<updateUrl path="mui/index/render"/>
<filterUrlParams>
<param name="status">processing</param>
</filterUrlParams>
</settings>
<aclResource>Magento_Sales::sales_order</aclResource>
<dataProvider class="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider" name="grid_record_grid_list_data_source">
<settings>
<requestFieldName>id</requestFieldName>
<primaryFieldName>main_table.entity_id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>


In the above code, there is a filterUrlParams tag that is responsible to filter collection. In order to make it work, set parameter name inside name attribute of item tag through which you require to filter.

The alternative method of doing this is:

<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="update_url" xsi:type="url" path="mui/index/render"/>
<item name="filter_url_params" xsi:type="array">
<item name="is_active" xsi:type="string">1</item>
</item>
</item>
</argument>
If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool

Re: How to filter admin grid collection based on certain condition in Ui component xml file New meth

Hey thanks for the super helpful reply. I'm not sure how I missed that thread. I haven't quite mastered the search function on here. I think I'll pass with him this time around. If i want any further guideline we will contact you here https://community.magento.com/t5/forums/replypage/board-id/admin-config/message-teatimeresults-id/7490.

Thanks...

Re: How to filter admin grid collection based on certain condition in Ui component xml file New meth

This website has a lot of entries, however  subway surfers yours caught my quordle attention. I think you ought to include more topics in the future.