Jump to content

Dynamically populate menu items


dushkin
 Share

Recommended Posts

Hello all,

 

 I am using the W3.CSS animated drop-down (https://www.w3schools.com/w3css/w3css_dropdowns.asp).

 But I cannot figure out how can I populate the menu items dynamically according to a list of item names.

I guess some Javascript should be involved here, so I try in this forum 🙂

     <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

    <div class="w3-dropdown-click">
       <button onclick="showMenu()" class="w3-button">Team1</button>
       <div id="Demo" class="w3-dropdown-content w3-bar-block w3-animate-zoom">
           <a href="#" class="w3-bar-item w3-button">Link 1</a>
           <a href="#" class="w3-bar-item w3-button">Link 2</a>
           <a href="#" class="w3-bar-item w3-button">Link 3</a>
       </div>
    </div>

Thanks!

Edited by dushkin
Link to comment
Share on other sites

Why client side, w/ javascript and some kind of database, instead of server side, w/ something like PHP and MYSQL?

I'd go w/ PHP and MYSQL to dynamically populate a menu, but that's me.

Please confirm that you've thought about client side / server side and purposely chose client side.

 

 

 

 

Edited by niche
Link to comment
Share on other sites

Hi Niche. I am working on my first ever web project.

I am using a REST API call to get the list of menu items.

The last weeks I spent studying HTML, CSS, JavaScript and JQuery.

My server was built using Spring Boot which is also new for me.

I hope I would not need to study also PHP to get a solution for that 😄

 

Link to comment
Share on other sites

I use REST API to get the values from a database into an object array (after parsing the JSON response):

async function showMenu() {
    try{
        (async () => {
            const response = await fetch('http://localhost:8088/teams') /*Get teams object from db*/
            var teamsArrObj = await response.json()
            console.log(teamsArrObj);

/* Meanwhile I return the array but actually I want to build the menu based on the returned values */

            return teamsArrObj;
        })()
    }catch{
        console.log("error"); /* Enhance error handling here! */
    }
}

Edited by dushkin
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...