alfajango / jquery-dynatable

A more-fun, semantic, alternative to datatables
http://www.dynatable.com
Other
2.77k stars 361 forks source link

cant set <th> from json | How to make dynamic tables by json #142

Open hamburger123456 opened 10 years ago

hamburger123456 commented 10 years ago

One disadvantage of dynatable.js is that you need a prepared table where the headers have to match to the json content. I would like to add a dynamic table header form json before starting dynatable. My first approach do not work: $.ajax({ url: 'abc.php', success: function(data){ data = JSON.parse(data);

$.each(data[0], function(i) { $("th", { text: i }).appendTo('#my-final-table thead'); });

var dynatable = $('#my-final-table').dynatable({ dataset: { records: data } }); } }); It inserts the th-tags in the right way (DOM) but I will get an jquery error that they aren't there. (I also tried setTimeout. Without any success) Any idea how to get dynamic tables? thx for any help in advance.

simonbengtsson commented 10 years ago

Your problem is probably that you forgot to add the tr tag, ie you should do something like this instead $("th", { text: i }).appendTo('#my-final-table thead tr');. That said, I would love to see a way to dynamically create a DynaTable without any html markup at all or just the table tag. Someone at stackoverflow wondered about the very same thing a while ago.

hamburger123456 commented 10 years ago

@someatoms You are absolut right. I copied the html-markup from http://www.dynatable.com/?sorts[country]=-1#json-from-ajax. There isn't a tr-tag in the table-head. I added one and it works like a charme. Thx for your hint.

drouillard commented 10 years ago

Keeping open for the feature request @someatoms proposes

j3g commented 10 years ago

this thread about a dynamic table in json...I am doing that. But I allow the user to select a new table. I cannot figure out how to reset the dynatable object. I basically get new table data delivered to me from a user click. But I can't get the dynatable object to reset and create from the new json. i'm stupid as i've looked at the source but still can't figure it out.

rogeraleite commented 9 years ago

Any feature developed concerning this thread?

almartos commented 8 years ago

User Guillaume from StackOverflow found a solution here:

http://stackoverflow.com/questions/29209226/dynatable-broke-with-dynamic-html-headers

and works in my case