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();
}
}
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"
