<script type="text/javascript">
var gData = [
{id:1,name:'A',parent_id:0},
{id:2,name:'B',parent_id:7},
{id:3,name:'C',parent_id:1},
{id:4,name:'D',parent_id:10},
{id:5,name:'E',parent_id:8},
{id:6,name:'F',parent_id:0},
{id:7,name:'G',parent_id:10},
{id:8,name:'H',parent_id:1},
{id:9,name:'I',parent_id:6},
{id:10,name:'J',parent_id:0},
{id:11,name:'K',parent_id:6},
{id:12,name:'L',parent_id:9},
{id:13,name:'M',parent_id:9},
];
function fetchByParentId(parent_id){
var fake = 'SELECT * FROM tb WHERE parent_id="'+parent_id+'"';
var a=[];
for(var i in gData){
if (gData[i].parent_id==parent_id){
a.push(gData[i]);
}
}
return a;
}
function renderTree(id){
var children = fetchByParentId(id);
if (children.length){
document.write('<ul>');
}
for(var i in children){
document.write('<li>');
document.write(children[i].name);
renderTree(children[i].id);
document.write('</li>');
}
if (children.length){
document.write('</ul>');
}
}
renderTree(0);
</script>