从javascript中在DataTables中发送JSON对象

时间:2015-05-15 10:00:36

标签: javascript jquery json datatable jquery-datatables

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

  

DataTables警告:table id = filteredAlertsTable - 第0行请求的未知参数'messageId'。有关此错误的详细信息,请参阅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>

1 个答案:

答案 0 :(得分:1)

这是因为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,它可以立即使用 - &gt;的 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" }
        ]
    } );

演示 - &gt;的 http://jsfiddle.net/u76bLpa3/