javascript - How to get the key of ajax response array in order to build up header of datatable? -


i have $.post query, retrieving json data ajax response. issue want load data in datatable , header of table need created dynamically.

my json data comes in format:

[{"id":"11105","name":"gummy drop (iphone, free, row except cn, 72.3mb, w"},{"id":"11107","name":"gummy drop (ipad, free, row except cn, 72.3mb, w\/ "},{"id":"4274","name":"z-redirect non-targeted traffic dragon city mobile"},{"id":"6484","name":"z-redirect non-targeted traffic dragon city mobile"}] 

as can see, 2 keys id , name. want 2 keys , use header column unable so.

here's code:

$(document).ready(function() {     window.prettyprint() && prettyprint();       $('#load').click(function()     {         var v = $('#drp_v').val();         var cnt = $('#drp_cnt').val();         var ctg = $('#drp_ctg').val();         alert("version :"+v+" category :"+ctg+" country :"+cnt);         $.post("ajax.php",{'version':v,'category':ctg,'country':cnt,'func':'show_datatable'},                         function(data)                         {                             var acolumns = [{ stitle: "week"}, { stitle: "winkelformule" }];                             var otable = $('#jsontable').datatable({                                 "columns":acolumns,                                 "sdom": 't<"clear">lfrtip',                                     "otabletools": {                                          "abuttons": [                                                {                                                     "sextends": "csv",                                                     "sbuttontext": "csv",                                               }                                         ]                                     }                              });                             otable.fncleartable();                             alert(data);                             for(var = 0; < data.length; i++)                              {                                  otable.fnadddata([                                             data[i].id,                                             data[i].name                                                    ]);                                                                   } // end                         },'json');     }); }); 

as can see line:

var acolumns = [{ stitle: "week"}, { stitle: "winkelformule" }]; creating <thead>.

now 2 issues are:

a) want use keys on here thead can dynamic.

b) secondly, can see initialising datatable upon ajax call. means every time ajax called, datatable initialised. want eliminate that. want datatable initialised once, <thead> created each time ajax call fired. know create <thead> during initialisation of datatable.

object.keys looking for.

response = $.post(your_ajax_paage) acolumns = [{ stitle: "week"}, { stitle: "winkelformule" }];  registerredkeys = []; for(i = 0; i<response.length; i++){       keycolumns = object.keys(response[i]); //get keys of each object       for(j = 0; j < keycolumns.length; j++){           if($.inarray({stitle: keycolumns[j]},registerredkeys)<=0){                acolumns.push({stitle: keycolumns[j]}) //checks if                 registerredkeys.push( keycolumns[j]);           }       } } 

then render acolumns. solves problem a. problem b need craft json response can keys before hand otherwise you'd end initializing thead on , overagain because don't know data. keep running list see if new columns have been registered. (make previouskeys public , run after above piece of code.

if(object.tojson(registerredkeys) === previouskeys){      //ignore  }else{    previouskeys = object.tojson(registerredkeys)    //render new thead } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -