3

I have created one custom product grid in product tab of magento 2. It displays my product listings and has one column with check boxes.I have written one observer so when clicked on SAVE in products that will invoke I need to get the row ids of selected check box. Please help me with this.what i need to know is how to get custom grid ids under the product tab. thanks in advance. If any one is familiar with this please help me.I am stucked here.

<?php
namespace Netenrich\Company\Block\Adminhtml\Catalog\Product\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;

class Distibutor extends \Magento\Backend\Block\Widget\Grid\Extended {

    /**
     * @var \Netenrich\Company\Model\ResourceModel\Company\CollectionFactory
     */
    protected $_collectionFactory;
    //protected $_companySpecificFactory;

    /**
     * @var \Netenrich\Company\Model\Company
     */
    protected $_Company;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Netenrich\Company\Model\Company $CompanyPage
     * @param \Netenrich\Company\Model\ResourceModel\Company\CollectionFactory $collectionFactory
     * @param \Magento\Core\Model\PageLayout\Config\Builder $pageLayoutBuilder
     * @param array $data
     */
    public function __construct(
    \Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Netenrich\Company\Model\ResourceModel\Company\CollectionFactory $collectionFactory,  array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
       // $this->_companySpecificFactory = $specificCollectionFactory;
        //$this->_Company = $Company;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct() {
        parent::_construct();
        $this->setId('productgridGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('org_id');
        $this->setDefaultDir('DESC');
        $this->setDefaultFilter(array('in_customers' => 1));
        $this->setFilterVisibility(true);
        $this->setSaveParametersInSession(true);
    }

    /**
     * Prepare collection
     *
     * @return \Magento\Backend\Block\Widget\Grid
     */
    protected function _prepareCollection() {
        $collection = $this->_collectionFactory->create();
        /* @var $collection \Netenrich\Company\Model\ResourceModel\Company\Collection */
        $collection=$collection->addFieldToFilter('org_type_id',2);
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    /**
     * Prepare columns
     *
     * @return \Magento\Backend\Block\Widget\Grid\Extended
     */
    protected function _prepareColumns() {

        /* $this->addColumn('in_customers', array(
          'header_css_class' => 'a-center',
          'type' => 'checkbox',
          'field_name' => 'in_customers',
          'values' => $this->_getSelectedCompanies(),
          'align' => 'center',
          //'index'             => 'entity_id'
          'index' => 'org_id'
          )); */

        $this->addColumn('in_customers', array(
            'header_css_class' => 'a-center',
            'type' => 'checkbox',
            'index' => 'org_id',
            'align' => 'center',
            'field_name' => 'selectedproducts[]',
          //  'values' => $this->_getSelectedCompanies()
        ));

        $this->addColumn('org_id', [
            'header' => __('ID'),
            'index' => 'org_id',
        ]);


        $this->addColumn('name', ['header' => __('Orginisation Name'), 'index' => 'name']);
        $this->addColumn('address', ['header' => __('Address'), 'index' => 'address']);
        $this->addColumn('address2', ['header' => __('Address2'), 'index' => 'address2']);
        $this->addColumn('org_type_id', ['header' => __('Orginisation Type'), 'index' => 'org_type_id', 'renderer' => 'Netenrich\Company\Block\Adminhtml\Company\Grid\Renderer\Organisation', 'filter_condition_callback' => array($this, '_addressFilter')]);
        $this->addColumn('affiliation', ['header' => __('Affiliation'), 'index' => 'name', 'renderer' => 'Netenrich\Company\Block\Adminhtml\Company\Grid\Renderer\Affiliation']);
        //$this->addColumn('var', ['header' => __('VAR'), 'index' => 'var']);
        $this->addColumn('payment_method', ['header' => __('Payment Method'), 'index' => 'payment_method']);


        return parent::_prepareColumns();
    }
7
  • What you have try to create that grid? – Dhiren Vasoya Dec 28 '16 at 12:55
  • created grid.php by extending \Magento\Backend\Block\Widget\Grid\Extended and used addColumuns() – vijay b Dec 28 '16 at 13:01
  • try in your controller action, to acces value posted. – Dhiren Vasoya Dec 28 '16 at 13:03
  • actually that grid will be displayed as new tab under product->catalog->edit...i added custom product tab it displays the our custom grid – vijay b Dec 28 '16 at 13:05
  • Have you posted any value from grid to Controller? – Dhiren Vasoya Dec 28 '16 at 13:07

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.