old data is appended after AJAX call in datatable

old data is appended after AJAX call in datatable

mailpayalvermamailpayalverma Posts: 1Questions: 1Answers: 0

I am trying to implement a complex query function which filters the records in a datatable, the problems is that the old search results are always getting appended to the new results although the data coming from the back-end is correct. Our code looks like this

$.ajax({
url: '/patients/getPatientsByQuery',
method: 'POST',
cache: false,
stateSave: false,
dataType: 'json',
bServerSide: false,
data: {
sql: result.sql,
getRule: getRulesResult,
params: JSON.stringify(result.params, null, 2)
},
success: function(filteredData) {

        console.log('data', JSON.stringify(filteredData));
        var table = $('#patientlist').DataTable();
           table.clear().draw();
           table.rows.add(filteredData.data);
           table.columns.adjust().draw();



      }

The problem is filteredData is appending the old result with new one. We dont know how to clear it on each ajax call.Would you please help me that .

This discussion has been closed.