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

anonymous
3,7624 gold badges26 silver badges67 bronze badges
asked May 8, 2019 at 9:36
2
  • 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 Commented May 8, 2019 at 10:04
  • you can check this on how to pass response from the controller to ajax magento.stackexchange.com/questions/138043/… Commented May 8, 2019 at 10:09

1 Answer 1

2

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: '/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;
 }
}
answered May 8, 2019 at 11:32
17
  • url: '/admin/customimport/index/import', where is this coming from? Commented Dec 16, 2019 at 10:42
  • it's my controller path Commented Dec 16, 2019 at 11:36
  • app\code{Vendor_Name}{Module_Name}\Controller\Adminhtml\Index\Import.php where is admin? '/admin/customimport/index/import Commented Dec 16, 2019 at 11:50
  • maybe a mistake brother. Commented Dec 16, 2019 at 11:58
  • No i am not judging i want to know how this work i am trying to learn this please can you explain Commented Dec 16, 2019 at 12:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.