Angular TreeView - Customize Node Appearance
For minor customization of nodes, you can define specific fields in node data objects. For example, the following code adds an icon to each node.
HTML
TypeScript
- <dx-tree-view
- [dataSource]="hierarchicalData" >
- </dx-tree-view>
- import { DxTreeViewModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- hierarchicalData = [{
- id: '1',
- text: 'Fruits',
- icon: '/pics/fruits.ico',
- items: [
- { id: '1_1', text: 'Apples', icon: '/pics/fruits/apple.ico' },
- { id: '1_2', text: 'Oranges', icon: '/pics/fruits/orange.ico' }
- ]
- }, {
- id: '2',
- text: 'Vegetables',
- icon: '/pics/vegetables.ico',
- items: [
- { id: '2_1', text: 'Cucumbers', icon: '/pics/vegetables/cucumber.ico' },
- { id: '2_2', text: 'Tomatoes', icon: '/pics/vegetables/tomato.ico' }
- ]
- }];
- }
- @NgModule({
- imports: [
- // ...
- DxTreeViewModule
- ],
- // ...
- })
If you need a more flexible solution, define an itemTemplate.
HTML
TypeScript
- <dx-tree-view
- [dataSource]="hierarchicalData"
- itemTemplate="itemTemplate">
- <div *dxTemplate="let itemObj of 'itemTemplate'">
- <i>{{itemObj.text}}</i>
- </div>
- </dx-tree-view>
- import { DxTreeViewModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- hierarchicalData = [{
- id: '1',
- text: 'Fruits',
- items: [
- { id: '1_1', text: 'Apples' },
- { id: '1_2', text: 'Oranges' }
- ]
- }, {
- id: '2',
- text: 'Vegetables',
- items: [
- { id: '2_1', text: 'Cucumbers' },
- { id: '2_2', text: 'Tomatoes' }
- ]
- }];
- }
- @NgModule({
- imports: [
- // ...
- DxTreeViewModule
- ],
- // ...
- })
You can also customize individual nodes. Declare them using the dxItem component.
HTML
TypeScript
- <dx-tree-view>
- <dxi-item>
- <i>Apples</i>
- </dxi-item>
- <dxi-item>
- <p>Oranges</p>
- </dxi-item>
- </dx-tree-view>
- import { DxTreeViewModule } from "devextreme-angular";
- // ...
- export class AppComponent {
- // ...
- }
- @NgModule({
- imports: [
- // ...
- DxTreeViewModule
- ],
- // ...
- })