How to prevent initial ajax request on non serverSide?

How to prevent initial ajax request on non serverSide?

DesconhecidoDesconhecido Posts: 11Questions: 5Answers: 0

Hi there,

I want to start with a page load of an empty datatable then ajax a data request after a user action (fill in search conditions, validate input, etc).
Right now I have it setup so it ajax calls a dummy file at page load, then after user input I change the ajax URL using: table.ajax.url('NEWFILEPATH').load();

But is there a way to create the datatable object BUT prevent that dummy initial ajax request for data when NOT using serverSide processing? i.e. a deferLoading for clientSide?

Answers

  • JPaulBJPaulB Posts: 3Questions: 0Answers: 0
    edited June 2016

    Hi,
    I've been having the same issue. I solved this by simply initializing the dataTable at the moment of the user action.
    So basically instead of initializing your dataTable at e.g. (document).ready you could do it after a user submits his request (in your case the search conditions etc.)
    Something like so

    //globally available variable for your dataTable
    var table = null;
    
    $('#mySubmit').on('click',function(){
         //your validation and search conditions here ...
    
        //it is important that you destroy the table before initializing it again
         if(table != null){ 
              table.destroy();
         }
    
        table = $('#myTable').DataTable({
            ajax : {
             "url": "/myUrl.htm"
             // further arguments to "ajax" 
             }
        })
    })
    
    

    Hope it helps.
    Paul

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    The other option is to make your own Ajax call and then use rows.add() to add the rows - i.e. bypass DataTables' own ajax options.

    Allan

This discussion has been closed.