you can also achieve dropdown tree
<div class="n-dropdown"><button class="n-btn n-dropdown-toggle" type="button" aria-haspopup="true" aria-expanded="false">Tree</button><div class="n-dropdown-menu"><div class="n-dropdown-content"><a class="n-dropdown-item" href="#">Item 1</a><a class="n-dropdown-item" href="#">Item 2</a><div class="n-dropdown"><a class="n-dropdown-item n-dropdown-toggle" href="#">Item 3</a><div class="n-dropdown-menu"><div class="n-dropdown-content"><a class="n-dropdown-item" href="#">Item 3 1</a><a class="n-dropdown-item" href="#">Item 3 2</a><a class="n-dropdown-item" href="#">Item 3 3</a><a class="n-dropdown-item" href="#">Item 3 4</a></div></div></div><div class="n-dropdown"><a class="n-dropdown-item n-dropdown-toggle" href="#">Item 4</a><div class="n-dropdown-menu"><div class="n-dropdown-content"><a class="n-dropdown-item" href="#">Item 4 1</a><a class="n-dropdown-item" href="#">Item 4 2</a><a class="n-dropdown-item" href="#">Item 4 3</a><a class="n-dropdown-item" href="#">Item 4 4</a></div></div></div></div></div></div>
using N.dropdown.getInstance(dropdownElement) it can initialize the .n-dropdown when it is not initialize yet and get the instance at the same time. if the .n-dropdown is already initialize, it will only get the instance.
document.addEventListener("DOMContentLoaded", (event) => {// apply the dropdown script to all .n-dropdown elementconst elements = document.querySelectorAll('.n-dropdown')elements.forEach((el) => {const instance = N.dropdown.getInstance(el)});})
Method Method | Return Return | Description Description |
---|---|---|
Instance.show | void | to show the dropdown programmatically. |
Instance.close | void | to close the dropdown programmatically. |
Instance.destroy | void | it is use to destroy and kill the dropdown instance. |
All dropdown events are fired at the .n-dropdown element.
Event Event | Description Description |
---|---|
show.n.dropdown | This event fires immediately when the show instance method is called. |
shown.n.dropdown | This event fires when the dropdown is completely shown (CSS transition is complete). |
hide.n.dropdown | This event fires immediately when the close instance method is called. |
hidden.n.dropdown | This event fires when the dropdown is completely hidden (CSS transition is complete). |
const dropdown = document.querySelector('#my-dropdown')dropdown.addEventListener('show.n.dropdown'. (event) => {// do something here...})