Element: <oj-tree-view>

Oracle® JavaScript Extension Toolkit (JET)
5.0.0

E90577-01

QuickNav

Attributes


Context Objects

JET Custom Elements

JET components are implemented as custom HTML elements. In addition to the component attributes documented in this page, JET components also support standard HTML global attributes like id and aria-label.

The JET data binding syntax can be used to define both component and global attributes through the use of dynamically evaluated expressions. All attributes (component and global) support attribute-level binding by prefixing the attribute name with ":" (e.g. :id="[...]"). When using attribute-level binding, all expression values are treated as strings. Additionally, component attributes support property-level binding by using the attribute name directly with no ":" prefix. When using property-level binding, the expressions should evaluate to the types documented by the corresponding attributes. Property-level binding is strongly recommended over attribute-level binding for component attributes.

A detailed description of working with custom HTML elements can be found in: JET Custom Element Usage.


PREVIEW: This is a preview API. Preview APIs are production quality, but can be changed on a major version without a deprecation path.

Version:
  • 5.0.0
Since:
  • 4.0.0
Module:
  • ojtreeview

JET TreeView

The JET TreeView allows a user to display the hierarchical relationship between the items of a tree.

Data

The JET TreeView gets its data in two different ways. The first way is from a TreeDataSource. There are two types of TreeDataSource that are available out of the box:

  • oj.JsonTreeDataSource - Use this when the underlying data is a JSON object. See the documentation for oj.JsonTreeDataSource for more details on the available options.
  • oj.CollectionTreeDataSource - Use this when oj.Collection is the model for each group of data. See the documentation for oj.CollectionTreeDataSource for more details on the available options.

The second way is using static HTML content as data.

Example of static content


<oj-tree-view id="treeview1">
  <ul>
    <li>
      <a id="group1" href="#">Group 1</a>
      <ul>
        <li><a id="item1-1" href="#">Item 1-1</a></li>
        <li><a id="item1-2" href="#">Item 1-2</a></li>
      </ul>
    </li>
    <li>
      <a id="group2" href="#">Group 2</a>
      <ul>
        <li><a id="item2-1" href="#">Item 2-1</a></li>
        <li><a id="item2-2" href="#">Item 2-2</a></li>
      </ul>
    </li>
  </ul>
</oj-tree-view>

Touch End User Information

Target Gesture Action
Item Tap Focus on the item. If selectionMode is enabled, selects the item as well.
Press & Hold Display context menu
Disclosure Icon Tap Expand or collapse the item.

Keyboard End User Information

Target Key Action
Tab Navigates to next focusable element on page.
Shift+Tab Navigates to previous focusable element on page.
DownArrow Moves focus to the item below.
UpArrow Moves focus to the item above.
LeftArrow On an expanded item, collapses the item. Otherwise, move focus to the item above. The action is swapped with RightArrow in RTL locales.
RightArrow On a collapsed item, expands the item. Otherwise, move focus to the item above. The action is swapped with LeftArrow in RTL locales.
Shift+DownArrow Extends the selection to the item below. Only applicable if the multiple selection is enabled.
Shift+UpArrow Extends the selection to the item above. Only applicable if the multiple selection is enabled.
Space Toggles the selection of the current item and deselects the other items.
Enter Selects the current item and deselects the other items. No op if the current item is already selected.
Ctrl+Space/Enter Toggles the selection of the current item while maintaining previously selected items. Only applicable if the multiple selection is enabled.
Shift+Space/Enter Selects contiguous items from the last selected item to the current item. Only applicable if the multiple selection is enabled.

Item Context

For item attributes, developers can specify a function as the return value. The function takes a single argument, which is an object that contains contextual information about the particular item. This gives developers the flexibility to return different value depending on the context.

The context parameter contains the following keys:

Key Description
componentElement The TreeView element.
data The data object for the item (not available for static content).
datasource A reference to the data source object (not available for static content).
depth The depth of the item. The depth of the first level children under the invisible root is 1.
index The index of the item relative to its parent, where 0 is the index of the first item.
key The key of the item.
leaf Whether the item is a leaf item.
parentElement The TreeView item element. The renderer can use this to directly append content.
parentKey The key of the parent item. The parent key is null for root item.

