Skip to main content
added 1 character in body
Source Link
anonymous
  • 3.8k
  • 4
  • 25
  • 67

app\codeapp\code\{Vendor_Name}{Module_Name}\Block\Adminhtml\Form\Edit\Tab\Import.php

app\code{Vendor_Name}{Module_Name}\Block\Adminhtml\Form\Edit\Tab\Import.php

app\code\{Vendor_Name}{Module_Name}\Block\Adminhtml\Form\Edit\Tab\Import.php

edited tags
Link
Source Link

Magento 2.3: How to Echo From Controller On Button Click?

I'm very new to Magento, I have a custom module and some tabs and fields in it, i have an 'import' tab, in that their is 'Import' button, just want to know the workflow of that button, like on button click just echo something from controller. Right now i'm getting no response on button click...

app\code{Vendor_Name}{Module_Name}\view\adminhtml\templates\form\Import.phtml

<div class="pp-buttons-container">
    <button id="<?php echo $block->getId() ?>" onclick="return false;">
        <span><span><span><?php echo 'Import'; ?></span></span></span>
    </button>
</div>
<script type="text/javascript">
    require(["jquery",], function($){
        "use strict";
        $(document).on('click','.col-action-grouped a',function(){
        });
    });
</script>

app\code{Vendor_Name}{Module_Name}\Block\Adminhtml\Form\Edit\Tab\Import.php

<?php
namespace EC\Customimport\Block\Adminhtml\Form\Edit\Tab;
 
 
class Import extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    protected $_template = 'form/import.phtml';
 
    public function isReadonly()
    {
        return false;
    }

    public function getTabLabel()
    {
        return __('Import');
    }
 
    public function getTabTitle()
    {
        return __('Import');
    }
 
    public function canShowTab()
    {
        return true;
    }
 
    public function isHidden()
    {
        return false;
    }
}

app\code{Vendor_Name}{Module_Name}\Controller\Adminhtml\Index\Import.php

<?php

namespace EC\Customimport\Controller\Adminhtml\Index;

use EC\Customimport\Model\CustomimportFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;

class Import extends \Magento\Backend\App\Action
{
    /**
     * @param Context $context
     */
    public function __construct(
        Context $context,
        CustomimportFactory $modelCustomimportFactory
    ) {
        $this->modelCustomimportFactory = $modelCustomimportFactory;
        $this->resultFactory = $context->getResultFactory();
        parent::__construct($context);
    }

    public function execute(){
        ignore_user_abort(true);
        set_time_limit(0);
        $id     = $this->getRequest()->getParam('id');
        $model  = $this->modelCustomimportFactory->create()->load($id);
        $model->import();
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setPath('adminhtml/*/index');
        return $resultRedirect;
    }
}

enter image description here