Adding Buttons to Magento Admin Grid View
If you’re looking to add custom buttons directly within rows of your Magento admin panel grid view, you’ve come to the right place. This guide will walk you through the process step-by-step and provide code examples to help you achieve this.
Understanding the Problem
You want to allow users to change status directly from the grid view without using the mass update checkbox option. This would enhance usability by eliminating additional clicks.
The Solution Approach
We’ll be leveraging Magento’s grid column rendering functionality along with custom JavaScript to create our button and handle the status change.
Step-by-Step Guide
1. Define your Column in Layout XML File
First, you need to define your new column in the appropriate layout XML file under app/code/Vendor/Module/etc/adminhtml.xml.
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/View/Layout/etc/layouts.xsd">
<update handle="default"/>
<referenceBlock name="content">
<block type="Vendor_Module/adminhtml_grid_column" as="status_column" template="Vendor_Module/status.phtml" after="columns">
<action method="setFilterVisibility">
<value>1</value>
</action>
<action method="setIsSystem">
<value>0</value>
</action>
</block>
</referenceBlock>
</layout>
2. Create the Column Block
Next, create a custom column block in app/code/Vendor/Module/Block/Adminhtml/Grid/Column.php.
<?php
namespace Vendor\Module\Block\Adminhtml\Grid;
class Status extends \Magento\Backend\Block\Widget\Grid\Column
{
public function _construct()
{
$this->setData('filter_condition_callback', 'Vendor\Module\Model\ResourceModel\YourModel_Collection::filterStatusConditionCallback');
}
}
3. Implement the Filter Condition Callback in Your Module’s Collection Class
<?php
namespace Vendor\Module\Model\ResourceModel;
class YourModel_Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
public function filterStatusConditionCallback($collection, $column)
{
// Implement your logic to filter the collection based on status
}
}
4. Create the Template File for Your Button
Create a new file at app/code/Vendor/Module/view/adminhtml/templates/status.phtml.
<?php
$status = 'status_column_value';
$baseUrl = $this->getUrl('your_module/index/saveStatus', ['id' => $block->getRowId()] );
?>
<div class="admin__actions">
<a href="<?php echo $baseUrl ?>?status=<?php echo $status ?>" class="action-primary" title="" id="status_column_row_<?php echo $block->getRowId() ?>">
<?php if ($status == 1): ?>
Enable
<?php else: ?>
Disable
<?php endif; ?>
</a>
</div>
5. Create a Controller to Handle Status Change
Create app/code/Vendor/Module/Controller/Adminhtml/Index/SaveStatus.php.
<?php
namespace Vendor\Module\Controller\Adminhtml\Index;
use Magento\Framework\App\Action\HttpPostActionInterface as PostActionInterface;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\ResultFactory;
class SaveStatus extends \Magento\Backend\App\Action implements PostActionInterface
{
public function execute()
{
$id = (int) $this->getRequest()->getParam('id');
$status = (int) $this->getRequest()->getParam('status');
// Your logic to update the status
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('your_module/grid/view');
}
}
Conclusion
Adding a custom button directly into each row of your Magento admin panel grid view is a powerful way to enhance user experience and streamline workflows.
By defining the column in layout XML, creating the corresponding block and collection filters, and implementing the necessary templates and controllers, you can achieve this functionality with ease.
Remember, always ensure your code follows best practices for security, performance, and maintainability. If you need further assistance or have any questions about Magento development, feel free to reach out to us at Laravel Company.
This guide should provide a solid foundation for handling such tasks in the future as well. Happy coding!