Accessibility

Application must ensure that the context menu is available and setup with the appropriate clipboard menu items so that keyboard-only users are able to reorder items just by using the keyboard.

Performance

Data Set Size

As a rule of thumb, it is recommended that applications limit the amount of data to display. Displaying large number of items in TreeView makes it hard for user to find what they are looking for, but affects the load time and scrolling performance as well.

Item Content

TreeView allows developers to specify arbitrary content inside its item. In order to minimize any negative effect on performance, please avoid putting a large number of heavy-weight components inside because as it adds more complexity to the structure, and the effect will be multiplied because there can be many items in the TreeView.

Expand All

While TreeView provides a convenient way to initially expand all parent items in the TreeView, it might have an impact on the initial rendering performance since expanding each parent item might cause a fetch from the server depending on the TreeDataSource. Other factors that could impact performance includes the depth of the tree, and the number of children in each level.

Animation

Applications can customize animations triggered by actions in TreeView by either listening for animateStart/animateEnd events or overriding action specific style classes on the animated item. See the documentation of oj.AnimationUtils class for details.

The following are actions and their corresponding sass variables in which applications can use to customize animation effects.

Action Sass Variable Description
expand $treeViewExpandAnimation When user expands an item.
collapse $treeViewCollapseAnimation When user collapses an item.

Note: Application logic should not interact with the component's properties or invoke its methods until the BusyContext indicates that the component is ready for interaction.

Slots

JET elements can have up to two types of child content:

  • Any child element with a slot attribute will be moved into that named slot, e.g. <span slot='startIcon'>...</span>. All supported named slots are documented below. Child elements with unsupported named slots will be removed from the DOM.
  • Any child element lacking a slot attribute will be moved to the default slot, also known as a regular child.

contextMenu

The contextMenu slot is set on the oj-menu within this element. This is used to designate the JET Menu that this component should launch as a context menu on right-click, Shift-F10, Press & Hold, or component-specific gesture. If specified, the browser's native context menu will be replaced by the JET Menu specified in this slot.

The application can register a listener for the Menu's ojBeforeOpen event. The listener can cancel the launch via event.preventDefault(), or it can customize the menu contents by editing the menu DOM directly, and then calling refresh() on the Menu.

To help determine whether it's appropriate to cancel the launch or customize the menu, the ojBeforeOpen listener can use component API's to determine which table cell, chart item, etc., is the target of the context menu. See the JSDoc and demos of the individual components for details.

Keep in mind that any such logic must work whether the context menu was launched via right-click, Shift-F10, Press & Hold, or component-specific touch gesture.

Example

Initialize the component with a context menu:

<oj-some-element>
    <-- use the contextMenu slot to designate this as the context menu for this component -->
    <oj-menu slot="contextMenu" style="display:none" aria-label="Some element's context menu">
...
    </oj-menu>
</oj-some-element>

Attributes

(readonly) current-item :*

The key of the item that has the browser focus. This is a read-only attribute so page authors cannot set or change it directly.
Supports writeback:
  • true
Names
Item Name
Property currentItem
Property change event currentItemChanged
Property change listener attribute (must be of type function) on-current-item-changed
Example

Get the current item:

myTreeView.currentItem;

data :oj.TreeDataSource

The data source for the TreeView. Accepts an instance of oj.TreeDataSource. See the data source section in the introduction for out of the box data source types. If the data attribute is not specified, the child elements are used as content. If there's no content specified, then an empty list is rendered.
Default Value:
  • null
Names
Item Name
Property data
Property change event dataChanged
Property change listener attribute (must be of type function) on-data-changed
Example

Initialize the TreeView with an oj.Collection:

myTreeView.data = new oj.CollectionTableDataSource(collection);

dnd :Object

Enable drag and drop functionality.

JET provides support for HTML5 Drag and Drop events. Please refer to third party documentation on HTML5 Drag and Drop to learn how to use it.
Default Value:
  • {"drag": null, "drop": null}
Names
Item Name
Property dnd
Property change event dndChanged
Property change listener attribute (must be of type function) on-dnd-changed

dnd.drag :Object

