1

I have created a grid & have replaced "Add New" button action as below

    <?php
    namespace Company\Module\Block\Adminhtml;

    class Listproduct extends \Magento\Backend\Block\Widget\Grid\Container
    {
        public function __construct(
            \Magento\Backend\Block\Widget\Context $context,
            array $data = []
        ) {        
            parent::__construct($context, $data);
        }

       protected function _construct()
        {
            $urlData = $this->getRequest()->getParams();

            parent::_construct();
            $this->_controller = 'adminhtml_listproduct';
            $this->_blockGroup = 'Company_Module';
            $this->_headerText = __('Manage products');
            $this->_addButtonLabel = __('Save');
            $this->removeButton('add');
        }

        protected function _prepareLayout()
        {
            $this->buttonList->add(
                    'nameofbutton',
                    [
                        'label' => __('Add Selected Products'),
                        'onclick' => 'setLocation(\'' . $this->getUrl('*/listproduct/massaddproducts') . '\')',
                        'class' => 'add primary'
                    ],
                    0
            );
            return parent::_prepareLayout();
        }
    }

This is my grid. enter image description here

My prepareColumn function from grid.php is

protected function _prepareColumns()
    {
        $this->addColumn(
            'id',
            [
                'header_css_class' => 'a-center',
                'type' => 'checkbox',
                'name' => 'id',
                'values' => $this->_getSelectedProducts(),
                'align' => 'center',
                'index' => 'entity_id',
                'use_index' => true
            ]
        );

        $this->addColumn(
            'entity_id',
            [
                'header' => __('entity_id'),
                'type' => 'number',
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'sku',
            [
                'header' => __('sku'),
                'index' => 'sku',
                'class' => 'sku'
            ]
        );

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

        $block = $this->getLayout()->getBlock('grid.bottom.links');
        if ($block) {
            $this->setChild('grid.bottom.links', $block);
        }

        return parent::_prepareColumns();
    }

    protected function _getSelectedProducts()
    {
        return $this->getRequest()->getPost('selected', []);
    }

Controller is:-

    <?php
    namespace Company\Module\Controller\Adminhtml\Listproduct;

    class Massaddproducts extends \Magento\Backend\App\Action
    {
       protected $_productloader;  

       public function __construct(
            \Magento\Backend\App\Action\Context $context,
            \Magento\Catalog\Model\ProductFactory $_productloader
        ) {
            parent::__construct($context);
            $this->_productloader       = $_productloader;
        }

        public function execute()
        {
            $ids = $this->getRequest()->getParam('id');

            //$data = $this->getRequest()->getPost();
            $data = $this->getRequest()->getParams();

            echo '<pre>'; print_r($data); echo '</pre>'; exit;


        }
    }

I just want checked checkbox values on button click of "Add selected products"

  • confused to understand what you want, do you need receive checkboxed items ? massaddproducts should be controller action then – Ilja Jan 11 '17 at 7:09
  • After I checked few checkboxes from grid, I want selected checkbox values on button click i.e. "Add Selected Products". And yes $this->getUrl('*/listproduct/massaddproducts') is my controller – Nitz Jan 11 '17 at 7:16
  • You find any solution for this issue. – Pratik Oct 9 '17 at 7:17
2

Try the code shared below:-

class Massaddproducts extends \Magento\Backend\App\Action
{
   protected $_productloader;  
   protected $filter;

   public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Ui\Component\MassAction\Filter $filter, 
        \Magento\Catalog\Model\ProductFactory $_productloader
    ) {
        $this->filter = $filter;
        parent::__construct($context);
        $this->_productloader       = $_productloader;
    }

    public function execute()
    {
        $ids = $this->getRequest()->getParam('id');

        //$data = $this->getRequest()->getPost();
        $data = $this->getRequest()->getParams();
        $collection = $this->filter->
                      getCollection($this->_collectionFactory
                                ->create()
                                ); // get your selected collection

        $collectionSize = $collection->getSize();
        $items = $collection->getItems();

        foreach($items as $order){
            $postSelectedArr[] = $order->getId();
        }
        echo '<pre>';
        print_r($postSelectedArr);            

    }
}

I think you will have a better understanding form this thread:- Mass Action not sending all data

P.S:- If you get error for var/generation try deleting var/generation and try again

Your Answer

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

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