2016-07-06 8 views
3

Ich versuche, einen Ajax-Aufruf und übergeben Sie den Dropdown-Wert im Controller und erhalten Sie diese bestimmte verwandte Bestellinformationen in View-Datei. Antwort wird wahr, aber wie man diese Antwort in meiner Ansichtsdatei verwendet.Wie bekomme ich Ajax-Antwort vom Controller in Magento 2

Hier meine phtml Datei ist:

<select id="chartOption"> 
    <option value="">Select Option</option> 
    <option value="status">Status</option> 
    <option value="payments">Payments</option> 
    <option value="browser">Browser</option> 
</select> 
<input type="button" name="chart_button" value="Generate chart" onclick="click_chart()"> 
<div id="result"></div> 
<script type="text/javascript"> 
     function click_chart() { 
      var a = document.getElementById("chartOption"); 
      var abc = a.options[a.selectedIndex].value; 
      data = jQuery(this).serialize(); 
      jQuery.ajax({ 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json", 
       url: "Blog/Post/Information", 
       data: "label=" + abc, 
       success: function (result) { jQuery('#result').html(result); }, 
       error: function (error) { jQuery('#result').html(error); } 
      }); 
     } 
</script> 

Und hier ist mein Controller:

public function execute() { 
    if (isset($_REQUEST['ajax'])) { 
     $label = $this->getRequest()->getPost('label'); 
     $_orders = $this->getOrders(); 
     $complete = $pending = $closed = $canceled = $processing = $onHold = 0; 
     foreach ($_orders as $_order): 
      $label = $_order->getStatusLabel(); 
      if ($label == 'Complete') { 
       $complete++; 
      } 
      if ($label == 'Pending') { 
       $pending++; 
      } 
      if ($label == 'Closed') { 
       $closed++; 
      } 
      if ($label == 'Canceled') { 
       $canceled++; 
      } 
      if ($label == 'Processing') { 
       $processing++; 
      } 
      if ($label == 'On Hold') { 
       $onHold++; 
      } 
     endforeach; 
     $orderData = "['Task', 'Hours per Day'],['Complete'," . $complete . "],['Pending'," . $pending . "]],['Processing'," . $processing . "]],['Canceled'," . $canceled . "]"; 
     $arr = array('result' => $orderData); 
     $jsonData = json_encode(array($arr)); 
     $this->getResponse()->setHeader('Content-type', 'application/json'); 
     $this->getResponse()->setBody($jsonData); 
    } 
} 

Antwort

7

unten ist das Beispiel, wie dies zu tun, es Modifizieren Sie bitte Ihre Forderung nach .

Ich habe js Vorlage dafür verwendet.

Folgendes Beispiel wird Dropdown in Ihrer PHP-Datei erstellen. Sie können dieselbe Methode verwenden, um Ihre Daten im gewünschten Format anzuzeigen.

In Ihrem JS

define([ 
     'jquery', 
     'underscore', 
     'mage/template', 
     'jquery/list-filter' 
     ], function (
      $, 
      _, 
      template 
     ) { 
      function main(config, element) { 
       var $element = $(element); 
       $(document).on('click','yourID_Or_Class',function() { 
         var param = 'ajax=1'; 
          $.ajax({ 
           showLoader: true, 
           url: YOUR_URL_HERE, 
           data: param, 
           type: "POST", 
           dataType: 'json' 
          }).done(function (data) { 
           $('#test').removeClass('hideme'); 
           var html = template('#test', {posts:data}); 
           $('#test').html(html); 
          }); 
        }); 
      }; 
     return main; 
    }); 

Kontroller

public function __construct(
     Context            $context, 
     \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, 
    ) { 

     $this->resultJsonFactory   = $resultJsonFactory; 
     parent::__construct($context); 
    } 


    public function execute() 
    { 
     $result     = $this->resultJsonFactory->create(); 
     if ($this->getRequest()->isAjax()) 
     { 
      $test=Array 
      (
       'Firstname' => 'What is your firstname', 
       'Email' => 'What is your emailId', 
       'Lastname' => 'What is your lastname', 
       'Country' => 'Your Country' 
      ); 
      return $result->setData($test); 
     } 
    } 

in Ihrer phtml Datei

<style> 
.hideme{display:none;} 
</style> 
<div id='test' class="hideme"> 
    <select> 
    <% _.each(posts, function(text,value) { %> 
     <option value="<%= value %>"><%= text %></option> 
    <% }) %> 
    </select> 
</div> 

Hoffnung, das hilft.

+0

das funktioniert aber magento entmutigt die Verwendung von Superglobalen wie $ _REQUEST. Wenn Sie sich auf dem Marktplatz bewerben wollen, wird es abgelehnt. –

Verwandte Themen