javascript parse tree

2019-08-29 09:26发布

i'm writing a plugin in jquery to be a file manager i build my folder structure in php i call that php file using ajax and i return something like this

{"1":[],"css":{"admin":[],"tabs":{"skin1":{"images":[]},"skin10":{"images":[]},"skin11":{"images":[]},"skin12":{"images":[]},"skin2":[],"skin3":{"images":[]},"skin4":{"images":[]},"skin5":{"images":[]},"skin6":{"images":[]},"skin7":{"images":[]},"skin8":{"images":[]},"skin9":{"images":[]}}},"img":{"admin":[],"filemanager":[],"icons":[]},"js":{"libs":[]},"menu":{"img":[]},"walpappere":{"1":[]}}

how can i parse the info using jquery or javascript to do something like this

var mystring = ''

foreach key (would be 1,css etc)

  mystring += key

  if has children

     mystring += key

     for each children same as above until all structure is parsed

3条回答
干净又极端
2楼-- · 2019-08-29 10:08

I would also look into using jQuery with jQuery tmpl and tmpl.plus. You could then create a recursive template to render. There is a bit of a learning curve but man does it really separate markup away from your logic ect. jQuery tmpl

If the recursion is done right, it could be as easy as calling:

var data = {"1":[],"css":{"admin":[],"tabs":{"skin1":{"images":[]},"skin10":{"images":[]},"skin11":{"images":[]},"skin12":{"images":[]},"skin2":[],"skin3":{"images":[]},"skin4":{"images":[]},"skin5":{"images":[]},"skin6":{"images":[]},"skin7":{"images":[]},"skin8":{"images":[]},"skin9":{"images":[]}}},"img":{"admin":[],"filemanager":[],"icons":[]},"js":{"libs":[]},"menu":{"img":[]},"walpappere":{"1":[]}};

//create the recursive template
$.template('myFolders'," ====this is the hard part here==== ");

//some javascript to add functionality to each nested list
var postProcess = function(item){
    $(item.nodes[0]).click(function(e){
        console.log("This is the item you clicked",$(this));
    });
};

//create the templates with event handling and append to the body
$.tmpl('myFolders',data, {rendered:postProcess}).appendTo($('body'));

This kind of workflow is great for really dynamic sites

查看更多
等我变得足够好
3楼-- · 2019-08-29 10:21
var obj = {... mess ... };

var myString = (function parseObj(obj, str){
    for(var key in obj){
        str += key;
        $.isPlainObject(obj[key]) && parseObj(obj[key]);
    }

    return str;
})(obj, '');

$.isPlainObject is from jQuery. If you don't want to use jQuery, it's equivalent to: {}.toString.call( obj[key] ) === '[object Object]'

查看更多
聊天终结者
4楼-- · 2019-08-29 10:28

Create a function which does that: http://jsfiddle.net/k5BTr/.

var obj = {"1":[],"css":{"admin":[],"tabs":{"skin1":{"images":[]},"skin10":{"images":[]},"skin11":{"images":[]},"skin12":{"images":[]},"skin2":[],"skin3":{"images":[]},"skin4":{"images":[]},"skin5":{"images":[]},"skin6":{"images":[]},"skin7":{"images":[]},"skin8":{"images":[]},"skin9":{"images":[]}}},"img":{"admin":[],"filemanager":[],"icons":[]},"js":{"libs":[]},"menu":{"img":[]},"walpappere":{"1":[]}};

function list(items, level) {
    for (var key in items) { // iterate
        if (items.hasOwnProperty(key)) {
            // write amount of spaces according to level
            // and write name and newline
            document.write(
                (new Array(level + 1)).join(" ") +
                key +
                "<br>"
            );

            // if object, call recursively
            if (items[key] != null && typeof items[key] === "object") {
                list(items[key], level + 1);
            }
        }
    }
}

list(obj, 0);
查看更多
登录 后发表回答