从 javascript 发送数据表中的 JSON 个对象

Sending JSON Objects in DataTables from javascript

我正在尝试从 JSON 对象填充数据表,但出现以下错误:

DataTables warning: table id=filteredAlertsTable - Requested unknown parameter 'messageId' for row 0. For more information about this error, please see http://datatables.net/tn/4

这是我的代码:

var dataSet = '${data}';
$('#filteredAlertsTable').dataTable( {
        "bProcessing": true,
        "aaData": dataSet,
        "aoColumns": [
                      { "mData": "messageId" },
                      { "mData": "host" },
                      { "mData": "creationTime" },
                      { "mData": "resolvedTime" },
                      { "mData": "severity" },
                      { "mData": "alertText" }
        ]
    } );

我正在从 javascript 变量中获取 dataSet,我尝试传递以下内容,

[{
    "severity": "Severity",
    "creationTime": "CreationTime",
    "resolvedTime": "ResolvedTime",
    "appName": "AppName",
    "host": "Host",
    "messageId": "MessageId",
    "alertText": "AlertText"
},
{
    "severity": "Severity1",
    "creationTime": "CreationTime1",
    "resolvedTime": "ResolvedTime1",
    "appName": "AppName1",
    "host": "Host1",
    "messageId": "MessageId1",
    "alertText": "AlertText1"
}]

{
    "mData": [{
        "severity": "Severity",
        "creationTime": "CreationTime",
        "resolvedTime": "ResolvedTime",
        "appName": "AppName",
        "host": "Host",
        "messageId": "MessageId",
        "alertText": "AlertText"
    },
    {
        "severity": "Severity1",
        "creationTime": "CreationTime1",
        "resolvedTime": "ResolvedTime1",
        "appName": "AppName1",
        "host": "Host1",
        "messageId": "MessageId1",
        "alertText": "AlertText1"
    }]
}

我已经想到了 this 问题,但我仍然无法弄清楚哪里出了问题

这是我的 HTML 代码。

<table id="filteredAlertsTable"
    class="table table-striped table-bordered ">
    <thead>
        <tr>
            <th>Message Id</th>
            <th>Host</th>
            <th>Creation Time</th>
            <th>Resolved Time</th>
            <th>Severity</th>
            <th>Alert Text</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

因为dataSet是一个字符串。很可能是由 '${data}' 引起的,因此 dataTable 看起来像这样:

var dataSet = '[{ "severity": "Severity", "creationTime": "CreationTime", "resolvedTime": "ResolvedTime", "appName": "AppName", "host": "Host", "messageId": "MessageId",    "alertText": "AlertText"},{    "severity": "Severity1",    "creationTime": "CreationTime1", "resolvedTime": "ResolvedTime1", "appName": "AppName1", "host": "Host1", "messageId": "MessageId1", "alertText": "AlertText1"}]';

与 JSON 一样,它立即起作用 -> http://jsfiddle.net/q78tu48q/

因此请确保您的字符串实际上 JSON 格式:

$('#filteredAlertsTable').dataTable( {
        "bProcessing": true,
        "aaData": JSON.parse(dataSet),
        "aoColumns": [
                      { "mData": "messageId" },
                      { "mData": "host" },
                      { "mData": "creationTime" },
                      { "mData": "resolvedTime" },
                      { "mData": "severity" },
                      { "mData": "alertText" }
        ]
    } );

演示 -> http://jsfiddle.net/u76bLpa3/