javascript - How to not repeat folders with JStree? -
i have json file data this:
datatree.json = [ { "datap": "816816816816816818", "name": "image1.jpg", "url": "/files/folder1/test/d2e9c54ceedc9/image1.jpg", "path": "/files/folder1/test/image1.jpg", "size": 35969 }, { "datap": "857022de4fccdcb54623ff6185daae706a47140c", "name": "image2.jpg", "url": "/files/folder1/pruebas/85623ff6185d7140c/image2.jpg", "path": "/files/folder1/pruebas/image2.jpg", "size": 17689282 }, { "datap": "b260ec3250420c953a9db41897c34e3551620ec325035516256b2/image3.jpg", "path": "/files/folder1/test/image3.jpg", "size": 710632 } ]
in part make operation , format jstree
$.getjson('/datatree.json', function (response) { var fulltree = []; if (response == []){ var refulltree = [] } else { var refulltree = [{"id":null,"text":"root","icon":"tree.png","state":null,"children":[]}] } function treeelements(element, tree){ var parts = element.path.split("/"); parts.splice(0,2); (var k in parts) { var count = 0 var part = parts[k]; if (part == "") part = "#"; var item = {"id":null, "text": part, "icon": icon(part), "children": []}; tree.push(item); tree = item.children; } function icon(search){ if (search.indexof(".png",".jpg") > -1){ return "glyphicon glyphicon-picture" } else if(search.indexof("jpg",".jpg") > -1){ return "glyphicon glyphicon-picture" } } } (var in response) { treeelements(response[i], fulltree); } refulltree[0]["children"] = fulltree });
the result output in format:
[ { "id": null, "text": "root", "icon": "tree.png", "state": null, "children": [ { "id": null, "text": "folder1", "children": [ { "id": null, "text": "test", "children": [ { "id": null, "text": "image1.jpg", "icon": "glyphicon glyphicon-picture", "children": [] } ] } ] }, { "id": null, "text": "folder1", "children": [ { "id": null, "text": "pruebas", "children": [ { "id": null, "text": "image2.jpg", "icon": "glyphicon glyphicon-picture", "children": [] } ] } ] }, { "id": null, "text": "folder1", "children": [ { "id": null, "text": "test", "children": [ { "id": null, "text": "image3.jpg", "icon": "glyphicon glyphicon-picture", "children": [] } ] } ] } ] } ]
this tree produced jstree, , 3 folders have same name. not want create 3 folders same name, want when find folder exists, data entered existing folder.
instead, want this:
here demo of how parse structure: http://jsfiddle.net/dgaf4/506/
here actual parsing code (also visible in fiddle);
var tmp = {}, i, j, k, l, p1, p2, fin = []; for(i = 0, j = a.length; < j; i++) { p1 = a[i].path.replace(/^\//,'').split('/'); p2 = ''; for(k = 0, l = p1.length; k < l; k++) { tmp[p2 + '/' + p1[k]] = { id : p2 + '/' + p1[k], parent : p2 ? p2 : '#', text : p1[k] }; p2 += '/' + p1[k]; } for(k in a[i]) { if(a[i].hasownproperty(k)) { tmp[a[i].path][k] = a[i][k]; } } if(a[i].path.match(/(jpg|png|jpeg)$/)) { tmp[a[i].path].icon = "glyphicon glyphicon-picture"; } } for(i in tmp) { if(tmp.hasownproperty(i)) { fin.push(tmp[i]); } } // fin contains structure in jstree compatible format
Comments
Post a Comment