1

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

  • your calling getId() method but it is not in your block class. Also your onclick function does not do anything so sure you will not get a response from that – fmsthird May 8 at 10:04
  • you can check this on how to pass response from the controller to ajax magento.stackexchange.com/questions/138043/… – fmsthird May 8 at 10:09
1

I solved it by doing make an ajax request and sent in to controller.

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

<div class="pp-buttons-container">
    <button class="import" 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",'mage/url'], function($, url){
        "use strict";
        $(document).on('click','.col-action-grouped a',function(){
        });

        $(document).on('click','.import',function(){
            jQuery.ajax({
            url: '/admin/customimport/index/import',
            type: "POST",
            data: {data:'success'},
            success: function(response){
                console.log('Sucess');
            }
        });
        });

    });
</script>

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(){
        echo 'Success';
        exit;
    }
}

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.