Properties:
Name Type Description
items Object If this object is specified, TreeView will initiate drag operation when the user drags on an item.
Properties
Name Type Argument Description
dataTypes string | Array.<string> <optional>
(optional) The MIME types to use for the dragged data in the dataTransfer object. This can be a string if there is only one type, or an array of strings if multiple types are needed.

For example, if selected items of employee data are being dragged, dataTypes could be "application/employees+json". Drop targets can examine the data types and decide whether to accept the data. A text input may only accept "text" data type, while a chart for displaying employee data may be configured to accept the "application/employees+json" type.

For each type in the array, dataTransfer.setData will be called with the specified type and the JSON version of the selected item data as the value. The selected item data is an array of objects, with each object representing a model object from the underlying data source. For example, if the underlying data is an oj.Collection, then this would be a oj.Model object. Note that when static HTML is used, then the value would be the HTML string of the selected item.

This property is required unless the application calls setData itself in a dragStart callback function.
dragStart function(Event, {items: Array.<D>}):void <optional>
(optional) A callback function that receives the "dragstart" event and context information as arguments:

function(event, context)

All of the event payloads listed below can be found under the context argument.
  • items: An array of objects, with each object representing the data of one selected item.

This function can set its own data and drag image as needed. If dataTypes is specified, event.dataTransfer is already populated with the default data when this function is invoked. If dataTypes is not specified, this function must call event.dataTransfer.setData to set the data or else the drag operation will be cancelled. In either case, the drag image is set to an image of the dragged items on the TreeView.
drag function(Event):void <optional>
(optional) A callback function that receives the "drag" event as argument:

function(event)

dragEnd function(Event):void <optional>
(optional) A callback function that receives the "dragend" event as argument:

function(event)

Default Value:
  • null
Names
Item Name
Property dnd.drag
Example

Initialize the TreeView such that only leaf items are focusable:

myTreeView.setProperty('dnd.drag.items', {
  'dataTypes': ['application/ojtreeviewitems+json'],
  'dragEnd': handleDragEnd
});

dnd.drop :Object

Properties:
Name Type Description
items Object An object that specifies callback functions to handle dropping items

Properties
Name Type Argument Description
dataTypes string | Array.<string> <optional>
A data type or an array of data types this component can accept.

This property is required unless dragEnter, dragOver, and drop callback functions are specified to handle the corresponding events.
dragEnter function(Event, {item: Element}):void <optional>
(optional) A callback function that receives the "dragenter" event and context information as arguments:

function(event, context)

All of the event payloads listed below can be found under the context argument.
  • item: The item being entered.

This function should call event.preventDefault() to indicate the dragged data can be accepted. Otherwise, dataTypes will be matched against the drag dataTypes to determine if the data is acceptable. If there is a match, event.preventDefault() will be called to indicate that the data can be accepted.
dragOver function(Event, {item: Element}):void <optional>
(optional) A callback function that receives the "dragover" event and context information as arguments:

function(event, context)

All of the event payloads listed below can be found under the context argument.
  • item: The item being dragged over.

This function should call event.preventDefault() to indicate the dragged data can be accepted. Otherwise, dataTypes will be matched against the drag dataTypes to determine if the data is acceptable. If there is a match, event.preventDefault() will be called to indicate that the data can be accepted.
dragLeave function(Event, {item: Element}):void <optional>
(optional) A callback function that receives the "dragleave" event and context information as arguments:

function(event, context)

All of the event payloads listed below can be found under the context argument.
  • item: The item that was last entered.

drop function(Event, oj.ojTreeView.ItemsDropOnDropContext):void (required) A callback function that receives the "drop" event and context information as arguments:

function(event, context)

All of the event payloads listed below can be found under the context argument.
  • item: The item being dropped on.
  • position: The drop position relative to the item being dropped on. Valid values are "inside", "before", "after", and "first" (the first child of the item being dropped on).

This function should call event.preventDefault() to indicate the dragged data can be accepted.

If the application needs to look at the data for the item being dropped on, it can use the getContextByNode method.
Default Value:
  • null
Names
Item Name
Property dnd.drop
Example

Initialize the TreeView such that only leaf items are focusable:

