JQuery DataTables sub table error

I need some help with a function, that returns:

Uncaught TypeError: Cannot read property 'show' of undefined

DataTables .net (jquery) has on their website an example, of opening a sub table using the following function:

$('#table tbody').on('click', 'td.details-control', 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())).show();
        tr.addClass('shown');
    }
});

Open in new window


Now, the row.data() contains all the necessary information, that is displayed. So, as an example, lets say the main table has 10 cols, and 5 of those are not displayed initially. Clicking on the table, sends the entire row, to the 'format' function, and created a table like so:

function format(d) {


    //var table = '<table class="table-user-information" border="0" style="padding-left:50px;width:50%">';
    var table = '<table id="tblGoodGuyAudit" class="table table-bordered table-responsive">';
    table += '<thead>';
    table += '<tr>';
    table += '<th>User Name</th>';
    table += '<th>Comment</th>';
    table += '<th>Date</th>';
    table += '<th>Status</th>';
    table += '</tr>';
    table += '</thead>';
    table += '<tbody>';

    

    $.each(d.object, function (index, element) {
        table += '<tr>';
        table += '<td>' + object.item+ '</td>';
        table += '</tr>';
    });
   
    table += '</tbody>';
    table += '</table>';
  
    return table;
}

Open in new window


Ok, so this all works fine. However, in my example, I want to pass the entire row, use an Id, to retrieve further data, to then populate my table. As such:

function format(d) {
   
    var data;
    var table;
  
    $.ajax({
        type: "POST",
        url: "Profiles.aspx/GetMessageTypeDuplicateValues",
        cache: false,
        data: JSON.stringify({ messageTypeId: d["MessageType"] }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr) {

            var contentType = xhr.getResponseHeader("Content-Type");
            if (xhr.status === 401 && contentType.toLowerCase().indexOf("text/html") >= 0) {
                window.location.reload();
            }
        },
        success: function(result) {
           data =  jQuery.parseJSON(result.d);
           table = '<table id="tblMessageTypeDuplicateValues" class="table table-bordered table-responsive">';
           table += '<thead>';
           table += '<tr>';
           table += '<th>MT Tag</th>';
           table += '</tr>';
           table += '</thead>';
           table += '<tbody>';


           $.each(data, function (index, element) {

               table += '<tr>';
               table += '<td>' + element["MtTag"] + '</td>';
               table += '</tr>';
           });
          
           table += '</tbody>';
           table += '</table>';

            console.log(table);
        }
    });

    return table;
    
}

Open in new window


the console.log(table) reports back a correct table, but the browser responds with:
Uncaught TypeError: Cannot read property 'show' of undefined

Why is this?
LVL 1
Wayne AthertonProduct Analyst Financial Messaging LondonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
this is due to the asynchronous ajax call. The return table in line 45 will be empty, as it returns far earlier than your ajax response.
You should be able to verify this by adding a console.log(table) in line 44.

You might try to set the ajax call to async : false:
 $.ajax({
        type: "POST",
        url: "Profiles.aspx/GetMessageTypeDuplicateValues",
        cache: false,
        async: false,
...

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
@Rainer, many thanks that's now returned data. I do get this warning now, though:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
jquery.js:6 XHR finished loading: POST "http://localhost:50830/Profiles.aspx/GetMessageTypeDuplicateValues".

Do I need to be concerned about this?
0
Rainer JeschorCommented:
Hi,
this depends. As it is now a synchronous call, it will block the UI until the call is finished. If your call is fast, it should not affect your page that much, if you experience delays, you might have to think about a different approach.
Thanks.
Rainer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.