1

I have a records in DataTable with a checkbox. I am trying to append new table to the row on clicking the checkbox. I am trying to do something like this.

Here they are taking values from row and formatting it and displaying back. But I have complete different table that I want to show.And I'm trying to achieve this with checkbox.

$('#tblEmployees').on('click', 'input[type="checkbox"].checkbox', function() {
    var tr = $(this).closest('tr');
    var Table = getTasks('Here I will pass my value to get Table through Ajax');
});

Now, I can display or hide when I check or un-check the checkbox.

Table Getting through Ajax

       function getTasks(id) {

        // Creating Inner table with little Styling
        var table = $('<table width="90%" style="margin: 0 auto;">');

        // Adding
        var titleRow = $('<tr>');
        titleRow.append('<th> Task # </th>');
        titleRow.append('<th> Task Description </th>');
        table.append(titleRow);

        $.ajax({
            url: '@Url.Action("GetTasks", "Programs")',
            data: { id: id },
            cache: false,
            type: "GET",
            success: function (data) {
                $.each(data, function (key, value) {

                    var row = $('<tr>');

                    row.append('<td>' + value.TaskNo + '</td>');
                    row.append('<td>' + value.TaskDescription + '</td>');

                    table.append(row);
                });
            }
        });
        return table;
    }
Ali Sheikh
  • 45
  • 1
  • 1
  • 12

2 Answers2

0
function selectColumn(rowID) {
    var tr = $('#tbCategory tbody tr[data-id=' + rowID + ']').after("<tr><td>Hi, It's Worked</td></tr>");
}

when your checkbox checked then pass here table row ID then append table after tr. I hope it helps you.

Neeraj Pathak
  • 759
  • 4
  • 13
0

Can you try this.

$('#tblEmployees').on('click', 'input[type="checkbox"].checkbox', function() {
        var tr = $(this).closest('tr');
        var Table = getTasks('yourID' , tr);
    });


function getTasks(id, parentRow) {

        // Creating Inner table with little Styling
        var table = $('<table width="90%" style="margin: 0 auto;">');

        // Adding
        var titleRow = $('<tr>');
        titleRow.append('<th> Task # </th>');
        titleRow.append('<th> Task Description </th>');
        table.append(titleRow);

        $.ajax({
            url: '@Url.Action("GetTasks", "Programs")',
            data: { id: id },
            cache: false,
            type: "GET",
            success: function (data) {
                $.each(data, function (key, value) {

                    var row = $('<tr>');

                    row.append('<td>' + value.TaskNo + '</td>');
                    row.append('<td>' + value.TaskDescription + '</td>');

                    table.append(row);
                });
parentRow.append(table.html())
            }
        });
        return table;
    }
Manoj
  • 1,175
  • 7
  • 11