The question is how to get the input data in mass action.
I have currently added a input field in the admin grid. Like this:
I have added the input field using the block. I want to post that values when clicking the mass action. Currently only the entity_id is being posted when I click the massaction button. I get the values in the controller, for the order data I use collection factory to get the data by using the entity_id. But I cannot get any other post data.
I am sharing my code for this process, correct me if I have to change somewhere or to add anything in it.
xxxxxx/BulkRefund/Block/Adminhtml/Grid/Grid.php
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
protected $_status;
protected $_blogFactory;
protected $moduleManager;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $backendHelper,
\xxxxxx\BulkRefund\Model\BulkRefundFactory $blogFactory,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Framework\Module\Manager $moduleManager,
array $data = []
) {
$this->_blogFactory = $blogFactory;
//$this->_status = $status;
$this->_formFactory = $formFactory;
$this->moduleManager = $moduleManager;
parent::__construct($context, $backendHelper, $data);
}
protected function _construct()
{
parent::_construct();
$this->setId('gridGrid');
$this->setDefaultSort('entity_id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
$this->setVarNameFilter('grid_record');
}
protected function _prepareCollection()
{
$collection = $this->_blogFactory->create()->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
protected function _prepareForm()
{
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create(
[
'data' => [
'id' => 'bulk_refunds',
'method' => 'post',
'enctype' => 'multipart/form-data',
],
]
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
protected function _prepareColumns()
{
$this->addColumn(
'entity_id',
[
'header' => __('Entity ID'),
'type' => 'number',
'index' => 'entity_id',
'header_css_class' => 'col-id',
'column_css_class' => 'col-id',
]
);
$this->addColumn(
'increment_id',
[
'header' => __('Increment ID'),
'index' => 'increment_id',
]
);
$this->addColumn(
'customer_name',
[
'header' => __('Customer Name'),
'index' => 'customer_name'
]
);
$this->addColumn(
'customer_email',
[
'header' => __('customer Email'),
'index' => 'customer_email',
]
);
$this->addColumn(
'created_at',
[
'header' => __('Created At'),
'index' => 'created_at',
]
);
$this->addColumn(
'grand_total',
[
'header' => __('Grand Total'),
'index' => 'grand_total',
]
);
$this->addColumn(
'total_refunded',
[
'header' => __('Total Refunded'),
'index' => 'total_refunded',
]
);
$this->addColumn(
'qty',
[
'filter' => false,
'sortable' => false,
'header' => __('Refund Percentage'),
// 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty::class,
'renderer' => \xxxxxxx\BulkRefund\Block\Adminhtml\Renderer\RefundAmount::class,
'name' => 'refund',
'inline_css' => 'refund',
'type' => 'input',
'validate_class' => 'validate-number',
'method' => 'POST',
'action' => $this->getUrl('grid/*/massRefund')
// 'form-method' => 'post',
// 'method' => 'post'
//'index' => 'qty'
]
);
$block = $this->getLayout()->getBlock('grid.bottom.links');
if ($block) {
$this->setChild('grid.bottom.links', $block);
}
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('qty');
$this->getMassactionBlock()->addItem(
'refund',
[
'label' => __('Mass Refund'),
'url' => $this->getUrl('grid/*/massRefund'),
'confirm' => __('Are you sure?'),
// 'getter' => 'getCustomerName',
'method' => 'get'
]
);
return $this;
}
public function getGridUrl()
{
return $this->getUrl('grid/*/grid', ['_current' => true]);
}
}
xxxx/BulkRefund/Block/Adminhtml/Renderer/RefundAmount.php
use Magento\Backend\Block\Context;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Catalog\Helper\Image;
use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;
// use Vendor\Module\Model\ResourceModel\ModuleHistory\CollectionFactory;
class RefundAmount extends AbstractRenderer
{
private $_storeManager;
private $imageHelper;
public function __construct(
Context $context,
Image $imageHelper,
StoreManagerInterface $storemanager,
// CollectionFactory $collectionFactory,
array $data = []
)
{
$this->_storeManager = $storemanager;
parent::__construct($context, $data);
$this->_authorization = $context->getAuthorization();
$this->imageHelper = $imageHelper;
}
public function render(DataObject $row)
{
$qty = $row->getData($this->getColumn()->getIndex());
$qty *= 1;
if (!$qty) {
$qty = '';
}
// Compose html
$html = '<form method="post" action="' . $this->getUrl('grid/*/massRefund') .'" id="form-qty-refund" > <input type="text" ';
$html .= 'name="' . $this->getColumn()->getId() . '" ';
// $html .= 'value="' . $qty . '"';
$html .= 'class="input-text admin__control-text '
. $this->getColumn()->getInlineCss() . '" />
</form>';
return $html;
}
}
xxxx/BulkRefund/Block/Adminhtml/Grid.php
class Grid extends \Magento\Backend\Block\Widget\Container
{
protected $_template = 'grid/view.phtml';
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
array $data = []
) {
$this->setId('blk_refunds');
$this->setDestElementId('bulk_refunds');
$this->setTitle(__('Bulk Refunds'));
parent::__construct($context, $data);
}
protected function _prepareLayout()
{
$this->setChild('grid', $this->getLayout()->createBlock('xxxxx\BulkRefund\Block\Adminhtml\Grid\Grid', 'grid.view.grid'));
return parent::_prepareLayout();
}
protected function _getAddButtonOptions()
{
$splitButtonOptions[] = [
'label' => __('Add New'),
'onclick' => "setLocation('" . $this->_getCreateUrl() . "')",
];
return $splitButtonOptions;
}
protected function _getCreateUrl()
{
return $this->getUrl('grid/*/new');
}
public function getGridHtml()
{
return $this->getChildHtml('grid');
}
}
xxxx/BulkRefund/Controller/Adminhtml/Grid/Grid.php
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\View\LayoutFactory;
class Grid extends Action
{
public function __construct(
Context $context,
Rawfactory $resultRawFactory,
LayoutFactory $layoutFactory
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->layoutFactory = $layoutFactory;
}
public function execute()
{
$resultRaw = $this->resultRawFactory->create();
$blogHtml = $this->layoutFactory->create()->createBlock(
'xxxx\BulkRefund\Block\Adminhtml\Grid\Grid',
'grid.view.grid'
)->toHtml();
return $resultRaw->setContents($blogHtml);
}
}
xxxxx/BulkRefund/Controller/Adminhtml/Grid/MassRefund.php
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
use xxxxx\BulkRefund\Model\ResourceModel\BulkRefund\CollectionFactory;
class MassRefund extends \Magento\Backend\App\Action
{
protected $filter;
protected $collectionFactory;
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory,
\xxxx\BulkRefund\Model\BulkRefundFactory $blogFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->_blogFactory = $blogFactory;
parent::__construct($context);
}
public function execute()
{
$deleteIds = $this->getRequest()->getPost();
print_r($deleteIds); // prints only entity id
echo "<br>";
$id = $this->getRequest()->getParam('qty');
$items = $this->collectionFactory->create()->addFieldToFilter('entity_id', $id)->getItems();
foreach ($items as $item) {
print_r($item->getData());
$test = $item->getData();
echo "<br>";
echo $test['increment_id'];
}
return $resultRedirect->setPath('*/*/');
}
}
xxxx/BulkRefund/view/adminhtml/templates/grid/view.phtml
<?php
echo $block->getGridHtml();
?>
HOPE SOMEONE WILL ANSWER THIS QUESTION
Thanks!