myTreeView.setProperty('dnd.drop.items', {
  'dataTypes': ['application/ojtreeviewitems+json'],
  'drop': handleDrop
});

expanded :KeySet

Specifies the key set containing the keys of the TreeView items that should be expanded. Use the ExpandedKeySet class to specify items to expand. Use the ExpandAllKeySet class to expand all items.
Default Value:
  • new ExpandedKeySet()
Supports writeback:
  • true
Names
Item Name
Property expanded
Property change event expandedChanged
Property change listener attribute (must be of type function) on-expanded-changed
Example

Initialize the TreeView with some expanded items:

myTreeView.expanded = new keySet.ExpandedKeySet(['item1', 'item2']);

item

The item attribute contains a subset of attributes for items.
Names
Item Name
Property item
Property change event itemChanged
Property change listener attribute (must be of type function) on-item-changed

(nullable) item.focusable :((itemContext: oj.ojTreeView.ItemContext<K,D>) => boolean)

A function that returns whether the item is focusable. A item that is not focusable cannot be clicked on or navigated to. See itemContext in the introduction to see the object passed into the focusable function. If no function is specified, then all the items will be focusable.
Default Value:
  • null
Names
Item Name
Property item.focusable
Example

Initialize the TreeView such that only leaf items are focusable:

myTreeView.setProperty('item.focusable', function(itemContext)
{
  return itemContext['leaf'];
});

(nullable) item.renderer :((itemContext: oj.ojTreeView.ItemContext<K,D>) => {insert: Element|string}|void)|null

The renderer function that renders the contents of the item. See itemContext in the introduction to see the object passed into the renderer function. The function should return one of the following:
  • An Object with the following property:
    • insert: HTMLElement | string - A string or a DOM element of the content inside the item.
  • Nothing: If the developer chooses to manipulate the item element directly, the function should return nothing.
Default Value:
  • null
Names
Item Name
Property item.renderer
Example

Initialize the TreeView with a renderer:

myTreeView.setProperty('item.renderer', function(itemContext)
{
  return itemContext['data'].get('FIRST_NAME');
});

(nullable) item.selectable :((itemContext: oj.ojTreeView.ItemContext<K,D>) => boolean)

A function that returns whether the item can be selected. If selectionMode is set to "none" this attribute is ignored. See itemContext in the introduction to see the object passed into the selectable function. If no function is specified, then all the items will be selectable.
Default Value:
  • null
Names
Item Name
Property item.selectable
Example

Initialize the TreeView such that only leaf items are selectable:

myTreeView.setProperty('item.selectable', function(itemContext)
{
  return itemContext['leaf'];
});

selection :Array.<*>

The current selections in the TreeView. An empty array indicates nothing is selected.
Default Value:
  • []
Supports writeback:
  • true
Names
Item Name
Property selection
Property change event selectionChanged
Property change listener attribute (must be of type function) on-selection-changed
Example

Initialize the TreeView with specific selection:

myTreeView.selection = ['item1', 'item2'];

selection-mode :string

Specifies whether selection can be made and the cardinality of selection in the TreeView.
Supported Values:
Name Type Description
"multiple" string Multiple items can be selected at the same time.
"none" string Selection is disabled.
"single" string Only one item can be selected at a time.
Default Value:
  • "none"
Names
Item Name
Property selectionMode
Property change event selectionModeChanged
Property change listener attribute (must be of type function) on-selection-mode-changed
Example

Initialize the tree view to enable multiple selection:

myTreeView.selectionMode = 'multiple';

translations :Object|null

A collection of translated resources from the translation bundle, or null if this component has no resources. Resources may be accessed and overridden individually or collectively, as seen in the examples.

If the component does not contain any translatable resource, the default value of this attribute will be null. If not, an object containing all resources relevant to the component.

If this component has translations, their documentation immediately follows this doc entry.

Names
Item Name
Property translations
Property change event translationsChanged
Property change listener attribute (must be of type function) on-translations-changed
Examples

Initialize the component, overriding some translated resources and leaving the others intact:

<!-- Using dot notation -->
<oj-some-element translations.some-key='some value' translations.some-other-key='some other value'></oj-some-element>

