@@ -113,6 +113,146 @@ To enable the search box, add the `<TreeListSearchBox>` tag in the `<TreeListToo
113113
114114![ treelist search box] ( images/search-box-overview.gif )
115115
116+ ## Filter From Code
117+
118+ You can set the TreeList filters programmatically through the component [ state] ({%slug treelist-state%}).
119+
120+ @[ template] ( /_contentTemplates/treelist/state.md#initial-state )
121+
122+ > caption The result from the code snippet below.
123+
124+ ![ ] ( images/searchbox-filter-control.gif )
125+
126+ > caption Set programmatically Searchbox Filter.
127+
128+ ```` Razor
129+ @* This snippet shows how to set filtering state to the TreeList from your code
130+ Applies to the Searchbox filter *@
131+
132+ @using Telerik.DataSource;
133+
134+ <TelerikButton OnClick="@SetTreeListFilter" Primary="true">Set filtered state</TelerikButton>
135+
136+ <TelerikTreeList Data="@Data"
137+ ItemsField="@(nameof(Employee.DirectReports))"
138+ Height="400px"
139+ Pageable="true"
140+ Width="850px"
141+ @ref="TreeListRef">
142+ <TreeListToolBar>
143+ <TreeListSearchBox />
144+ </TreeListToolBar>
145+ <TreeListColumns>
146+ <TreeListColumn Field="Name" Expandable="true" Width="320px" />
147+ <TreeListColumn Field="Id" Width="120px" />
148+ <TreeListColumn Field="Address" Title="Email Address" Width="220px" />
149+ <TreeListColumn Field="HireDate" Width="220px" />
150+ </TreeListColumns>
151+ </TelerikTreeList>
152+
153+ @code {
154+ public TelerikTreeList<Employee> TreeListRef { get; set; } = new TelerikTreeList<Employee>();
155+
156+ async Task SetTreeListFilter()
157+ {
158+ var filteredState = new TreeListState<Employee>()
159+ {
160+ SearchFilter = CreateSearchFilter()
161+ };
162+
163+ await TreeListRef.SetState(filteredState);
164+ }
165+
166+ private IFilterDescriptor CreateSearchFilter()
167+ {
168+ var descriptor = new CompositeFilterDescriptor();
169+ var fields = new List<string>() { "Name", "Address" };
170+ var searchValue = "root: 1";
171+ descriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
172+
173+ foreach (var field in fields)
174+ {
175+ var filter = new FilterDescriptor(field, FilterOperator.Contains, searchValue);
176+
177+ filter.MemberType = typeof(string);
178+
179+ descriptor.FilterDescriptors.Add(filter);
180+ }
181+
182+ return descriptor;
183+ }
184+
185+ public List<Employee> Data { get; set; }
186+
187+
188+ public class Employee
189+ {
190+ public List<Employee> DirectReports { get; set; }
191+
192+ public int Id { get; set; }
193+ public string Name { get; set; }
194+ public string Address { get; set; }
195+ public DateTime HireDate { get; set; }
196+ }
197+
198+ public int LastId { get; set; } = 1;
199+
200+ protected override async Task OnInitializedAsync()
201+ {
202+ Data = await GetTreeListData();
203+ }
204+
205+ async Task<List<Employee>> GetTreeListData()
206+ {
207+ List<Employee> data = new List<Employee>();
208+
209+ for (int i = 1; i < 15; i++)
210+ {
211+ Employee root = new Employee
212+ {
213+ Id = LastId,
214+ Name = $"root: {i}",
215+ Address = $"{i}@example.com",
216+ HireDate = DateTime.Now.AddYears(-i),
217+ DirectReports = new List<Employee>(),
218+ };
219+ data.Add(root);
220+ LastId++;
221+
222+ for (int j = 1; j < 4; j++)
223+ {
224+ int currId = LastId;
225+ Employee firstLevelChild = new Employee
226+ {
227+ Id = currId,
228+ Name = $"first level child {j} of {i}",
229+ Address = $"{currId}@example.com",
230+ HireDate = DateTime.Now.AddDays(-currId),
231+ DirectReports = new List<Employee>(),
232+ };
233+ root.DirectReports.Add(firstLevelChild);
234+ LastId++;
235+
236+ for (int k = 1; k < 3; k++)
237+ {
238+ int nestedId = LastId;
239+ firstLevelChild.DirectReports.Add(new Employee
240+ {
241+ Id = LastId,
242+ Name = $"second level child {k} of {j} and {i}",
243+ Address = $"{nestedId}@example.com",
244+ HireDate = DateTime.Now.AddMinutes(-nestedId)
245+ }); ;
246+ LastId++;
247+ }
248+ }
249+ }
250+
251+ return await Task.FromResult(data);
252+ }
253+ }
254+ ````
255+
116256## Customize the SearchBox
117257
118258The ` TreeListSearchBox ` component offers the following settings to customize its behavior:
0 commit comments