0

I'm trying to come up with the best way to generate a table from the below JSON object:

mytable=[
    {div:{
        nested:[
            {table:{
                nested:[
                    {thead:{
                        nested:[
                            {tr:{
                                nested:[
                                    {th:{}},
                                    {th:{}},
                                    {th:{}}
                                ]}}
                        ]
                    }},
                    {tbody:{}}
                ]}}
        ]}}
];

The end result would generate the HTML elements and be structured like so:

<div>
    <table>
        <thead>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

My logic is to check if the the object has the property nested if so generate the element and continue to loop, however I can't figure out how to do the bind the child elements back to the parents at the same time.

17
  • You dont need to return the table built inside the json. Just return the data and build the html with javascript Commented Mar 18, 2016 at 19:52
  • @VitorRigoni I know what your saying but thats not what I'm looking for, I want to use a more dynamic approach using a loop to speed up development. Commented Mar 18, 2016 at 19:55
  • Your JSON is not valid. You have a problem with your nested th. Plus, what have you tried? Commented Mar 18, 2016 at 19:56
  • Just an idea but you could turn your json into a xml (there are many tools for this) and you would have instant html :) Commented Mar 18, 2016 at 19:57
  • @cl3m nice catch sorry fixing now. Commented Mar 18, 2016 at 19:58

1 Answer 1

1

this does the work:

var mytable = /* your json */

function toHTML(j, n) {

    n = n || document.createDocumentFragment('div');
    for (var i = 0, o, l = j.length; i < l; i++) {
        for (var tag in j[i]) {
            o = document.createElement(tag);
            n.appendChild(o);
            'nested' in j[i][tag] && toHTML(j[i][tag].nested, o);
        }
    }
    return n;
}
var res = toHTML(mytable);

// now append it where needed
document.body.appendChild(res);
Sign up to request clarification or add additional context in comments.

10 Comments

You sir are a beast! I love the shorthand to. If you live in the SF Bay Area and need a job we will def hire you :)
thx @JordanDavis, for the moment I'm ok in Switzerland...never say never! ;)
@fedeghe I'm looking for an OOP way to do the same thing but for server responses you got anything? stackoverflow.com/questions/36581180/…
@JordanDavis, since your question is a bit vague I refer to the link to the other one... I've worked a lot on that and the answer I gave here is just a hyperdistilled version of that github.com/fedeghe/widgzard ... worth a look, hope helps... Btw is not clear why You say 'for server responses'... Why should matter from where the json comes from?
@fedeghe I posted a link to the bottom of that comment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.