Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: addClass to anchor tag #55

Closed
ghost opened this issue Jan 8, 2014 · 2 comments
Closed

Question: addClass to anchor tag #55

ghost opened this issue Jan 8, 2014 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 8, 2014

Hi,
I'm trying to get this working with a bootstrap template but i cannot get one of the menu items to display correct.

I'm using a manually filled menu list for now and so far i've got the following code (placed inline in my view, so probably not the right location for this logic but yeah).

Menu::handler('main')->addClass('nav nav-list');
Menu::handler('main')->getItemsAtDepth(0)->map(function($item) { if($item->hasChildren()) { $item->addClass('dropdown-toggle'); } });
Menu::handler('main')->getItemListsAtDepth(1)->setElement('ul')->addClass('submenu');

This outputs the following:

<ul class="nav nav-list">
  <li>
    <a href="#">Home</a>
  </li>
  <li class="dropdown-toggle active-child">
    <a href="#">Child test</a>
      <ul class="submenu">
        <li>
          <a href="#">Child 1</a>
        </li>
        <li class="active">
          <a href="#">Child 2</a>
      </li>
    </ul>
  </li>
</ul>

Now the problem i'm having is that i need the 'dropdown-toggle' class to be added to the 'Child test' anchor tag and not to the 'li' element.

Can this be done without using raw?

@vespakoen
Copy link
Owner

Yes we can!, via getContent =)

Menu::handler('main')->getItemsAtDepth(0)->map(function($item)
{
    if($item->hasChildren())
    {
        // Add a class to the LI
        $item->addClass('dropdown');

        // Add a class to the A
        $item->getContent()
            ->addClass('dropdown-toggle')
            ->setAttribute('data-toggle', 'dropdown');

        // Add a class to the UL
        $item->getChildren()
            ->addClass('dropdown-menu');
    }
});

@ghost
Copy link
Author

ghost commented Jan 8, 2014

Awesome thanks! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant