How to hide only some child rows which are empty

How to hide only some child rows which are empty

[Deleted User][Deleted User] Posts: 0Questions: 6Answers: 0

Referencing to: https://datatables.net/examples/api/row_details.html

Let's say that I have a few rows without an extension number. Therefore I don't want to show the line "Extension number: [...]" only in the few ones which don't have one. I am not quite sure how to do that.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    in the example the format function builds the child row display. You can use if statements to see if there is a value for extension and only append the row to the string if there is.

    Kevin

  • [Deleted User][Deleted User] Posts: 0Questions: 6Answers: 0
    edited December 2019

    How does that statement have to look like? My empty extn entries have the value "empty"

    I tried that in the format function:

    if(d.extn == "empty"){
    // dont show anything
    }
    else{
    '<td>Extension number:</td>'+
    '<td>'+d.extn+'</td>'+

    But this isnt working.

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    edited December 2019 Answer ✓

    I would guess that it has to do with how you are appending the strings. Likely you are getting an error in your browse's console due to where you have the + placed.

    Maybe something like this:

    EDIT: Made a couple changes.

    function format ( d ) {
        // `d` is the original data object for the row
        var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
            '<tr>'+
                '<td>Full name:</td>'+
                '<td>'+d.name+'</td>'+
            '</tr>';
            if (d.extn !== "empty") {
            html +=  '<tr>'+
                  '<td>Extension number:</td>'+
                  '<td>'+d.extn+'</td>'+
              '</tr>';
            }
            html +=  '<tr>'+
               + '<td>Extra info:</td>'+
               +  '<td>And any further details here (images etc)...</td>'+
            + '</tr>'+
        '</table>';
        return html;
    }
    

    I didn't try this so there may be errors. If you still have problems please build a simple test case showing what you are trying to do so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.