Search code examples
jqueryajaxdatatablesreload

remove selected class after get Ajax data and draw() DataTable


I'm trying to reload the selected row when user click on Edit button from database, but after reload the row and call table.row().data(response).draw(false);, remove selected class from the row.

$.ajax({
    type: callMethod,
    url: url + '/' + value,
    timeout: 3000,
    error : function(jqXHR, textStatus, errorThrown) {
        
        //create notification
        var notifyOptions = {
                title: '<h4>Error 500</h4>',
                text: '<p>'+ textStatus + '. ' + errorThrown + '.</p>',
                type: 'error'
        }
        
        show_notification(notifyOptions);
        
    },
    success: function(response) {
        
        table.row().data(response).draw(false);
        
        table.row(selected).select();
        
    }
}).done(function(data) {

    table.row(selected).select();

});

I writed table.row(selected).select(); in success and done but that doesn't work.

I used too:

table.on( 'draw.dt', function () {
    $(table.context[0].aoData[selected].nTr).addClass('selected');
});

but I understand I can't use that. because that has error in remove row.

thanks


Solution

  • I find a solution, that work in DataTables, I override functions:

    After edit and view row:

    $.ajax({
        type: callMethod,
        url: url + '/' + value,
        timeout: 3000,
        error : function(jqXHR, textStatus, errorThrown) {
            
            //create notification
            var notifyOptions = {
                    title: '<h4>Error 500</h4>',
                    text: '<p>'+ textStatus + '. ' + errorThrown + '.</p>',
                    type: 'error'
            }
            
            show_notification(notifyOptions);
            
        },
        success: function(response) {
            
            table.row().data(response).draw(false);
            
            table.on( 'draw.dt', function () {
                $(table.context[0].aoData[selected].nTr).addClass('selected');
            });
            
        }
    }).done(function(data) {
    
        ...
    
    });
    

    After remove row:

    $.ajax({
        type: callMethod,
        url: url + '/' + value,
        timeout: 3000,
        error : function(jqXHR, textStatus, errorThrown) {
            
            //create notification
            var notifyOptions = {
                    title: '<h4>Error 500</h4>',
                    text: '<p>'+ textStatus + '. ' + errorThrown + '.</p>',
                    type: 'error'
            }
            
            show_notification(notifyOptions);
            
        },
        success: function(response) {
            
            table.off( 'draw.dt');
            
        }
    }).done(function(data) {
    
        ...
    
    });