[Answer]-How to get data of all rows from a dynamically generated table by jQuery

1👍

Without any additional libraries, something like this:

(1) get the data from the table

function getTableData()
{
    // Array of data we'll return
    var data = [];

    // Counter
    var i = 0;

    // Cycle through each of the table body's rows
    $('tbody tr').each(function(index, tr) {
        var tds = $(tr).find('td');
        // Check we've got two <td>s
        if (tds.length > 1) {
            // If we do, get their text content and add it to the data array
            data[i++] = {
                email: tds[0].textContent,
                password: tds[1].textContent
            }
        }
    });
    return data;
}

(2) post it to the server

$.ajax({
    method: 'post',
    url: '', // Set the URL of whatever in Django will handle your post
    data: getTableData()
});

However, if I were attempting this task, I would achieve (1) by using Knockout, which would allow for a much better separation between view and viewmodel (or between template and view, you might think of them, as a Django user). Great that you’re using a decent server-side framework, would be a pity to end up with spaghetti code on the client!

Leave a comment