Tree view
Bootstrap 5 Tree view plugin
MDB treeview plugin is used to show hierarchical information which starts from the root item and proceed to its children and their respective children. Each item besides the root has a parent and can have children.
The parent is the node which is higher in the hierarchy and the child the one that is lower. Siblings are items which have one and the same parent. Items can be expanded and collapsed.
Note: Read the API tab to find all available options and advanced customization
Note: Currently, the plugin is only compatible with the basic MDB package imported in UMD format. More about import MDB formats.
Basic example - DOM
Add data-mdb-treeview-init
and .treeview
class to the container with your list to initialize the component
structure. If you want to nest your lists - wrap a text of a li
tag in
a
tag and define other ul
after it. Add .show
class to
the list that you want to expand from the beginning.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Basic example - JavaScript
You can define whole structure of tree using only JavaScript. There are some props in data that you can use to customize it:
name
- defines a text for an itemchildren
- defines a nested list and it's childrenicon
- defines a custom expanding icon for the nested listshow
- defines if list expands from the beginning or notdisabled
- defines if list item is disabled or not
- One
- Two
- Three
- Second-one
- Second-two
- Second-three
- Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
- Third-three
- Fourth-one
- Fourth-two
- Fourth-three
- Third-one
Open on item click
Use data-mdb-open-on-click
to specify whether to open treeview lists by clicking only on the arrow or
on the entire list item.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Accordion
Use a data-mdb-accordion
attribute to enable or disable opening only one list at
the same level.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Selectable
Use a data-mdb-selectable
attribute to set up checkboxes in every list item.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Expand
Expand your treeview to the particular list using the
expand(ID)
method.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Collapse
Collapse your treeview using the collapse()
method.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Custom icons - DOM
You can add your custom icons to your treeview. For non-nested elements - paste code of an
icon in the item. For nested elements - declare span
tag with
aria-label="toggle"
attribute inside a
element and paste there the
code for your icon.
Use a data-mdb-rotation-angle
attribute to define a rotation angle of nested
lists icons.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
- Third-one
- Third-two
- Third-three
Custom icons - JavaScript
Add icons to the list elements by pasting an icon code in the
name
property. If you want to change the rotatable icon in the nested list - use
icon
property instead.
- One
- Two
- Three
- Second-one
- Second-two
- Second-three
- Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
- Third-three
- Fourth-one
- Fourth-two
- Fourth-three
- Third-one
Color
Set the color of an active item using the
data-mdb-treeview-color
attribute.
Current color: primary
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Line
Use a data-mdb-line
attribute to set up an additional line in every nested list.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Disabled - DOM
Add .treeview-disabled
class to the a
(if nested) or
li
tag to disable your list item. You can expand disabled items, but you can't
select them.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Disabled - JavaScript
To generate a disabled list item, use disabled
property.
- One
- Two
- Three
- Second-one
- Second-two
- Second-three
- Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
- Third-three
- Fourth-one
- Fourth-two
- Fourth-three
- Third-one
Filter
Use the .filter(string)
method to expand list items that meet your requirements.
- One
- Two
-
Three
- Second-one
- Second-two
-
Second-three
-
Third-one
- Fourth-one
- Fourth-two
- Fourth-three
- Third-two
-
Third-three
- Fourth-one
- Fourth-two
- Fourth-three
-
Third-one
Tree view - API
Import
Usage
Via data attribute
Using the Tree view plugin doesn't require any additional JavaScript code - simply add
data-mdb-treeview-init
attribute to
.treeview
and use other data attributes to set all options.
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-treeview-color=""
.
Name | Type | Default | Description |
---|---|---|---|
structure
|
Array | null | null |
Defines whole structure of the treeview |
treeviewColor
|
String | 'primary' |
Defines a text and background color for an active or hovered item |
openOnClick
|
Boolean | true |
Enables opening list by clicking only on the icon |
selectable
|
Boolean | false |
Setup checkboxes for the every list item |
accordion
|
Boolean | false |
Allows only one list on the same level to be opened |
rotationAngle
|
Number | 90 |
Defines a rotation angle of the icons in nested lists |
line
|
Boolean | false |
Adds a line to every nested list |
Methods
Name | Description | Example |
---|---|---|
dispose
|
Destroys a Treeview instance |
myTreeview.dispose()
|
getInstance
|
Static method which allows you to get the tree view instance associated to a DOM element. |
Treeview.getInstance(myTreeviewEl)
|
collapse
|
Collapses every list in treeview |
myTreeview.collapse()
|
expand('level-id')
|
Expands a treeview to the list with a particular ID |
myTreeview.expand('level-sampleItems')
|
filter(string)
|
Expands list items that meet your requirements |
myTreeview.filter('sample')
|
Events
Name | Description |
---|---|
itemSelected.mdb.treeview
|
This event fires immediately when one of the treeview checkboxes are changed. Selected
items are available with items property of the event.
|
itemActive.mdb.treeview
|
This event fires immediately when one of the treeview list items are selected by
click. Selected item is available with
item property of the event.
|