|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: jQuery TreeList Documentation - Getting Started with the TreeList |
| 4 | +description: "Get started with the jQuery TreeList by Kendo UI and learn how to create, initialize, and enable the component." |
| 5 | +slug: getting_started_kendoui_treelist_component |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the TreeList |
| 10 | + |
| 11 | +This guide demonstrates how to get up and running with the Kendo UI for jQuery TreeList. |
| 12 | + |
| 13 | +After the completion of this guide, you will achieve the following end result: |
| 14 | + |
| 15 | +```dojo |
| 16 | + <div id="treelist"></div> |
| 17 | +
|
| 18 | + <script> |
| 19 | + let myDataArray = [ |
| 20 | + {ID: 1, Name: "Tom", Date: "10/15/2022", parentId: null}, |
| 21 | + {ID: 2, Name: "John", Date: "11/25/2022", parentId: 1}, |
| 22 | + {ID: 3, Name: "Annie", Date: "05/09/2022", parentId: 1}, |
| 23 | + {ID: 4, Name: "Rachel", Date: "08/06/2022", parentId: 2}, |
| 24 | + {ID: 5, Name: "Klemens", Date: "10/07/2022", parentId: 4}, |
| 25 | + {ID: 6, Name: "Micah", Date: "05/19/2022", parentId: 3}, |
| 26 | + {ID: 7, Name: "Junie", Date: "04/04/2022", parentId: 3}, |
| 27 | + {ID: 8, Name: "Krishnah", Date: "07/19/2022", parentId: 7}, |
| 28 | + {ID: 9, Name: "Enrichetta", Date: "01/11/2022", parentId: 8}, |
| 29 | + {ID: 10, Name: "Marten", Date: "02/13/2022", parentId: 9}, |
| 30 | + {ID: 11, Name: "Rosmunda", Date: "08/15/2022", parentId: 9}, |
| 31 | + {ID: 12, Name: "Regan", Date: "08/15/2022", parentId: 2}, |
| 32 | + {ID: 13, Name: "Drew", Date: "08/15/2022", parentId: 2}, |
| 33 | + {ID: 14, Name: "Bevis", Date: "08/15/2022", parentId: 4}, |
| 34 | + {ID: 15, Name: "Olga", Date: "08/15/2022", parentId: 4}, |
| 35 | + {ID: 16, Name: "Robert", Date: "08/15/2022", parentId: 6}, |
| 36 | + {ID: 17, Name: "Priscilla", Date: "08/15/2022", parentId: 6}, |
| 37 | + {ID: 18, Name: "Oren", Date: "08/15/2022", parentId: 6}, |
| 38 | + {ID: 19, Name: "Thomas", Date: "08/15/2022", parentId: 6}, |
| 39 | + {ID: 20, Name: "Keiko", Date: "08/15/2022", parentId: 5}, |
| 40 | + ]; |
| 41 | +
|
| 42 | + $("#treelist").kendoTreeList({ |
| 43 | + dataSource: { |
| 44 | + data: myDataArray, |
| 45 | + schema: { |
| 46 | + model: { |
| 47 | + id: "ID", |
| 48 | + parentId: "parentId", |
| 49 | + expanded: true, |
| 50 | + fields: { |
| 51 | + Date: { type: "date" } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }, |
| 56 | + toolbar: ["create"], |
| 57 | + // Enable filtering. |
| 58 | + filterable: true, |
| 59 | + // Enable editing. |
| 60 | + editable: "inline", |
| 61 | + height: 400, |
| 62 | + columns: [ |
| 63 | + { field: "Name" }, |
| 64 | + { field: "Date", format: "{0:dd-MM-yyyy}" }, |
| 65 | + { title: "Edit", command: [ "edit" ]} |
| 66 | + ] |
| 67 | + }); |
| 68 | + </script> |
| 69 | +``` |
| 70 | + |
| 71 | +## 1. Create an Empty div Element |
| 72 | + |
| 73 | +First, create an empty `<div>` element on the page that will serve as the main container of the TreeList component. |
| 74 | + |
| 75 | +```html |
| 76 | +<div id="treelist"></div> |
| 77 | +``` |
| 78 | + |
| 79 | +## 2. Initialize the TreeList |
| 80 | + |
| 81 | +In this step, you will initialize the Grid from the empty `<div>` element. |
| 82 | + |
| 83 | +```html |
| 84 | +<div id="treelist"></div> |
| 85 | + |
| 86 | +<script> |
| 87 | + // Target the div element by using jQuery and then call the kendoTreeList() method. |
| 88 | + $("#treelist").kendoTreeList({ |
| 89 | + // Add some basic configurations such as height. |
| 90 | + height: 400 |
| 91 | + }); |
| 92 | +</script> |
| 93 | +``` |
| 94 | + |
| 95 | +## 3. Bind the TreeList to Data |
| 96 | + |
| 97 | +Once the basic initialization is completed, you can start adding additional configurations to the TreeList. The first and most important configuration is the [`kendo.data.TreeListDataSource`](/api/javascript/data/treelistdatasource). |
| 98 | + |
| 99 | +The TreeList renders its hierarchy based on the `parentId`-`id` relationship. The data objects contain both an `id` and a `parentId` field which describe the hierarchy of the items. You can change these field names by using the [`schema.model` definition](/api/javascript/data/datasource/configuration/schema#schema.model). |
| 100 | + |
| 101 | +> The TreeList distinguishes the root items based on the `parentId`: |
| 102 | +> * If the `schema.model.fields.[parentIdField]` is nullable, root items will be the items whose `parentId` field values are `null`. |
| 103 | +> * If the `schema.model.fields.[parentIdField]` is not nullable, root items will be the items which have a default value for their data type. |
| 104 | +> |
| 105 | +> When you use the `schema.model.fields` configuration, list all fields. Set the field which represents the `id` through the `schema.model.id`. If these are not set, they will work for displaying data but will post incomplete objects to the server when editing items. |
| 106 | +
|
| 107 | +```javascript |
| 108 | + let myDataArray = [ |
| 109 | + {ID: 1, Name: "Tom", Date: "10/15/2022", parentId: null}, |
| 110 | + {ID: 2, Name: "John", Date: "11/25/2022", parentId: 1}, |
| 111 | + {ID: 3, Name: "Annie", Date: "05/09/2022", parentId: 1}, |
| 112 | + {ID: 4, Name: "Rachel", Date: "08/06/2022", parentId: 2}, |
| 113 | + {ID: 5, Name: "Klemens", Date: "10/07/2022", parentId: 4}, |
| 114 | + {ID: 6, Name: "Micah", Date: "05/19/2022", parentId: 3}, |
| 115 | + {ID: 7, Name: "Junie", Date: "04/04/2022", parentId: 3}, |
| 116 | + {ID: 8, Name: "Krishnah", Date: "07/19/2022", parentId: 7}, |
| 117 | + {ID: 9, Name: "Enrichetta", Date: "01/11/2022", parentId: 8}, |
| 118 | + {ID: 10, Name: "Marten", Date: "02/13/2022", parentId: 9}, |
| 119 | + {ID: 11, Name: "Rosmunda", Date: "08/15/2022", parentId: 9}, |
| 120 | + {ID: 12, Name: "Regan", Date: "08/15/2022", parentId: 2}, |
| 121 | + {ID: 13, Name: "Drew", Date: "08/15/2022", parentId: 2}, |
| 122 | + {ID: 14, Name: "Bevis", Date: "08/15/2022", parentId: 4}, |
| 123 | + {ID: 15, Name: "Olga", Date: "08/15/2022", parentId: 4}, |
| 124 | + {ID: 16, Name: "Robert", Date: "08/15/2022", parentId: 6}, |
| 125 | + {ID: 17, Name: "Priscilla", Date: "08/15/2022", parentId: 6}, |
| 126 | + {ID: 18, Name: "Oren", Date: "08/15/2022", parentId: 6}, |
| 127 | + {ID: 19, Name: "Thomas", Date: "08/15/2022", parentId: 6}, |
| 128 | + {ID: 20, Name: "Keiko", Date: "08/15/2022", parentId: 5}, |
| 129 | + ]; |
| 130 | + |
| 131 | + $("#treelist").kendoTreeList({ |
| 132 | + dataSource: { |
| 133 | + data: myDataArray, |
| 134 | + schema: { |
| 135 | + model: { |
| 136 | + id: "ID", |
| 137 | + parentId: "parentId", |
| 138 | + expanded: true, |
| 139 | + fields: { |
| 140 | + Date: { type: "date" } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | +``` |
| 147 | + |
| 148 | +## 4. Configure the TreeList Columns |
| 149 | + |
| 150 | +The TreeList enables you to configure each individual column and apply a set of [column properties](/api/javascript/ui/treelist/configuration/columns#related-properties). |
| 151 | + |
| 152 | +```javascript |
| 153 | + $("#treelist").kendoTreeList({ |
| 154 | + columns: [ |
| 155 | + { field: "Name" }, |
| 156 | + { field: "Date", format: "{0:dd-MM-yyyy}" }, |
| 157 | + { title: "Edit", command: [ "edit" ]} |
| 158 | + ] |
| 159 | + }); |
| 160 | +``` |
| 161 | + |
| 162 | +## 5. Add Editing and Filtering |
| 163 | + |
| 164 | +Among other functionalities, the TreeList supports editing and filtering. The [editing]({% slug editing_kendoui_treelist_widget %}) feature enables users to edit individual TreeList cells. The [filtering]({% slug filtering_kendoui_treelist_widget %}) feature allows users to filter the data inside the TreeList. |
| 165 | + |
| 166 | +```javascript |
| 167 | + $("#treelist").kendoTreeList({ |
| 168 | + toolbar: ["create"], |
| 169 | + // Enable filtering. |
| 170 | + filterable: true, |
| 171 | + // Enable editing. |
| 172 | + editable: "inline", |
| 173 | + }); |
| 174 | +``` |
| 175 | + |
| 176 | +## Next Steps |
| 177 | + |
| 178 | +* [Referencing Existing Component Instances]({% slug widget_methodsand_events_kendoui_installation %}) |
| 179 | +* [Demo Page for the Kendo UI for jQuery TreeList](https://demos.telerik.com/kendo-ui/treelist/index) |
| 180 | + |
| 181 | +## See Also |
| 182 | + |
| 183 | +* [JavaScript API Reference of the TreeList](/api/javascript/ui/treelist) |
| 184 | +* [Knowledge Base Section](/knowledge-base) |
| 185 | + |
| 186 | +<script> |
| 187 | + window.onload = function() { |
| 188 | + document.getElementsByClassName("btn-run")[0].click(); |
| 189 | + } |
| 190 | +</script> |
0 commit comments