Erdem Güngör
Erdem Güngör

Reputation: 877

Dynamic list with javascript

I'm writing a mobile app to handle with some data from a database.

So my question is what is the best way to display the received data? I get the data as a json. I read a bit about knockout...

My problem is i dont know how much and what kind data i get. That should happen dynamically. The data could be something about personnel or information about an article.

I hope u understand my question ^^ Sry for my bad english.

Upvotes: 0

Views: 3834

Answers (2)

YCotov
YCotov

Reputation: 92

All deppends from your imagination

1) Create youe own custom dynamic list;

2) Use 3rd party one YUI http://developer.yahoo.com/yui/treeview/

Upvotes: 1

Louis Ricci
Louis Ricci

Reputation: 21086

If you just want to display the data in a user-readable way, then iterate through the json object building an HTML UL from the data.

function display(obj, result) {
    if(obj == null)
        return result;
    var ul, li;
    for(var k in obj) {
        var value = obj[k];
        if(typeof(value) == "object") {
            li = result.appendChild(document.createElement("li"));
            li.innerHTML = k + ":";
            if(value != null) {
                ul = li.appendChild(document.createElement("ul"));
                display(value, ul);
            }
        } else {
            li = result.appendChild(document.createElement("li"));
            li.innerHTML = k + ": " + value;
        }
    }
    return result;
}
document.getElementById("displayDiv").appendChild(
    display(jsonObj, document.createElement("ul"));
);

Upvotes: 1

Related Questions