0

I am currently programming a crawler in Python. This crawler is storing data in a mongoDB database. I would like to load the data dynamically into a table without having to reload the whole page.

My current (relevant) code is looking like this:

var $tagsLabel = $("<lable>")
var $tagsInput = $("<textarea>");

/* Formatting function for row details - modify as you need */
function format(d) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
        '<tr>' +
        '<td>Raw text:</td>' +
        '<td>' + d.Raw_html + '</td>' +
        '</tr>' +
        '</table>';
}

$(document).ready(function () {
    $table = $("#dataTable")
        .on("preInit.dt", function (e, settings) {
            $tagsLabel.append($tagsInput);
            $('.dataTables_tags').append($tagsLabel)
        })
        .on("init.dt", function (e, settings) {
            $tagsInput.tagEditor({
                delimiter: ', ',
                placeholder: 'Enter search tags ...',
                onChange: function (field, editor, tags) {
                    if (tags.length) {
                        if (tags.length > 1) {
                            $table.search(tags.join('|'), true, false).draw();
                        } else {
                            $table.search(tags[0]).draw();
                        }
                    } else {
                        $table.search('').draw(true);
                    }
                },
            });
        })
        .DataTable({
            "dom": '<l<"dataTables_tags"><t>ip>',
            "ajax": '/json-int',
            "columns": [
                {
                    "class": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": '',
                    width: "5px"
                },
                {"data": "Timestamp", width: "135px"},
                {"data": "Title"},
                {"data": "Url"},
                {"data": "domain"},
                {"data": "Type"},
                {"data": "Raw_html", "visible": false}
                //{"data": "Raw_html", "visible": false}

            ],
            "order": [[1, 'asc']]
        });


    // Add event listener for opening and closing details
    $('#dataTable tbody').on('click', 'td', function () {
        var tr = $(this).closest('tr');
        var row = $table.row(tr);

        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        } else {
            // Open this row
            row.child(format(row.data()), 'no-padding').show();
            tr.addClass('shown');

        }
    });

    $('#dataTable tbody').on('click', 'td > button', function (e) {
        alert('Tada!');
        return false;
    });
});



<table id="dataTable" class="table table-striped table-hover table-bordered table-condensed"
                           style="width: 100%; font-size: 12px" role="grid">
                        <thead>
                        <tr>
                            <th></th>
                            <th>Timestamp</th>
                            <th>Title</th>
                            <th>URL</th>
                            <th>Domain</th>
                            <th>Page Type</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th></th>
                            <th>Timestamp</th>
                            <th>Title</th>
                            <th>URL</th>
                            <th>Domain</th>
                            <th>Page Type</th>
                        </tr>
                        </tfoot>
                    </table>

The function I'm trying to use to update the table dynamically:

    function updateTable() {
    $(document).ready(function () {
        var t = $('#dataTable').DataTable();
        $.ajax({
            type: "GET",
            url: "/json",
        }).done(function (data) {

            var parsed = JSON.parse(data);

             parsed.forEach((obj, i) => {
                t.row.add([
                    obj.Timestamp,
                    obj.Title,
                    obj.Url,
                    obj.domain,
                    obj.Type,
                    obj.Raw_html
                ]).draw(false);
            });
        }).fail(function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR, textStatus, errorThrown);
        })
    });
}
setInterval(updateTable, 5000);

The table is loading fine and if I reload the whole page, the data is loaded into the DataTable. Every time the function is running to update the table dynamically, I get the following error:

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

I posted an example of the data I am trying to load dynamically into the table: https://myjson.com/re9cu

Hopefully someone can point me in the right direction.

EDIT: Sorry, maybe I did not describe the question clear enough. When I load the page the data is loaded. But I would like to add new rows to the table when there is new data. the /json is providing this new data each time it is requested.

1
  • Sorry, maybe I did not describe the question clear enough. When I load the page the data is loaded. But I would like to add new rows to the table when there is new data. the /json is providing this new data each time it is requested. Commented Dec 6, 2018 at 11:23

1 Answer 1

3

You don't need to invent your own bicycle, it's already there https://datatables.net/examples/data_sources/ajax.html. You simply should extend your DataTable initialization with corresponding parameter:

var t = $('#dataTable').DataTable({
    ajax: '/url/to/script/feeding/data',
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thats exactly what I am currently doing to load the data on page load. But I would like to add new rows with new data to the table. I've added that to the question.
To update your data from source, simply invoke ajax.reload() method datatables.net/reference/api/ajax.reload()

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.