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;
}
}
1 Answer 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: '/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;
}
}
-
url: '/admin/customimport/index/import', where is this coming from?jibin george– jibin george2019年12月16日 10:42:06 +00:00Commented Dec 16, 2019 at 10:42
-
it's my controller pathPartab Saifuddin Zakir– Partab Saifuddin Zakir2019年12月16日 11:36:33 +00:00Commented Dec 16, 2019 at 11:36
-
app\code{Vendor_Name}{Module_Name}\Controller\Adminhtml\Index\Import.php where is admin? '/admin/customimport/index/importjibin george– jibin george2019年12月16日 11:50:47 +00:00Commented Dec 16, 2019 at 11:50
-
maybe a mistake brother.Partab Saifuddin Zakir– Partab Saifuddin Zakir2019年12月16日 11:58:07 +00:00Commented 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 explainjibin george– jibin george2019年12月16日 12:04:42 +00:00Commented Dec 16, 2019 at 12:04
Explore related questions
See similar questions with these tags.
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