Using Thymeleaf in Java Script code

0

Good evening, friends,

I had a javascript code extract using JSP tags that generated a pie chart and it works correctly. I now need to migrate to Thymeleaf and I have no idea how to write the code. The javascript code with the JSP looks like this:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">
  google.charts.load('current', {'packages':['corechart']});

  google.charts.setOnLoadCallback(drawChartPie);

  function drawChartPie() {

    var data = google.visualization.arrayToDataTable([
      ['Conta', 'Valor'],         
        <c:forEach items="${model.contaCorretora }" var="contaCorretora">
      ['${contaCorretora.tipoConta}', ${contaCorretora.valorAtual}],
    </c:forEach>
      ]);

    var options = {
      title: '${model.pessoa.nome }'
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }   
</script>
    
asked by anonymous 31.07.2017 / 03:19

1 answer

0

Good afternoon, you can use Inline to Javascript, this way you can access the view variables directly within the JS code. For example:

<script th:inline="javascript">
  /*<![CDATA[*/
  ...

  var titulo = [[${model.pessoa.nome}]];

  ...
  /*]]>*/
</script> 

I hope I have helped.

    
12.04.2018 / 20:26