Jump to content

1.7.6.2 ajax call on AdminModuleController


Tom1369

Recommended Posts

I  call ajax from select2 in backoffice so I create hook


 

public function hookActionAdminControllerSetMedia() {
MediaCore::addJsDefL('my_search_token', $this->context->link->getAdminLink('Psb2BAjaxModuleAdminController')); $this->context->controller->addCSS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/select2-full/dist/css/select2.min.css','all'); $this->context->controller->addJS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/select2-full/dist/js/select2.min.js'); $this->context->controller->addJS(_PS_BO_ALL_THEMES_DIR_ . 'default/js/tree.js'); 
$this->context->controller->addCSS($this->_path . 'views/css/' . $this->name . '.css', 'all');
}

And In my Module psb2b/controllers/admin/ajax.php

 

namespace Company\PSB2B\Controller\admin;
use Symfony\Component\HttpFoundation\JsonResponse;

class Psb2BAjaxModuleAdminController extends ModuleAdminController 
{ 

public function initContent() { 
$this->ajax = true; 
parent::initContent(); 
return new JsonResponse('test'); 
}
}

 

And my js

 

$(document).ready(function(){ 
  $('#category_features').select2({ 
    width: 'resolve', 
    ajax: { 
      type: 'POST', 
      url: my_search_token, 
      dataType:'json', 
      delay: 250, 
      data: function (params) { 
        return { 
          q: params.term // search term 
               }; 
      }, 
      success: function (result) 
      { console.log(result); 
      } } 
  }); 
});

But I got error message Controller Psb2BAjaxModuleAdminController not found

Edited by Tom1369 (see edit history)
Link to comment
Share on other sites

Hi There, 

There is a plenty of way to integrate ajax functionality to an admin module controller. 

First you need to bind an ajax function on click of that  button. You also need to send the module admin controller link to the js file. You can send the file via custom script tag in your helper form.

<?php 
$link = new Link(); 
$urladmin = $link->getAdminLink( 'YourControllerName' ); 
?> 
<script type="text/javascript"> 
  var priceExpiryDate = "{$urladmin}"; 
</script>

Then you can use ajax to fire function on the server. 

$.ajax({ 
  type: 'POST', 
  cache: false, 
  dataType: 'json',
  url: adminajax_link, 
  data: { 
    ajax: true, 
    action: 'yourActionName'//lowercase with action name }, 
    success : function (data) { 
      console.log(data); 
    }, 
      error : function (data){ 
        console.log(data); 
      } 
    });

Then do your php on code on the function e.g. that is your actionname in your admin controller class.

public function ajaxProcessYourActionName() { echo json_encode('foo');//something you want to return exit; }

Please mind the camel case of the name.

Boom It is done.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...