Search Nodes

Searching is disabled in the TreeView widget by default. Assign true to the searchEnabled option to display the search panel. The searchExpr option specifies which data fields should be searched. Assign an array of field names to it if you need to search several fields.

jQuery
JavaScript
var treeViewData = [
    { key: '1', name: 'Fruits' },
    { key: '1_1', name: 'Apples', count: 20, parent: '1' },
    { key: '1_2', name: 'Oranges', count: 3, parent: '1' },
    { key: '2', name: 'Vegetables' },
    { key: '2_1', name: 'Cucumbers', count: 15, parent: '2' },
    { key: '2_2', name: 'Tomatoes', count: 23, parent: '2' }
];

$(function() {
    $("#treeViewContainer").dxTreeView({
        dataSource: treeViewData,
        dataStructure: 'plain',
        keyExpr: 'key',
        displayExpr: 'name',
        parentIdExpr: 'parent',
        searchEnabled: true,
        searchExpr: ["count", "name"]
    });
});
Angular
HTML
TypeScript
<dx-tree-view
    [dataSource]="treeViewData"
    dataStructure="plain"
    keyExpr="key"
    displayExpr="name"
    parentIdExpr="parent"
    [searchEnabled]="true"
    [searchExpr]="['count', 'name']">
</dx-tree-view>
import { DxTreeViewModule } from 'devextreme-angular';
// ...
export class AppComponent {
    treeViewData = [
        { key: '1', name: 'Fruits' },
        { key: '1_1', name: 'Apples', count: 20, parent: '1' },
        { key: '1_2', name: 'Oranges', count: 3, parent: '1' },
        { key: '2', name: 'Vegetables' },
        { key: '2_1', name: 'Cucumbers', count: 15, parent: '2' },
        { key: '2_2', name: 'Tomatoes', count: 23, parent: '2' }
    ];
}
@NgModule({
     imports: [
         // ...
         DxTreeViewModule
     ],
     // ...
 })

View Demo

When a user types a string in the input field, the TreeView suggests all nodes that contain this string. Assign 'startswith' to the searchMode option if you want the TreeView to suggest only those nodes that start with the input string.

jQuery
JavaScript
$(function() {
    $("#treeViewContainer").dxTreeView({
        dataSource: treeViewData,
        dataStructure: 'plain',
        keyExpr: 'key',
        displayExpr: 'name',
        parentIdExpr: 'parent',
        searchEnabled: true,
        searchMode: "startswith"
    });
});
Angular
HTML
TypeScript
<dx-tree-view
    [dataSource]="treeViewData"
    dataStructure="plain"
    keyExpr="key"
    displayExpr="name"
    parentIdExpr="parent"
    [searchEnabled]="true"
    searchMode="startswith">
</dx-tree-view>
import { DxTreeViewModule } from 'devextreme-angular';
// ...
export class AppComponent {
    treeViewData = [
        { key: '1', name: 'Fruits' },
        { key: '1_1', name: 'Apples', count: 20, parent: '1' },
        { key: '1_2', name: 'Oranges', count: 3, parent: '1' },
        { key: '2', name: 'Vegetables' },
        { key: '2_1', name: 'Cucumbers', count: 15, parent: '2' },
        { key: '2_2', name: 'Tomatoes', count: 23, parent: '2' }
    ];
}
@NgModule({
     imports: [
         // ...
         DxTreeViewModule
     ],
     // ...
 })

You can customize the search panel by specifying the searchEditorOptions option. The following code changes the panel's default width and placeholder:

jQuery
JavaScript
$(function() {
    $("#treeViewContainer").dxTreeView({
        dataSource: treeViewData,
        dataStructure: 'plain',
        keyExpr: 'key',
        displayExpr: 'name',
        parentIdExpr: 'parent',
        searchEnabled: true,
        searchEditorOptions: {
            placeholder: "Type search value here...",
            width: 300
        }
    });
});
Angular
HTML
TypeScript
<dx-tree-view
    [dataSource]="treeViewData"
    dataStructure="plain"
    keyExpr="key"
    displayExpr="name"
    parentIdExpr="parent"
    [searchEnabled]="true">
    <dxo-search-editor-options
        placeholder="Type search value here..."
        [width]="300">
    </dxo-search-editor-options>
</dx-tree-view>
import { DxTreeViewModule } from 'devextreme-angular';
// ...
export class AppComponent {
    treeViewData = [
        { key: '1', name: 'Fruits' },
        { key: '1_1', name: 'Apples', count: 20, parent: '1' },
        { key: '1_2', name: 'Oranges', count: 3, parent: '1' },
        { key: '2', name: 'Vegetables' },
        { key: '2_1', name: 'Cucumbers', count: 15, parent: '2' },
        { key: '2_2', name: 'Tomatoes', count: 23, parent: '2' }
    ];
}
@NgModule({
     imports: [
         // ...
         DxTreeViewModule
     ],
     // ...
 })
See Also