jQuery Datatables use function as ajax data source instead of path or url -


how use functions data source ajax datatables? i'm using in electron application built in database i'm calling function, writing results data.json file, , using file source:

populatedata(); //gets data db , writes output data.json  let table = $('#acctransaction').datatable({       dom: 'bfrtip',       select: {         style: 'single'       },       ajax: '../data.json',       ... 

instead, more efficient if call db function directly it's not working....not valid url.

let table = $('#acctransaction').datatable({       dom: 'bfrtip',       select: {         style: 'single'       },       ajax: getdata(),       ... 

i realize can make source 'data: newdata,' loose ablility call table.ajax.reload

setinterval( function () {   table.ajax.reload( null, false ); // user paging not reset on reload   console.log('reloading..') }, 3000 ); 

how use functions data source ajax datatables instead of url's or file paths?

dont know if worth, wrap ajax call function, , in function reinitialise table using destroy flag. demo below :

function reload(url) {   $.ajax({     url: url,     type: 'get',     datatype: 'json'   })   .done(function(json) {      console.log(json) //check reponse      return $('#example').datatable({         destroy: true,         data : json.data,         columns: [            { data: 'name', title: 'name' },            { data: 'position', title: 'position' }         ]      })   }) } 

now can refresh datatable by

reload('url/to/json') 

where can change url anything, remember change columns definition ( or build dynamically). done using interval :

setinterval(function() {    reload('https://api.myjson.com/bins/38p4r') }, 1000)    

demo -> http://jsfiddle.net/zefl681c/


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -