0

I have created admin grid for product in product view page.It display all product details.But i need to display the current product details in admin grid.

How to apply filter collection from table and display in grid.

di.xml

 <virtualType name="XxxStockGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Xxx\Stock\Model\ResourceModel\Product\Collection</argument>           
        </arguments>
    </virtualType>

    <type name="Xxx\Stock\Model\ResourceModel\Product\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">xx_stock_movement</argument>
            <argument name="eventPrefix" xsi:type="string">stockmovements_product_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">product_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Xx\Stock\Model\ResourceModel\Product</argument>
        </arguments>
    </type>   

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="Xxx_stock_stock_movements_data_source" xsi:type="string">Xxx\Stock\Model\ResourceModel\Product\Grid\Collection</item>
            </argument>
        </arguments>
    </type> 
<?php

Model

 namespace Xxx\Stock\Model;

use Magento\Framework\Model\AbstractModel;

class Product extends AbstractModel
{

    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this-_init('Xxx\Stock\Model\ResourceModel\Product');
    }

}

Resource files

<?php

namespace Xxx\Stock\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Message\ManagerInterface;
use Magento\Catalog\Model\Product as Collection;
use Magento\Framework\App\ResourceConnection;

class Product extends AbstractDb
{

    /**
     * DB Storage table name
     */
    const TABLE_NAME = 'xx_stock_movement';

    /**
     * @var \Magento\Framework\DB\Adapter\AdapterInterface
     */
    protected $_connection;

    /**
     * @var Resource
     */
    protected $_resource;

    /**
     * Date model
     * 
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_date;
    protected $_productCollectionFactory;
    protected $_messageManager;

    /**
     * constructor
     * 
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
     */
    public function __construct(
    \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Model\ResourceModel\Db\Context $context, Collection $productCollection, ResourceConnection $resource, ManagerInterface $messageManager
    )
    {
        $this->_connection = $resource->getConnection();
        $this->_resource = $resource;
        $this->_messageManager = $messageManager;
        $this->_date = $date;
        $this->_productCollection = $productCollection;
        parent::__construct($context);
    }

    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('xx_stock_movement', 'movement_id');
    }

    protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
    {

        if ($object->isObjectNew()) {
            $object->setCreatedAt($this->_date->date());
        }
        return parent::_beforeSave($object);
    }

Collection file

<?php

namespace XxX\Stock\Model\ResourceModel\Product;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Event\ManagerInterface;
use xxX\Stock\Helper\Data;

class Collection extends AbstractCollection
{

    protected $_idFieldName = 'movement_id';
    protected $_eventPrefix = 'stockmovements_product_grid_collection';
    protected $_eventObject = 'product_grid_collection';


    protected $_helperData;

    public function __construct(
    EntityFactoryInterface $entityFactory, LoggerInterface $logger, FetchStrategyInterface $fetchStrategy, ManagerInterface $eventManager, Data $helperData
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);
        $this->_helperData = $helperData;
    }

    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init('Xxx\Stock\Model\Product', 'Xx\Stock\Model\ResourceModel\Product');
    }
}

Grid collection

namespace Xx\Stock\Model\ResourceModel\Product\Grid;

class Collection extends \Xx\Stock\Model\ResourceModel\Product\Collection implements \Magento\Framework\Api\Search\SearchResultInterface
{

    protected $_aggregations;

    public function __construct(\Xx\Stock\Helper\Data $helperData,\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, $mainTable = 'xx_stock_movement', $eventPrefix, $eventObject, $resourceModel, $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document')
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager,$helperData);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }

    protected function _renderFiltersBefore()
    {
        $this->getSelect()->joinLeft(
                        array('stock_item' => $this->getTable('cataloginventory_stock_item')), 'main_table.item_id  = stock_item.item_id', 'product_id')
                ->joinLeft(
                        array('product' => $this->getTable('catalog_product_entity')), 'stock_item.product_id = product.entity_id', array('product_sku' => 'product.sku')
        );
        parent::_renderFiltersBefore();
    }

    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;
    }

}

Thanks.

  • Have you created gird via UI component or normal grid? – Abdul Mar 22 '18 at 4:53
  • Ui Component............. – Mano M Mar 22 '18 at 4:55
  • can u share your code? – Abdul Mar 22 '18 at 5:02
  • Model files...? – Mano M Mar 22 '18 at 5:09
  • yes & layout file – Abdul Mar 22 '18 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.