# Angular TreeList Toolbar Template

> Starting with version [`17.0.0`](https://www.telerik.com/kendo-angular-ui/components/changelogs/kendo-angular-ui#v17.0.0), the [ToolBar](https://www.telerik.com/kendo-angular-ui/components/treelist/toolbar/treelist-toolbar) component was introduced in the Kendo UI for Angular TreeList and is now the recommended approach for configuring the TreeList's toolbar. You can use the toolbar template as an alternative to the ToolBar component to achieve highly customized solutions.

The [`ToolbarTemplateDirective`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/toolbartemplatedirective) enables you to customize the [Angular TreeList](https://www.telerik.com/kendo-angular-ui/components/treelist) toolbar by adding components like buttons, inputs, dropdowns, and others. To further enhance the functionality of the TreeList's toolbar and personalize the user experience, you can also add HTML elements, flexible positioning, and spacing.

To achieve this toolbar customization, nest an `<ng-template>` inside the `<kendo-treelist>` tag and apply the `kendoTreeListToolbarTemplate` directive. Inside the template, add the desired elements and configure them as needed.

```html
<kendo-treelist>
	<ng-template kendoTreeListToolbarTemplate position="bottom">
		<button kendoTreeListAddCommand>Add new</button>
		<kendo-treelist-spacer></kendo-treelist-spacer>
		<button kendoTreeListExcelCommand>Export to Excel</button>
	</ng-template>
</kendo-treelist>
```

> caution The `ToolbarTemplateDirective` does not support the built-in overflow modes available in the ToolBar component. To utilize overflow functionality, use the [ToolBar component](https://www.telerik.com/kendo-angular-ui/components/treelist/toolbar/treelist-toolbar) in the TreeList instead of the template approach.

The following example demonstrates how to define the toolbar template.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/toolbar/toolbar-template/basic/source.txt)

## Setting the Toolbar Position

You can display the toolbar above the TreeList, below the TreeList, or both. To configure this behavior, define its [`position`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/toolbartemplatedirective#position) option, which accepts the following values:

* `top`—Locates the toolbar above the group panel or header.
* `bottom`—Locates the toolbar under the pager.
* `both`—Displays one toolbar above the group panel or header and another under the pager.

The following example demonstrates setting the toolbar `position` to `'top'`, `'bottom'`, or `'both'` to control where it renders relative to the TreeList.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/toolbar/toolbar-template/position/source.txt)

## Defining the Spacing between Toolbar Elements

The [`TreeListSpacer`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelistspacercomponent) enables you to define spacing between the toolbar inner elements and arrange them in accordance with your preferences. By default, each `kendo-treelist-spacer` element occupies all available space. To override this behavior and specify a custom size, you utilize its [`width`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelistspacercomponent#width) property.

The following example demonstrates how to position the export button in the center of the available toolbar space.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/toolbar/toolbar-template/spacer/source.txt)

## Focusing Custom Toolbar Elements

When the TreeList is [`navigable`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelistcomponent#navigable) and the `kendoTreeListToolbarTemplate` contains focusable elements, use the [`TreeListToolbarFocusableDirective`](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelisttoolbarfocusabledirective) to ensure proper navigation and prevent the click event from propagating. You can apply the directive to any focusable component, including:

* [`ComboBox`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/combobox)
* [`DropDownList`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist)
* [`MultiSelect`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/multiselect)
* [`AutoComplete`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/autocomplete)
* [`DatePicker`](https://www.telerik.com/kendo-angular-ui/components/dateinputs/datepicker)
* [`NumericTextBox`](https://www.telerik.com/kendo-angular-ui/components/inputs/numerictextbox)
* [`TextBox`](https://www.telerik.com/kendo-angular-ui/components/inputs/textbox)
* Native HTML elements such as `<button>` or `<input>`

Apply the `kendoTreeListToolbarFocusable` directive to the component or custom element that you want to include in keyboard navigation. To prevent the click event from propagating, also add `(click)="$event.stopPropagation()"` to the element.

```html
<ng-template kendoTreeListToolbarTemplate>
	<kendo-combobox [data]="listItems" kendoTreeListToolbarFocusable (click)="$event.stopPropagation()"></kendo-combobox>
	<kendo-dropdownlist [data]="listItems" kendoTreeListToolbarFocusable (click)="$event.stopPropagation()"></kendo-dropdownlist>
	<kendo-textbox kendoTreeListToolbarFocusable (click)="$event.stopPropagation()"></kendo-textbox>
</ng-template>
```

The following example demonstrates using the `kendoTreeListToolbarFocusable` directive to manage focus and keyboard navigation for a DropDownList and TextBox inside the TreeList toolbar.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/toolbar/toolbar-template/focusable/source.txt)

### Handling Components with Internal Navigation

To ensure proper navigation for focusable components with internal navigation, such as TextBox, wrap the component in a custom container element and apply the `kendoTreeListToolbarFocusable` directive.

To navigate within the TextBox, focus the component. To keep navigating between the TreeList toolbar elements, focus the TextBox container instead.

```html
<ng-template kendoTreeListToolbarTemplate>
	...
	<div #wrapper kendoTreeListToolbarFocusable (keydown.enter)="textbox.focus()">
		<kendo-textbox #textbox
		(click)="$event.stopImmediatePropagation()"
		(keydown)="onKeydown($event, wrapper)"
		></kendo-textbox>
	</div>
</ng-template>
```

```typescript
public onKeydown(ev: KeyboardEvent, wrapper: HTMLDivElement): void {
	if (ev.key === 'Escape') {
		wrapper.focus();
	}
	if (ev.key === 'ArrowLeft' || ev.key === 'ArrowRight') {
		ev.stopImmediatePropagation();
	}
}
```

The following example demonstrates combining `kendoTreeListToolbarFocusable` with keyboard event handlers to manage focus and Tab navigation for a TextBox inside the TreeList toolbar.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/toolbar/toolbar-template/focusable-textbox/source.txt)

## Suggested Links

* [API Reference of the ToolbarTemplateDirective](https://www.telerik.com/kendo-angular-ui/components/treelist/api/toolbartemplatedirective)
* [API Reference of the TreeListComponent](https://www.telerik.com/kendo-angular-ui/components/treelist/api/treelistcomponent)
* [API Index of the TreeList](https://www.telerik.com/kendo-angular-ui/components/treelist/api)
