Create nested UL lists from data object

前端 未结 5 722
南笙
南笙 2020-12-14 05:21

I\'m trying to create a nested UL from JSON. I am able to loop through and grab the data from the object, but I am having trouble building the nested UL. I figure the \'.app

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 05:34

    Most of what you make is dynamic so start with something like this

    Then in your code use the things you just made and add to them. Something like this:

    for(var i = 0; i < menu.length; i++){
        var navItem = menu[i];
        subMenuL1 = mainMenu.append('
  • ' + navItem.title +'
  • '); for(var j = 0; j < menu[i].menu.length; j++){ var navItemLevel1 = navItem.menu[j]; subMenuL2 = subMenuL1.append('
  • ' + navItemLevel1.title +'
  • '); for(var k = 0; k < menu[i].menu[j].menu.length; k++){ var navItemLevel2 = navItemLevel1.menu[k]; subMenuL2.append('
  • ' + navItemLevel2.title +'
  • '); } } };

    Note: I did not test at all... just looked at the example code and made a few changes to get you in the right direction.

提交回复
热议问题