javascript - Sort-arrow in datatable appears more than once if destroy is set to true -


i need reinitialize datatable on every click of submit button. set destroy : true achieve this. every time datatable re-initializes, number of sort-arrows appear each column increases one. how can prevent happening? here javascript code. every time user clicks on submit, processformdata called, calls initializedatatable after validation of form fields.

var firstname = ""; var lastname = ""; var country = "";  $(document).ready(function() {     $('#userdetails').datatable(); });  function processformdata() {      firstname = document.userdetailstable.firstname.value;     lastname = document.userdetailstable.lastname.value;     country = document.userdetailstable.country.value;      if (document.userdetailstable.firstname.value == ""             || document.userdetailstable.lastname.value == ""             || document.userdetailstable.country.value == "") {         bootbox.alert("please fill in fields!");     } else {          initializedatatable();     }     return false; }  function initializedatatable() {      $('#userdetailstable').datatable({                 "destroy" : true,         "pagelength" : 12,         "processing" : true,         "serverside" : true,         "ajax" : {             "url" : '/fetchsearchresults',             "type" : 'post',             "data" : {                 "length" : 12,                 "formfirstname" : firstname,                 "formlastname" : lastname,                 "formcountry" : country             }         }     });  }; 

it appears approach use delete option should work in theory. i'm not taking path try , reproduce without knowing if we've possibly got different setup anyway. instead recommend using ajax.reload() function updated ajax.data update data held in table. possible adjustment of code this:

function initializedatatable() {     return $('#userdetailstable').datatable({             "destroy" : true,         "pagelength" : 12,         "processing" : true,         "serverside" : true,         "ajax" : {             "url" : '/fetchsearchresults',             "type" : 'post',             "data" : {                 "length" : 12,                 "formfirstname" : firstname,                 "formlastname" : lastname,                 "formcountry" : country             }         }     }); };  var processformdata = (function(){     var table = null;     return function(){         var firstname = document.userdetailstable.firstname.value;         var lastname = document.userdetailstable.lastname.value;         var country = document.userdetailstable.country.value;          if (firstname == "" || lastname == "" || country == "") {             bootbox.alert("please fill in fields!");         } else if (table !== null) {             table.ajax.data.firstname = firstname;             table.ajax.data.lastname = lastname;             table.ajax.data.country = country;             table.ajax.reload();         } else {             table = initializedatatable();         }     }; })(); 

calling processformdata() should give updated table. i've not tested code though.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -