0

I have created custom grid using ui component.

I want to add product sku column in custom grid which contains product skus list from product table.

How to do that ....

If anyone knows please explain me.

closed as too broad by Prince Patel, Priyank, Manoj Deswal, Jai, Piyush Mar 20 '18 at 6:49

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1
  • Vendor Name : Testing
  • Module Name : ABC

app/code/Testing/ABC/Model/ResourceModel/ABC/Grid/Collection.php

You need to just add code to fetch product sku in _renderFiltersBefore() function

<?php

namespace Testing\ABC\Model\ResourceModel\ABC\Grid;

use Testing\ABC\Model\ResourceModel\ABC\Collection as ABCCollection;
use Magento\Framework\View\Element\UiComponent\DataProvider\Document as ABCModel;

class Collection extends ABCCollection 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 = ABCModel::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
    protected function _renderFiltersBefore()
    {
        $this->getSelect()->joinLeft(
            ['product' => $this->getTable('catalog_product_entity')],
            'product.entity_id = main_table.product_id',
            ['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;
    }
}

Note : In your xml file, filter should be working by product_id.

  • still I'm not getting @Rohan............. – Mano M Mar 20 '18 at 7:09
  • can you post your error message? – Rohan Hapani Mar 20 '18 at 8:33
  • Do you fetch records by product id? – Rohan Hapani Mar 20 '18 at 8:42
  • Happy to help!!! Happy Coding :) @PriyaM – Rohan Hapani Mar 20 '18 at 9:01
  • I'll post answer of your second question. Please upvote also in answer. So other user can use this answer. – Rohan Hapani Mar 20 '18 at 9:02
0

add your desired columns in prepareColumns

protected function _prepareCollection()
    {
               $collection = $this->_productFactory->create()->getCollection()
               ->addAttributeToSelect(
                   'name'
               )->addAttributeToSelect(
                   'sku'
               )->addAttributeToSelect(
                   'price'
               )
        ->addOrder('entity_id', 'asc');
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

protected function _prepareColumns()
    {

            $this->addColumn(
                'in_events',
                [
                    'header_css_class'  => 'a-center',
                    'type' => 'checkbox',
                    'name' => 'in_events',
                    'values' => $this->_getSelectedProducts(),
                    'index' => 'entity_id',
                    'header_css_class' => 'col-select col-massaction',
                   'column_css_class' => 'col-select col-massaction',
                   // 'use_index' => true
                ]
            );

        $this->addColumn(
            'entity_id',
            [
                'header' => __('ID'),
                'sortable' => true,
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn('name', ['header' => __('Name'), 'index' => 'name']);
        $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku'
            ]);
        $this->addColumn(
            'price',
            [
                'header' => __('Price'),
                'type' => 'currency',
                'currency_code' => (string)$this->_scopeConfig->getValue(
                    \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
                ),
                'index' => 'price'
            ]
        );
        $this->addColumn(
            'position',
            [
                'header' => __('Position'),
                'type' => 'number',
                'index' => 'position',
                'editable' => 'false'
            ]
        );

        return parent::_prepareColumns();
    }
  • Thanks..But I'm creating ui component grid – Mano M Mar 20 '18 at 6:44

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