Skip to content

Commit 3f0cb8e

Browse files
dimodiDimo Dimov
andauthored
docs: Add ListView SearchBox KB (#547)
Co-authored-by: Dimo Dimov <dimo@Dimos-MacBook-Pro.local>
1 parent 260339f commit 3f0cb8e

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: ListView SearchBox
3+
description: How to implement a ListView search box that filters by multiple data fields.
4+
type: how-to
5+
page_title: ListView Search Box
6+
slug: listview-searchbox
7+
position:
8+
tags: listview,search,searchbox,filter,filtering
9+
ticketid: 1536554
10+
res_type: kb
11+
---
12+
13+
## Description
14+
15+
How to implement a ListView seach box, similar to the [SearchBox in the Blazor Grid](https://demos.telerik.com/blazor-ui/grid/searchbox)? I would like to use a single input to search and filter in all data fields of the ListView data.
16+
17+
## Solution
18+
19+
It is possible to search and filter the ListView programmatically. The example below is a modified version of the [ListView filtering demo](https://demos.telerik.com/blazor-ui/listview/filtering).
20+
21+
* Notice the usage of a [`CompositeFilterDescriptor`](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.CompositeFilterDescriptor), instead of a [`FilterDescriptor`](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.FilterDescriptor). This is because we are searching in multiple fields with an `OR` logical operator. The [Grid Filtering documentation]({%slug components/grid/filtering%}#filter-descriptors) explains the difference between the two descriptor types.
22+
* Populate the `FilterDescriptors` collection of the `CompositeFilterDescriptor` object with one `FilterDescriptor` for each field that you want to search in.
23+
24+
>caption ListView Search Textbox
25+
26+
````CSHTML
27+
@* ListView SearchBox *@
28+
29+
@using Telerik.DataSource
30+
@using Telerik.DataSource.Extensions
31+
32+
<TelerikListView Data="@ListViewData"
33+
Pageable="true"
34+
PageSize="10"
35+
Width="600px">
36+
<HeaderTemplate>
37+
<div style="padding: 1em">
38+
<label for="searchbox">Search in both Name and Descriprtion:</label>
39+
<TelerikTextBox Value="@SearchString" ValueChanged="@LoadListViewData"></TelerikTextBox>
40+
<TelerikButton OnClick="@ClearSearch">Clear</TelerikButton>
41+
</div>
42+
</HeaderTemplate>
43+
<Template>
44+
<div class="listview-box">
45+
<strong>@context.Name:</strong>
46+
<br />
47+
@context.Description
48+
</div>
49+
</Template>
50+
</TelerikListView>
51+
52+
<style>
53+
.listview-box {
54+
display: inline-block;
55+
margin: 1em;
56+
padding: 1em;
57+
border: 1px solid #424242;
58+
background: #fafafa;
59+
}
60+
</style>
61+
62+
@code {
63+
List<Product> ListViewData;
64+
List<Product> SourceData;
65+
string SearchString = "22";
66+
67+
async Task LoadListViewData(string newSearchString)
68+
{
69+
SearchString = newSearchString;
70+
71+
if (String.IsNullOrWhiteSpace(SearchString))
72+
{
73+
ListViewData = SourceData;
74+
}
75+
else
76+
{
77+
var request = new DataSourceRequest()
78+
{
79+
Filters = new List<IFilterDescriptor>()
80+
};
81+
82+
var cfd = new CompositeFilterDescriptor();
83+
84+
cfd.LogicalOperator = FilterCompositionLogicalOperator.Or;
85+
cfd.FilterDescriptors.Add(new FilterDescriptor("Name", FilterOperator.Contains, SearchString));
86+
cfd.FilterDescriptors.Add(new FilterDescriptor("Description", FilterOperator.Contains, SearchString));
87+
88+
request.Filters.Add(cfd);
89+
90+
ListViewData = (SourceData.ToDataSourceResult(request).Data as IEnumerable<Product>).ToList();
91+
}
92+
}
93+
94+
async Task ClearSearch()
95+
{
96+
SearchString = "";
97+
await LoadListViewData(SearchString);
98+
}
99+
100+
protected override async Task OnInitializedAsync()
101+
{
102+
SourceData = new List<Product>();
103+
104+
for (int i = 1; i <= 50; i++)
105+
{
106+
107+
SourceData.Add(new Product()
108+
{
109+
ID = i,
110+
Name = "Product " + (i * 11).ToString(),
111+
Description = "Description " + (i * 22).ToString()
112+
});
113+
}
114+
115+
await LoadListViewData(SearchString);
116+
}
117+
118+
public class Product
119+
{
120+
public int ID { get; set; }
121+
public string Name { get; set; }
122+
public string Description { get; set; }
123+
}
124+
}
125+
````

0 commit comments

Comments
 (0)