Skip to content

Update chart in twig template after AJAX request not displaying #42

@Luka2010

Description

@Luka2010

I try since weeks to update the chart via an AJAX request. I use tabs which send of the AJAX request
In my normal view I have the following function:

$('#country-tabs a').on('click', function (e) {
         e.preventDefault()
         $(this).tab('show');
         var $this = $(this),
         loadurl = $this.attr('href');

         var form = $('form');
                       
         var jsonData = $.ajax({
             url: loadurl,  
             type:       'POST',
             data: form.serializeArray(),
             dataType:   'json',  
             async: false,

             success: function(data, status) {              
                 $("div#client-loop-container").html(data.chartview); 
                 $('#status').addClass('status-bar-success');
     }
       }).responseText;

     });

My controller then returns

if ($request->isXmlHttpRequest())
       {   
         $result = $this->renderView('fruits/chart.html.twig', [
                 'fruitCounts' => $fruitCounts,
                 'barchart' => $barchart
           ]);
          return new JsonResponse(['success' => true, 'chartview' => $result]);
    }

My chart.twig.html then has the following:

   <div id="client-loop-container">
       <div class="w-100" id="div_chart"></div>
        {{  dump(barchart) }}      
        {% block javascripts %}
            <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
           <script type="text/javascript">          
             {{ gc_draw(barchart, 'div_chart') }}
           </script>
       {% endblock %}

dump(barchart) Shows CMEN\GoogleChartsBundle\GoogleCharts\Charts\LineChart with the updated data, but I do not get any Chart displayed. What am I missing here? Is it a bug, do I use it wrong or is it simply not possible to do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions