mercredi 19 octobre 2016

JQuery Get data from specific HTML table, when there's more then one table. Check boxes are involved

This is for a Web Api

I have multiple HTML tables and I want to get the data from a specific one. At the moment I use check boxes to select the data then I press a button located under each table. Currently if I select data from all of my table and press one of the buttons, the data selected regardless of table, will be posted to my back end code.

The following code is for one table.

To populate the table I use this code.

    var Count = 0;
    $.ajax({
        url: 'http://localhost/Dynamic?table=' + tableName,
        dataType: 'Json',
        success: function (tableResults) {
            $.each(tableResults, function (index, value) {
                if ($('#data th:last-child').length === 0) {
                    $('#data thead').append('<th>' + value + '</th>');
                } else {
                    $('#data th:last-child').after('<th>' + value + '</th>');
                }
            });
            $('#data th:last-child').after('<th><a id=Count</a>Allow</th>');
        }
    });

    $.ajax({
        url: 'http://localhost/Dynamic?dif=' + tableName,
        dataType: 'Json',
        success: function (Results) {
            $.each(Results, function () {
                var row = "";
                var rowData = [];
                rowData.push(tableNameglobal);
                for (i = 0; i < this.length; i++) {
                    var input = '<td>' + this[i] + '</td>';
                    rowData.push(this[i]);

                    row = row + input;
                }

                $('#data tbody:last-child').append('<tr>' + row + '<td> <input class="checkBox" type="checkbox" name="' + Count + '"/> </td></tr>');
                TableData.push(rowData);
                Count++;
            });
        }
    });

And then once I've pressed the button this method is called

$(document).ready(function () {
    $('#Add').click(function () {
        var id = [];
        $('Count:checkbox:checked').each(function (i) {
            id.push(this.name);
        });
        if (id.length === 0) {
            alert("Please select at least one checkbox");
        }
        else {
            var postData = [];

            for (i = 0; i < id.length; i++) {
                postData.push(TableData[id[i]]);
            }

            $.post('http://localhost/Dynamic/update', { 'Update': postData }, 'Json');

        }
    });
});

Does anyone know how I can get the data from a specific table?




Aucun commentaire:

Enregistrer un commentaire