blsn
blsn

Reputation: 1093

Dynamic HTML navigation bar from array

I outputted the following array to the equivalent JavaScript JSON variable, and I tried to create a dynamic based navigation bar.

$navArray = array(
    array('id'=>1,'parent'=>0,'text'=>'1A','href'=>'1a'),
    array('id'=>2,'parent'=>1,'text'=>'2B','href'=>'2b'),
    array('id'=>3,'parent'=>1,'text'=>'3C','href'=>'3c'),
    array('id'=>4,'parent'=>2,'text'=>'4D','href'=>'4d'),
    array('id'=>5,'parent'=>2,'text'=>'5E','href'=>'5e'),
    array('id'=>6,'parent'=>5,'text'=>'6F','href'=>'6f'),
    array('id'=>7,'parent'=>5,'text'=>'7G','href'=>'7g'),
    array('id'=>8,'parent'=>3,'text'=>'8H','href'=>'8h'),
);

The script (JavaScript/jQuery) should get the array and returns HTML based on the parent 'id' as follows:

<ul>
    <li>
        <a href="1a">1A</a>
        <ul>
            <li>
                <a href="2b">2B</a>
                <ul>
                    <li>
                        <a href="4d">4D</a>
                    </li>
                    <li>
                        <a href="/סוכרת">5E</a>
                        <ul>
                            <li>
                                <a href="6f">6F</a>
                            </li>
                            <li>
                                <a href="7g">7G</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>
                <a href="3c">3C</a>
                <ul>
                    <li>
                        <a href="8h">8H</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
<ul>

The HTML screen result should look like this:

enter image description here

I tried doing something... but that didn't work.

$('li.dropdown').hover(
    function(){
        $(this).find('ul').slideDown();
    },
    function(){
        $(this).find('ul').slideUp();
    });

How do you dynamically create a multi dimensional level HTML navigation bar as described?

Upvotes: 0

Views: 1779

Answers (1)

casraf
casraf

Reputation: 21694

Sounds like a classic case for recursion, can you change the array objects to include their own children?

$navArray = array(
    array('id'=>1,'text'=>'1A','href'=>'1a', 'children' => array(
      array('id'=>2,'text'=>'2B','href'=>'2b', 'children' => array(
        array('id'=>4,'text'=>'4D','href'=>'4d'),
        array('id'=>5,'text'=>'5E','href'=>'5e', 'children' => array(
          array('id'=>6,'text'=>'6F','href'=>'6f'),
          array('id'=>7,'text'=>'7G','href'=>'7g'),
        )),
      )),
      array('id'=>3,'text'=>'3C','href'=>'3c', 'children' => array(
        array('id'=>8,'text'=>'8H','href'=>'8h')
      )),
    )),
);

Then if you output your array into JS (let arr = <?=json_encode($navArray)?>), you can feed it into your function which will run recursively through the children, something similar to this should work:

function createMenuItems(arr) {
  let output = '<ul>'
  for (let item of arr) {
    output += `<li><a id="${item.id}" href="${item.href}">${item.text}</a></li>`
    if (item.children && item.children.length) {
      output += createMenuItems(item.children);
    }
  }
  output += '</ul>'
  return output
}

Upvotes: 1

Related Questions