<!-- Using JSON notation -->
<oj-some-element translations='{"someKey":"some value", "someOtherKey":"some other value"}'></oj-some-element>

Get or set the translations property after initialization:

// Get one
var value = myComponent.translations.someKey;

// Set one, leaving the others intact. Always use the setProperty API for 
// subproperties rather than setting a subproperty directly.
myComponent.setProperty('translations.someKey', 'some value');

// Get all
var values = myComponent.translations;

// Set all.  Must list every resource key, as those not listed are lost.
myComponent.translations = {
    someKey: 'some value',
    someOtherKey: 'some other value'
};

Context Objects

Each context object contains, at minimum, a subId property, whose value is a string that identifies a particular DOM node in this element. It can have additional properties to further specify the desired node. See getContextByNode for more details.

Properties:
Name Type Description
subId string Sub-id string to identify a particular dom node.

Following are the valid subIds:

oj-treeview-item

Context for TreeView items.

Properties:
Name Type Description
componentElement Element The TreeView element.
data Object The data object for the item (not available for static content).
datasource oj.TreeDataSource A reference to the data source object (not available for static content).
depth number The depth of the item. The depth of the first level children under the invisible root is 1.
index number The index of the item relative to its parent, where 0 is the index of the first item.
key Object the key of the item.
leaf boolean Whether the item is a leaf item.
parentKey Object The key of the parent item. The parent key is null for root item.

Events

ojAnimateEnd

Triggered when the default animation of a particular action has ended. Note this event will not be triggered if application cancelled the default animation on animateStart.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
action 'expand' | 'collapse' The action that started the animation. See animation section for a list of actions.
element Element The target of animation.
Examples

Specify an ojAnimateEnd listener via the DOM attribute:

<oj-tree-view on-oj-animate-end='[[listener]]'></oj-tree-view>

Specify an ojAnimateEnd listener via the JavaScript property:

myTreeView.onOjAnimateEnd = listener;

Add an ojAnimateEnd listener via the addEventListener API:

myTreeView.addEventListener('ojAnimateEnd', listener);

ojAnimateStart

Triggered when the default animation of a particular action is about to start. The default animation can be cancelled by calling event.preventDefault().
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
action 'expand' | 'collapse' The action that starts the animation. See animation section for a list of actions.
element Element The target of animation.
endCallback function():void If the event listener calls event.preventDefault() to cancel the default animation, it must call the endCallback function when it finishes its own animation handling and when any custom animation ends.
Examples

Specify an ojAnimateStart listener via the DOM attribute:

<oj-tree-view on-oj-animate-start='[[listener]]'></oj-tree-view>

Specify an ojAnimateStart listener via the JavaScript property:

myTreeView.onOjAnimateStart = listener;

Add an ojAnimateStart listener via the addEventListener API:

myTreeView.addEventListener('ojAnimateStart', listener);

ojBeforeCollapse

Triggered before an item is collapsed via the expanded attribute or via the UI. Call event.preventDefault() to veto the event, which prevents collapsing the item.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
key * The key of the item to be collapsed.
item Element The item to be collapsed.
Examples

Specify an ojBeforeCollapse listener via the DOM attribute:

<oj-tree-view on-oj-before-collapse='[[listener]]'></oj-tree-view>

Specify an ojBeforeCollapse listener via the JavaScript property:

myTreeView.onOjBeforeCollapse = listener;

Add an ojBeforeCollapse listener via the addEventListener API:

myTreeView.addEventListener('ojBeforeCollapse', listener);

ojBeforeCurrentItem

Triggered before the current item is changed via the currentItem attribute or via the UI. Call event.preventDefault() to veto the event, which prevents changing the current item.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
previousKey * The key of the previous item.
previousItem Element The previous item.
key * The key of the new current item.
item Element The new current item.
Examples

Specify an ojBeforeCurrentItem listener via the DOM attribute:

<oj-tree-view on-oj-before-current-item='[[listener]]'></oj-tree-view>

Specify an ojBeforeCurrentItem listener via the JavaScript property:

myTreeView.onOjBeforeCurrentItem = listener;

Add an ojBeforeCurrentItem listener via the addEventListener API:

myTreeView.addEventListener('ojBeforeCurrentItem', listener);

