/    Sign up×
Community /Pin to ProfileBookmark

Dynamically creating an html table from an array of objects with mustache

I make an ajax call that returns a dataset in json which looks like this: [URL=”http://imgur.com/HyeSzYb”]http://imgur.com/HyeSzYb[/URL]

Everything (including the correct column names) has already been taken care of via DB views so I wanted to write a script that just grabs a dataset and spits it out in nicely formatted html. This way the view can be changed (columns added and removed) and the code will not have to be updated. I initially tried to get this to work with mustache but there doesn’t seem to be a simple way of doing it. Right now I just do it in js like this:

[CODE]//Using bootstrap to pretty it up
htmlTableDataset = ‘<table class=”table table-hover table-bordered”>’;
//Get <th> headers from first object in dataset
var htmlTableHeaders = ‘<thead><tr>’;
for (var keyName in dataset[0]){
htmlTableHeaders += ‘<th>’+keyName+'</th>’;
};
htmlTableHeaders += ‘</tr></thead>’;
//Build table body
var htmlTableBody = ‘<tbody>’;
datasetLength = dataset.length;
for (var i=0; i<datasetLength; i++) {
currentRow = dataset[i];
var htmlDatarow = ‘<tr>’;
for (var prop in currentRow){
htmlDatarow += ‘<td>’ + currentRow[prop] + ‘</td>’;
}
htmlDatarow += ‘</tr>’;
htmlTableBody += htmlDatarow;
};
htmlTableBody += ‘</tbody>’;
//Finish table
htmlTableDataset += (htmlTableHeaders + htmlTableBody + ‘</table>’);
$(htmlContainer).html(htmlTableDataset);[/CODE]

This works fine, but I really would like to simplify it further with mustache (if that is possible). The problem is that its an array of objects with multiple properties- can mustache process this in a more efficient way?

edit: I’d also like to add that I am already using mustache in a bunch of other places (code I don’t feel like re-writing with a new engine right now) so if mustache can’t do it I’ll stick to pure js for the time being.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbySep 24.2014 — I'll admit, I don't know a thing about mustache, but just in plain javascript alone your script could be reduced a bit:
[CODE]
var htmlTableDataset = '<table class="table table-hover table-bordered"><thead><tr>';
for(var keyName in dataset[0]) htmlTableDataset += '<th>'+keyName+'</th>';
htmlTableDataset += '</tr></thead><tbody>';
for(var i = 0; i < dataset.length; i++) {
htmlTableDataset += '<tr>';
for(var prop in dataset[i]) htmlTableDataset += '<td>' + currentRow[prop] + '</td>';
htmlTableDataset += '</tr>';
}
htmlTableDataset += '</tbody></table>';
$(htmlContainer).html(htmlTableDataset);
[/CODE]


I realize you are looking for a solution using mustache, but I just figured in the event you do not find that solution, you could still reduce your lines of code.
Copy linkTweet thisAlerts:
@red888authorSep 24.2014 — Thanks for the response. For the sake of readability and consistency I also use brackets. The code works fine its just that I what to maintain my little work flow of: load an html template file, get a section of the template file to process, pass to mustache along with any variables to plug in. I have all my html in one nicely formatted file except for this ugly mofo. I'd like all the html to be in the template file so I only have one place to go when I need to modify the html.
×

Success!

Help @red888 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...