You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用Mule Payload构建动态HTML表格作为邮件正文?

用Mule Parse Template组件实现动态API状态HTML表格

步骤1:先将JSON Payload转换为数组格式

在Parse Template组件前添加一个Transform Message组件,用DataWeave把原JSON对象转成键值对数组,方便后续模板遍历处理:

%dw 2.0
output application/json
---
payload pluck ((status, apiName) -> {
    apiName: apiName,
    status: status
})

转换后的Payload格式如下:

[
  {"apiName": "API1", "status": "UP"},
  {"apiName": "API2", "status": "DOWN"},
  {"apiName": "API3", "status": "UP"}
]

步骤2:编写HTML模板(含CSS/JS)

在Parse Template组件中使用以下模板,它会动态渲染表格,还能根据状态显示不同样式:

<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            width: 80%;
            border-collapse: collapse;
            margin: 20px auto;
            font-family: Arial, sans-serif;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
        }
        .status-up {
            color: green;
            font-weight: bold;
        }
        .status-down {
            color: red;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <h2 style="text-align:center">API状态监控表</h2>
    <table id="apiStatusTable">
        <thead>
            <tr>
                <th>API名称</th>
                <th>API状态</th>
            </tr>
        </thead>
        <tbody>
            <!-- 动态渲染内容 -->
        </tbody>
    </table>

    <script>
        // 读取Mule传入的Payload并解析为数组
        const apiData = ${payload};
        const tableBody = document.querySelector('#apiStatusTable tbody');

        // 遍历数据生成表格行
        apiData.forEach(api => {
            const row = document.createElement('tr');
            const nameCell = document.createElement('td');
            nameCell.textContent = api.apiName;
            
            const statusCell = document.createElement('td');
            statusCell.textContent = api.status;
            // 根据状态添加样式类
            statusCell.classList.add(api.status === 'UP' ? 'status-up' : 'status-down');
            
            row.appendChild(nameCell);
            row.appendChild(statusCell);
            tableBody.appendChild(row);
        });
    </script>
</body>
</html>

关键说明

  • Parse Template组件会自动把${payload}替换为流中传入的数组数据,JS拿到数据后遍历生成对应表格行
  • CSS做了基础的表格美化,同时根据API状态设置不同字体颜色,状态展示更直观
  • 整个实现完全动态,API数量变化时表格行数会自动适配,无需硬编码任何API名称或状态

内容的提问来源于stack exchange,提问作者Mule-Newbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:00:00