简体   繁体   English

Spring 启动和 Thymeleaf:在 Z686155AF75A60A0F36E9D80C1F7ED 中迭代 map 条目

[英]Spring Boot and Thymeleaf: iterating map entries in JavaScript

Spring Boot + Thymeleaf here. Spring 启动 + Thymeleaf 在这里。 I have the following static HTML file that I've been testing with:我有以下我一直在测试的static HTML 文件:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <script>
        window.onload = function() {

            var foodChart = new CanvasJS.Chart("foodChart", {
                animationEnabled: true,
                title: {
                    text: "Foods"
                },
                data: [{
                    type: "pie",
                    startAngle: 240,
                    yValueFormatString: "##0.00\"%\"",
                    indexLabel: "{label} {y}",
                    dataPoints: [
                        {y: 95.0, label: "Pizza"},
                        {y: 5.0, label: "French Fries"}
                    ]
                }]
            });
            foodChart.render();
        }
    </script>
</head>
<body>

<div id="foodChart" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

</body>
</html>

This opens in a browser and works just great, Pie Chart and all.这将在浏览器中打开并且效果很好,饼图和所有。

But now I want to make the Pie Chart dynamic and I want the server to set the values of all the slices in the Pie Chart.但现在我想让饼图动态化,并且我希望服务器设置饼图中所有切片的值。 So on the server-side (Java/Spring Boot), I have:所以在服务器端(Java/Spring Boot),我有:

Map<String,BigDecimal> foodMap = new HashMap<>();
foodMap.put("Pizza", BigDecimal.valueOf(35.0)); // 35%
foodMap.put("French Fries", BigDecimal.valueOf(65.0)); // 65%

And I'm trying to figure out how to modify the HTML file (template) with Thymeleaf tags so that I can use this foodMap to drive the slices of the Pie Chart, dynamically.我试图弄清楚如何使用 Thymeleaf 标签修改 HTML 文件(模板),以便我可以使用这个foodMap来动态地驱动饼图的切片。 I think I need something like:我需要类似的东西:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <script>
        window.onload = function() {

            var foodChart = new CanvasJS.Chart("foodChart", {
                animationEnabled: true,
                title: {
                    text: "Foods"
                },
                data: [{
                    type: "pie",
                    startAngle: 240,
                    yValueFormatString: "##0.00\"%\"",
                    indexLabel: "{label} {y}",
                    dataPoints: [

                    <th:each="key: ${foodMap}">
                        {<th:text="y: ${value}, label: ${key}" />}
                    </th>

                    ]
                }]
            });
            foodChart.render();
        }
    </script>
</head>
<body>

<div id="foodChart" style="height: 300px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

</body>
</html>

But this doesn't work, can anyone nudge me in the right direction?但这不起作用,任何人都可以将我推向正确的方向吗?

You should take advantage of Thymeleaf's JavaScript inlining for this.为此,您应该利用 Thymeleaf 的JavaScript 内联 For example, if you do this:例如,如果您这样做:

<script th:inline="javascript">
  let foodMap = /*[[${foodMap}]]*/ {};
</script>

It will output:它将 output:

<script>
  let foodMap = {"Pizza":35.0,"French Fries":65.0};
</script>

Once you have this data, it's pretty easy to convert it to the format you want.一旦你有了这些数据,就很容易将它转换成你想要的格式。 Putting it all together:把它们放在一起:

<script th:inline="javascript">
  window.onload = function() {
    let foodMap = /*[[${foodMap}]]*/ {};

    let dataPoints = [];
    for (food in foodMap) {
      dataPoints.push({
        'label': food,
        'y': foodMap[food]
      });
    };

    var foodChart = new CanvasJS.Chart("foodChart", {
      animationEnabled: true,
      title: {text: "Foods"},
      data: [{
        type: "pie",
        startAngle: 240,
        yValueFormatString: "##0.00\"%\"",
        indexLabel: "{label} {y}",
        dataPoints: dataPoints
      }]
    });
    
    foodChart.render();
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM