Removing Childrow of DataTable via button.

Removing Childrow of DataTable via button.

MairinabrandonMairinabrandon Posts: 8Questions: 2Answers: 0

Good day, i'm about to delete/remove the selected row of datatable via button inside its current row. when i try

table.row($(this).parents('tr')).remove().draw();

it works fine when no child row hidden, but when i try to execute those codes when there is a child row hidden the row does not remove at all, need to re open the table to take effect.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited May 2020 Answer ✓

    Guessing you are using the Responsive extension. If so then $(this).parents('tr') won't be the correct path to the parent row. When the button is in a Responsive child row right click on it and click Inspect. Look at the path to the parent. You can use column().responsiveHidden() to determine if the column the button is in is hidden by Responsive to decide what to use to get the parent row. Here is an example:
    http://live.datatables.net/xijecupo/160/edit

    If this doesn't help then please provide a link to your page or a test case so we can see what you have. This will allow use to offer suggestions to help.

    Kevin

  • MairinabrandonMairinabrandon Posts: 8Questions: 2Answers: 0
    edited May 2020

    this works for me Sir kthorngren,.

    var table = $('#myCart').DataTable();

            var row = $(this).parents('tr');
    
                if($(row).hasClass('child')) {
                    //table.row($(this).parents('tr')).remove().draw();
                    //table.row($(row).prev('tr')).child.remove();
                    /*row.child.remove();*/
                    //table.row.removeClass('shown');
                    var irow = $(this).closest('tr.child').prev();
                    table.row(irow).remove().draw();
                    alert('row child hidden');
                }
                else
                {
                    table
                    .row($(this).parents('tr'))
                    .remove()
                    .draw();
                    alert('row child show');
                };
    

    Thank you so much for the example =)

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    What exactly is not working?

    The column().responsiveHidden() docs state this about the return value:

    true if the column is responsive visible, false otherwise.

    The return results seem opposite of the API name. I think you will want this instead:

    if ( ! table.column(4).responsiveHidden() ) {
    alert('Col is hidden');
    } else {
    alert('Col is visible');
    }
    

    If this doesn't help then please provide a link to your page or a test case (or update mine) so we can take a look at what you have.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.