|
| 1 | +--- |
| 2 | +title: Implementing Search Functionality in RadListBox with Checkboxes |
| 3 | +description: Learn how to add a search or filter feature to a RadListBox with checkboxes while preserving the checkbox states during filtering. |
| 4 | +type: how-to |
| 5 | +page_title: Add Search to ListBoxes with Checkboxes |
| 6 | +slug: listbox-add-search-to-listboxes-with-checkboxes |
| 7 | +tags: radlistbox, asp.net ajax, search, filter, checkboxes, preserve state |
| 8 | +res_type: kb |
| 9 | +ticketid: 1677570 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadListBox for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +</tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +I'm trying to implement a search box on a RadListBox that has checkboxes. However, when I apply the filter, the checkbox functionality is lost. Is there a way to keep the checkbox functionality while filtering? This knowledge base article also answers the following questions: |
| 26 | + |
| 27 | +- How to filter items in RadListBox without losing checkbox states? |
| 28 | +- How to implement a client-side search feature in RadListBox? |
| 29 | +- How to preserve checkbox states in RadListBox during filtering? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To achieve a search or filter functionality in a RadListBox with checkboxes without losing the checkbox states, follow these steps: |
| 34 | + |
| 35 | +1. **Implement Client-Side Filtering**: Use JavaScript to filter the items in the RadListBox based on the user's input in a search box. |
| 36 | + |
| 37 | +2. **Preserve Checkbox State**: Ensure that checkbox states are preserved during filtering by saving and restoring the checked states. |
| 38 | + |
| 39 | +Here is a sample implementation: |
| 40 | + |
| 41 | +````ASP.NET |
| 42 | +<telerik:RadListBox ID="RadListBox1" runat="server" CheckBoxes="true" ShowCheckAll="true" AutoPostBack="false"> |
| 43 | + <Items> |
| 44 | + <telerik:RadListBoxItem Text="Music-Country" Value="Music-Country" /> |
| 45 | + <telerik:RadListBoxItem Text="Music-Rap" Value="Music-Rap" /> |
| 46 | + <telerik:RadListBoxItem Text="TV-Cartoons" Value="TV-Cartoons" /> |
| 47 | + <telerik:RadListBoxItem Text="TV-Drama" Value="TV-Drama" /> |
| 48 | + </Items> |
| 49 | +</telerik:RadListBox> |
| 50 | +```` |
| 51 | + |
| 52 | +````JavaScript |
| 53 | +function filterListBox(sender) { |
| 54 | + let checkedStates = []; |
| 55 | + let searchText = sender.value.toLowerCase(); |
| 56 | + |
| 57 | + let listBox = $find('<%= RadListBox1.ClientID %>'); |
| 58 | + let items = listBox.get_items(); |
| 59 | + |
| 60 | + // Save the checked states |
| 61 | + for (let i = 0; i < items.get_count(); i++) { |
| 62 | + let item = items.getItem(i); |
| 63 | + checkedStates.push(item.get_checked()); |
| 64 | + } |
| 65 | + |
| 66 | + // Filter items based on the search text |
| 67 | + for (let i = 0; i < items.get_count(); i++) { |
| 68 | + let item = items.getItem(i); |
| 69 | + let itemText = item.get_text().toLowerCase(); |
| 70 | + item.set_visible(itemText.indexOf(searchText) !== -1); |
| 71 | + } |
| 72 | + |
| 73 | + // Restore the checked states |
| 74 | + for (let i = 0; i < items.get_count(); i++) { |
| 75 | + let item = items.getItem(i); |
| 76 | + item.set_checked(checkedStates[i]); |
| 77 | + } |
| 78 | +} |
| 79 | +```` |
| 80 | + |
| 81 | +**Key Points:** |
| 82 | + |
| 83 | +- **Search Box**: The input element captures the user's search query and calls the `filterListBox()` function on every key press. |
| 84 | + |
| 85 | +- **JavaScript Filtering**: The `filterListBox()` function iterates over all items in the RadListBox, setting their visibility based on the search query. |
| 86 | + |
| 87 | +- **Preserving Checkbox States**: Before filtering, the checked states of each item are saved. After filtering, these states are restored, ensuring that checkbox functionality is not lost. |
| 88 | + |
| 89 | +## See Also |
| 90 | + |
| 91 | +- [RadListBox Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/listbox/overview) |
| 92 | +- [RadListBox Client-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/listbox/client-side-programming/overview) |
| 93 | +- [RadListBox Server-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/listbox/server-side-programming/overview) |
0 commit comments