2

I need to filter custom table collection in admin grid. I'm struggling with this. Please suggest for this.

Here is my code

namespace Abc\Mkt\Model\ResourceModel\Logs;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection {
    protected function _construct()
    {
        $this->_init(
        'Abc\Mkt\Model\Logs',
        'Abc\Mkt\Model\ResourceModel\Logs'  );
    } 
}

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <virtualType name="AbcMktGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
        <arguments>
            <argument name="appliers" xsi:type="array">
                <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
                <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="AbcMktGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Abc\Mkt\Model\ResourceModel\Logs\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">AbcMktGridFilterPool</argument>
        </arguments>
    </virtualType>
    <virtualType name="Abc\Mkt\Model\ResourceModel\Logs\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">abc_error_logs</argument>
            <argument name="resourceModel" xsi:type="string">Abc\Mkt\Model\ResourceModel\Logs</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="grazitti_mkt_index_listing_data_source" xsi:type="string">Abc\Mkt\Model\ResourceModel\Logs\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Framework\Notification\MessageList">
        <arguments>
            <argument name="messages" xsi:type="array">
                <item name="ExpiryAlert" xsi:type="string">Abc\Mkt\Model\System\Message\ExpiryAlert</item>
            </argument>
        </arguments>
    </type>
</config>

Thanks

12
  • Have you created GRID using ui_component ? Oct 23, 2019 at 10:11
  • yes, I have created
    – Shomita
    Oct 23, 2019 at 10:12
  • You want to sort that by asc/desc position or something else ? Oct 23, 2019 at 10:15
  • I need records three days old from custom table
    – Shomita
    Oct 23, 2019 at 10:24
  • @Shiwani see my answer hope this will help you
    – Waqar Ali
    Oct 24, 2019 at 5:30

1 Answer 1

0

In Your

Abc\Mkt\Model\ResourceModel\Logs\Collection.php

class Collection extends AbstractCollection
{
    protected $_idFieldName = 'entity_id';
    /**
     * Collection constructor
     *
     *
     * @param  EntityFactoryInterface $entityFactory
     * @param  LoggerInterface $logger
     * @param  FetchStrategyInterface $fetchStrategy
     * @param  ManagerInterface $eventManager
     * @param  StoreManagerInterface $storeManager
     * @param  AdapterInterface $connection
     * @param  AbstractDb $resource
     */
    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,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        $this->_init(
            'Abc\Mkt\Model\Logs',
            'Abc\Mkt\Model\ResourceModel\Logs'
        );
        $this->storeManager = $storeManager;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    }
    protected function _initSelect()
    {
        \\ timeOfLastThreeDays
        parent::_initSelect();
        $this->getSelect()->where('main_table.created_time BETWEEN ADDDATE(NOW(),-3) AND NOW() ');
    }
}

Here is di.xml Your can compare your One with mine

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="frontname_controller_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Module\Grid\Collection</item> <!-- Collection path -->
            </argument>
        </arguments>
    </type>
    <type name="Vendor\Module\Model\ResourceModel\Module\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">tablename</argument>
            <argument name="eventPrefix" xsi:type="string">vendor_module_categoryimages_listing</argument>
            <argument name="eventObject" xsi:type="string">vendor_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Vendor\Module\Model\ResourceModel\Module</argument>
        </arguments>
</type>

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.