0

I have a MVC page with a JQuery DataTable that I want to populate when the user clicks the Query Button.

Here is my DataTable:

<table id="mytable" class="table table-striped table-bordered mt-5">
<thead>
    <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
        <th>Column 4</th>
        <th>Column 5</th>
        <th>Column 6</th>
        <th>Column 7</th>

    </tr>
</thead>
<tbody></tbody>
</table>

Here is where I initialize the table (table is empty when page loads and not to be populated until user clicks the Query button):

$('#mytable').DataTable({
    processing: true, // for show progress bar
    order: [[2, "desc"]],
    dataSrc: 'vm',
    dom: 'rfltip',
    bRetrieve: true,
    iDisplayLength: 50,
    columns: [
        { "data": "COL1" },
        { "data": "COL2" },
        { "data": "COL3" },
        { "data": "COL4" },
        { "data": "COL5" },
        { "data": "COL6" },
        { "data": "COL7" }
    ]
});

And here is the function that gets called when the user clicks the Query button:

function QueryEvents() {
      $.ajax({
            url: "/MyController/MyJsonRequest",
            type: "get",
            data: {
                id: $("#id").val()
            },
            error: function (response) {

                console.log("response: " + response);
            }

        })
        .done(function (result) {              

            var table = $('#mytable').DataTable();
            table.clear().draw();
            console.log('good to here' + JSON.stringify(result));
            table.rows.add(result).draw();

        });
};

The call to the controller is working find and is returning a Json object. Here is the object listing from the console.log entry, so I know data is being returned:

good to here{"vm":[{"COL1":"0000000017","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"smith, joe","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"},{"COL1":"0000000018","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"jones, billy","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"}]}

It is well formed json and I am not getting any errors, but the dataTable is not populated. Not sure what I am missing?

1 Answer 1

1

Following is your object which you printed on console.

{"vm":[{"COL1":"0000000017","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"smith, joe","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"},{"COL1":"0000000018","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"jones, billy","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"}]}

But DataTable only requires inner array.

[{"COL1":"0000000017","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"smith, joe","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"},{"COL1":"0000000018","COL2":"Balance - 4 Place","COL3":"2334234","COL4":"INS1234","COL5":"jones, billy","COL6":"Yearly Maint","COL7":"/Date(1582314988910)/"}]

So you should use

table.rows.add(result.vm).draw();

instead of

table.rows.add(result).draw();
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.