When I update the data with AJAX, Child rows it fails

When I update the data with AJAX, Child rows it fails

feng xiafeng xia Posts: 9Questions: 3Answers: 0
edited February 2021 in Free community support

Everything was normal at first

Child rows code

var table = $('#ple').DataTable();
function format ( d ) {
        // `d` is the original data object for the row
        var order_table = "<div class= \"row\"><div class=\"col-10\">"+d+"</div></div>";
        // order();
        return order_table
    }
$('#ple tbody').on('click', 'td', function () {
            var tr = $(this).closest('tr');
            var row = table.row( tr );
            alert('ok'+row.data()[0])
            $.ajax({
                url:'/queryseller/queryproducts',
                type: 'POST',
                data:{
                    date:row.data()[0],
                    csrfmiddlewaretoken:$('[name=csrfmiddlewaretoken]').val(),
                },
                success:function(res){
                    if ( row.child.isShown() ) {
                        row.child.hide();
                        tr.removeClass('shown');
                    }
                    else {
                        row.child( format(res) ).show();
                        tr.addClass('shown');
                    }
                }
            });
        });
    })

ajax code

    $("#Button").click(function () { 
            $.ajax({
                url:'/queryseller/queryseller',
                type: 'POST',
                data:{
                    date:$("#test1-1").val(),
                    csrfmiddlewaretoken:$('[name=csrfmiddlewaretoken]').val(),
                },
                success:function(res){
                    $("#ple").DataTable().destroy();
                    $("#ple").DataTable({
                        data:res.data,

                });

                }
            })

The child code is disabled when the button is clicked to get the data

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975
    edited February 2021 Answer ✓

    Why are you destroying the Datatable? There is no need to if all you are doing is updating the table data. Use clear() followed by rows.add() to reload the data. For example:

                success:function(res){
                    $("#ple").DataTable().clear();
                    $("#ple").DataTable().rows.add( res.data ).draw();
    

    It sounds like destroying the Dataable is removing the event handler.

    Kevin

  • feng xiafeng xia Posts: 9Questions: 3Answers: 0

    hi,kthorngren,Thank you for your answer, it solved my problem

This discussion has been closed.