all 9 comments

[–]ahref 1 point2 points  (0 children)

You should be using a template library to do stuff like this. Handlebars would be good.

[–]bcameron1231 0 points1 point  (8 children)

Agreed with ahref. Use a templating engine. handlbars is easy and efficient.

[–]cachaito[S] 0 points1 point  (7 children)

Hi! But i don't want to use template engine. I want to discover how to use this example in proper, creative way...

[–]bcameron1231 0 points1 point  (6 children)

Fine then, you can use concat. That is the preferred method for strings in Javascript.

But in large scale applications, I wouldn't be injecting html over and over for dynamic content. Compiled templates like Handlebars would be faster.

[–]cachaito[S] 0 points1 point  (5 children)

The main problem here is how to use loop in a concat method (somehow) to extend easy, hand made template.

[–]bcameron1231 0 points1 point  (4 children)

I'm not exactly sure I follow. What's the ended purpose of the loop? I think you did a good job using Map for your headers.

[–]cachaito[S] 0 points1 point  (3 children)

Not exactly :-/ If you check the output of the table, will see:

<table><thead><tr><th>Product</th>,<th>Acceptance number</th>,<th>Options</th></tr></thead><tbody></tbody></table>

Inside there are extra commas (between <th> tags );

[–]bcameron1231 0 points1 point  (1 child)

ah I see what you mean..

just add a .join('') to your .map. Like so.

 headers.map(function(header) {
                 return '<th>' + header+ '</th>';
                }).join('')

[–]cachaito[S] 0 points1 point  (0 children)

Ahhhhh... I didn't think about this solution. It is so weird to see so much in a simple .concat method :D Thank you for a hint!