Avatar of chaitu chaitu
chaitu chaitu
Flag for India asked on

how to write it recursively

how to construct the below HTML ;as you can see the table data can be multiply nested.

 while(rs.next())
		 {
			 
			 al.add(rs.getInt("ID"));
			 
			 Menu m = new Menu();
			 m.setId(rs.getInt("ID"));
			 m.setName(rs.getString("NAME"));
			 m.setMenuId(rs.getInt("PARENT_ID"));
			 
			 if(al.contains(rs.getInt("PARENT_ID")))
			 {
				 System.out.println("in if...................");
				 
				 ArrayList<SubMenu> subMenuList = new  ArrayList<SubMenu>();
				 SubMenu sm = new SubMenu();
				 sm.setId(rs.getInt("ID"));
				 sm.setName(rs.getString("NAME"));
				 sm.setMenuId(rs.getInt("PARENT_ID"));
				 subMenuList.add(sm);
				 m.setSubMenuList(subMenuList);
			 }
			 
			 System.out.println(m);
			 
		 }

Open in new window


    <li>
		   <a href="#" class="parent"><span>Menu 1</span></a>
				<div>
					<ul>
						<li><a href="#" ><span>Page 1</span></a></li>
						<li><a href="#" ><span>Page 2</span></a>
						
						<div>
				<ul>
					<li><a href="#" ><span>Page 4</span></a></li>
					<li><a href="#" ><span>Page 5</span></a></li>
				</ul>
					</div>
						</li>
					</ul>
				</div>
		</li>



		<li>
			<a href="#" class="parent"><span>Menu 2</span></a>
			<div>
				<ul>
					<li><a href="#" ><span>Page 3</span></a></li>
				</ul>
			</div>
		</li>


		 <li>
			 <a href="#" class="parent"><span>Help</span></a>
			 <div>
				<ul>
					<li><a href="#" ><span>About</span></a></li>
				</ul>
			</div>
		 </li>
 

Open in new window


this is the data from the table.
ID            NAME      PARENT_ID LINKS    ACTION
4	page1	1	/images/email.gif	page1
2	Menu2	(null)	(null)	                Menu2
1	Menu1	(null)	/images/home.gif	Menu1
3	Help	(null)	(null)	                Help
5	page2	1	(null)	                page2
6	page3	2	/images/photo.gif	page3
7	about	3	(null)	               about
8	page4	5	/images/photo.gif	page3
9	page5	5	(null)	               about

Open in new window

JavaJSPJava EE

Avatar of undefined
Last Comment
zzynx

8/22/2022 - Mon
zzynx

I would do it in two passes:
1. add all main menu items (parentID = NULL)
2. Add all others (order by parentID)
chaitu chaitu

ASKER
Please post the pseudo code.
ASKER CERTIFIED SOLUTION
zzynx

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
zzynx

Thanx 4 axxepting
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes