0

I am trying to populate datatable from JSON object but getting the following error:

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

Here is my code:

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

I am getting dataSet from javascript variable, I tried passing the following,

[{
    "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"
}]

and

{
    "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"
    }]
}

I have gone thought this question but I still can't figure out what's wrong

Here is my HTML code.

<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>
2
  • I suggest pass the same format back as json which matches your initialization in dataTables. For ex: if messageId is the first mData during initialization append it as first element of array in json object which is passed back!! Commented May 15, 2015 at 10:04
  • From my experience with DataTables and that sort of error, you should double-check your HTML and be sure you have as many rows as you have table headers. This might be the issue, can't tell for sure until I see the HTML Commented May 15, 2015 at 10:05

1 Answer 1

1

It is because dataSet is a string. Most likely caused by '${data}' so it for the dataTable looks like this :

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"}]';

As JSON it works right away -> http://jsfiddle.net/q78tu48q/

So make sure your string actually is in JSON format :

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

demo -> http://jsfiddle.net/u76bLpa3/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.