• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

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?
0
Wayne Atherton
Asked:
Wayne Atherton
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now