9

I am trying to bind a child table with a parent table. I am not able to figure out how this can be done when the data for the child table is coming through an AJAX call which then creates a dynamic table.

I have followed this

Below is my code.

$('#myTable tbody).on('click', 'td.details-control', function () {

        var tr = $(this).closest('tr');
        var row = $('#myTable').DataTable().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()).show();
            tr.addClass('shown');
        }
    });

function format() {

    $.ajax({
        type: 'GET',
        url: '@Url.Action("MyFunction", "Home")',
        data: { "Id": MyId },
        dataType: "json",
        async: false,
        success: function (data) {
            var list = data;
            $.each(list, function (index, item) {

                return '<table>.......<table />';

                //i need to loop across the list and create a dynamic table with tr and td


            });
        },
        error: function (result) {
            var error = JSON.stringify(result);
            throw "Error...";
        }
    });
}
7
  • Have you tried just adding those new rows to the existing table? Commented May 8, 2015 at 15:23
  • I added some static data just to start off and it worked fine. However, I am fetching the data from database and need to use the AJAX call. I am not able to format a dynamic table using the items in the list. Commented May 8, 2015 at 15:27
  • Instead of returning the rows into the other function, I would add them directly to the table. Commented May 8, 2015 at 15:28
  • I have followed this - datatables.net/examples/api/row_details.html Commented May 8, 2015 at 15:31
  • Think about it this way -- what you're doing is trying to grab data at the point where you click the button. What the datatables example does is grab all the data you need for both the main table display AND the detail section. You're merely adding a div on click that has elements you've grabbed from DataTables data model. So, abandon your ajax call, return all the data you need in the initial query, and on click, you'd simply display data that's already there. Commented May 8, 2015 at 15:37

1 Answer 1

8
$('#myTable tbody').on('click', 'td.details-control', function () {

    var tr = $(this).closest('tr');
    var row = $('#myTable').DataTable().row(tr);

    if (row.child.isShown()) {
        // This row is already open - close it

        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        format(row.child);  // create new if not exist
        tr.addClass('shown');
    }
});

and then the format() will be

function format(callback) {
    $.ajax({
        .... 
        //async: false, you can use async 
        success: function (data) {
            var list = data;
            var html = '';
            $.each(list, function (index, item) {
                ...
                //create <tr> <td> with loop
                html= '<tr><td>.......</tr>';
            });
            callback($('<table>' + html + '</table>')).show();
        },
        ...
    });
}

demo here jsfiddle

Sign up to request clarification or add additional context in comments.

5 Comments

any fiddle for reference...?? or any link that can be an example for this..??
added fiddle to answer
@JAG saw ur jsfiddle. I want to include css part in html. How to do that? (m new to web). kindly answer.
u can jus add css class to table like callback($('<table class="some css class">' + html + '</table>')).show();
Best example for this case. Extremely useful fiddle. Many thanks.

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.