ojBeforeExpand

Triggered before an item is expanded via the expanded attribute or via the UI. Call event.preventDefault() to veto the event, which prevents expanding the item.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
key * The key of the item to be expanded.
item Element The item to be expanded.
Examples

Specify an ojBeforeExpand listener via the DOM attribute:

<oj-tree-view on-oj-before-expand='[[listener]]'></oj-tree-view>

Specify an ojBeforeExpand listener via the JavaScript property:

myTreeView.onOjBeforeExpand = listener;

Add an ojBeforeExpand listener via the addEventListener API:

myTreeView.addEventListener('ojBeforeExpand', listener);

ojCollapse

Triggered after an item has been collapsed.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
key * The key of the item that was just collapsed.
item Element The item that was just collapsed.
Examples

Specify an ojCollapse listener via the DOM attribute:

<oj-tree-view on-oj-collapse='[[listener]]'></oj-tree-view>

Specify an ojCollapse listener via the JavaScript property:

myTreeView.onOjCollapse = listener;

Add an ojCollapse listener via the addEventListener API:

myTreeView.addEventListener('ojCollapse', listener);

ojExpand

Triggered after an item has been expanded.
Properties:

All of the event payloads listed below can be found under event.detail.

Name Type Description
key * The key of the item that was just expanded.
item Element The item that was just expanded.
Examples

Specify an ojExpand listener via the DOM attribute:

<oj-tree-view on-oj-expand='[[listener]]'></oj-tree-view>

Specify an ojExpand listener via the JavaScript property:

myTreeView.onOjExpand = listener;

Add an ojExpand listener via the addEventListener API:

myTreeView.addEventListener('ojExpand', listener);

Methods

getContextByNode(node) → {Object|null}

Returns an object with context for the given child DOM node. This will always contain the subid for the node, defined as the 'subId' property on the context object. Additional component specific information may also be included. For more details on returned objects, see context objects.
Parameters:
Name Type Argument Description
node Element <not nullable>
The child DOM node
Returns:
The context for the DOM node, or null when none is found.
Type
Object | null
Example
// Returns {'subId': 'oj-some-sub-id', 'componentSpecificProperty': someValue, ...}
var context = myComponent.getContextByNode(nodeInsideElement);

getProperty(property) → {*}

Retrieves a value for a property or a single subproperty for complex properties.
Parameters:
Name Type Description
property string The property name to get. Supports dot notation for subproperty access.
Since:
  • 4.0.0
Returns:
Type
*
Example

Get a single subproperty of a complex property:

var subpropValue = myComponent.getProperty('complexProperty.subProperty1.subProperty2');

refresh() → {void}

Refreshes the component.
Returns:
Type
void

setProperties(properties) → {void}

Performs a batch set of properties.
Parameters:
Name Type Description
properties Object An object containing the property and value pairs to set.
Since:
  • 4.0.0
Returns:
Type
void
Example

Set a batch of properties:

myComponent.setProperties({"prop1": "value1", "prop2.subprop": "value2", "prop3": "value3"});

setProperty(property, value) → {void}

Sets a property or a single subproperty for complex properties and notifies the component of the change, triggering a [property]Changed event.
Parameters:
Name Type Description
property string The property name to set. Supports dot notation for subproperty access.
value * The new value to set the property to.
Since:
  • 4.0.0
Returns:
Type
void
Example

Set a single subproperty of a complex property:

myComponent.setProperty('complexProperty.subProperty1.subProperty2', "someValue");

Type Definitions

ItemContext

Properties:
Name Type Argument Description
componentElement Element The TreeView element.
data D <optional>
The data object of the item (not available for static content).
depth number The depth of the item. The depth of the first level children under the invisible root is 1.
index number The index of the item relative to its parent, where 0 is the index of the first item.
key K The key of the item.
leaf boolean Whether the item is a leaf item.
parentElement Element The TreeView item element. The renderer can use this to directly append content.
parentKey K <optional>
The key of the parent item (not available for root item).

ItemsDropOnDropContext

Properties:
Name Type Description
item Element The item being dropped on.
position 'inside' | 'before' | 'after' | 'first' The drop position relative to the item being dropped on.