Implementing Search Functionality
1. Using IonSearchbar Component
| Property | Values | Description |
|---|---|---|
| searchbar | ion-searchbar |
Core component for implementing search functionality in Ionic apps. |
| debounce | number |
Delay in milliseconds before triggering the search event. Default is 250ms. |
| placeholder | string |
Text to display in the searchbar when no input is present. |
| value | string |
Current value of the search input field. |
| type | text | search | email | password | tel | url |
Input type for the searchbar element. |
| mode | ios | md |
Mode of the component (iOS or Material Design). |
Example: Search with Debounce and Event Handling
import { Component } from '@angular/core';\n\n@Component({\n selector: 'app-search',\n template: `\n <ion-searchbar\n debounce="500"\n (ionInput)="onSearch($event)">\n </ion-searchbar>\n `\n})\nexport class SearchPage {\n onSearch(event: any) {\n console.log('Search term:', event.detail.value);\n }\n}
Note: The
ionInput event fires on every keystroke, and the debounce property helps reduce the number of events for performance optimization.
2. Implementing Search Events
| Event | Description | Parameters | Usage |
|---|---|---|---|
| ionChange | Fires when the search input value changes | event.detail.value |
Use to filter results in real-time as the user types |
| ionInput | Fires when the input value changes (before ionChange) | event.detail.value |
Use for immediate UI feedback or debounced search |
| ionClear | Fires when the search input is cleared (via clear button) | None | Use to reset search results and clear filters |
| ionFocus | Fires when the search input gains focus | None | Use to show suggestions or highlight search area |
| ionBlur | Fires when the search input loses focus | None | Use to hide suggestions or finalize search |
Example: Handling Search Events
import { Component, ViewChild } from '@angular/core';
import { IonSearchbar } from '@ionic/angular';
@Component({
selector: 'app-search',
template: '<ion-searchbar (ionChange)="onSearch($event)" (ionClear)="clearSearch()"></ion-searchbar>'
})
export class SearchComponent {
@ViewChild('searchBar') searchBar!: IonSearchbar;
onSearch(event: any) {
const searchTerm = event.detail.value;
console.log('Searching for:', searchTerm);
// Filter data based on search term
}
clearSearch() {
console.log('Search cleared');
// Reset search results
}
}
Note: The
ionChange event is ideal for filtering large datasets in real-time, but consider debouncing to prevent excessive filtering during fast typing.
3. Configuring Search Debounce
| Property | Values | Description |
|---|---|---|
| debounce | 300 |
Time in milliseconds to wait before triggering search after user input |
| minLength | 1 |
Minimum number of characters required to trigger search |
Example: Configuring Debounce in Ionic Search
import { IonSearchbar } from '@ionic/angular';
@Component({
selector: 'app-search',
template: '<ion-searchbar [debounce]="500" [minLength]="2"></ion-searchbar>'
})
export class SearchPage {
// Debounce set to 500ms, minimum 2 characters
}
Note: Setting
debounce to 0 disables debouncing and triggers search on every keystroke.
4. Adding Search Icons
| Icon | Description | Usage | Notes |
|---|---|---|---|
search |
Standard search icon | <ion-icon name="search"></ion-icon> |
Default search icon in Ionic |
magnifying-glass |
Alternative search icon with magnifying glass | <ion-icon name="magnifying-glass"></ion-icon> |
Used for enhanced visual search |
search-outline |
Outline version of search icon | <ion-icon name="search-outline"></ion-icon> |
Lighter weight, suitable for light backgrounds |
Example: Using Search Icons in a Header
<ion-header>
<ion-toolbar>
<ion-title>Search Page</ion-title>
<ion-buttons slot="end">
<ion-button>
<ion-icon name="search"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
Note: Ensure that the search icon is accessible and has appropriate ARIA labels for screen readers.
5. Using Clear Button
| Property | Description | Default | Notes |
|---|---|---|---|
| clearButton | Enables or disables the clear button in the search input. | false |
Set to true to show a clear button when text is entered. |
| clearButtonIcon | Custom icon for the clear button. | 'close' |
Accepts any valid Ionic icon name or custom SVG. |
Example: Using Clear Button
<ion-searchbar
placeholder="Search items"
clearButton="true"
clearButtonIcon="close-circle">
</ion-searchbar>
Note: The clear button appears only when text is present in the search input.
6. Filtering List Data
| Property | Description | Default | Notes |
|---|---|---|---|
| filter | Function to filter items in the list | () => true |
Receives item and index, returns boolean |
| filterOptions | Options object passed to filter function | {} |
Used for custom filtering logic |
Example: Basic Filtering
const items = [
{ id: 1, name: 'Apple', category: 'fruit' },
{ id: 2, name: 'Carrot', category: 'vegetable' },
{ id: 3, name: 'Banana', category: 'fruit' }
];
const filtered = items.filter(item => item.category === 'fruit');
console.log(filtered); // [{ id: 1, name: 'Apple', category: 'fruit' }, { id: 3, name: 'Banana', category: 'fruit' }]
Example: Advanced Filtering with Options
const filterItems = (items, options) => {
const { searchTerm, category } = options;
return items.filter(item => {
const matchesSearch = item.name.toLowerCase().includes(searchTerm.toLowerCase());
const matchesCategory = !category || item.category === category;
return matchesSearch && matchesCategory;
});
};
const items = [
{ id: 1, name: 'Apple', category: 'fruit' },
{ id: 2, name: 'Carrot', category: 'vegetable' }
];
const result = filterItems(items, { searchTerm: 'app', category: 'fruit' });
console.log(result); // [{ id: 1, name: 'Apple', category: 'fruit' }]
7. Implementing Auto-Complete
| Property | Description | Default | Notes |
|---|---|---|---|
| autoComplete | Enables or disables auto-complete functionality | false |
Set to true to enable auto-complete |
| debounce | Debounce time in milliseconds for search input | 300 |
Reduces number of API calls during typing |
| minLength | Minimum number of characters to trigger search | 1 |
Prevents unnecessary API calls for short inputs |
Example: Auto-Complete with Debounce
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-search',
template: '<ion-searchbar [(ngModel)]="searchTerm" [debounce]="500" (ionChange)="onSearch($event)"></ion-searchbar>'
})
export class SearchComponent {
searchTerm = '';
onSearch(event: any) {
console.log('Searching for:', event.detail.value);
}
}
Note: The
debounce property helps reduce API load by limiting how frequently the search function is called.
Warning: Setting
minLength too high may prevent users from finding results for short queries.
8. Styling Searchbar
| Property | Values | Description |
|---|---|---|
| --background | transparent |
Background color of the searchbar |
| --border-radius | 0 |
Border radius of the searchbar |
| --box-shadow | none |
Box shadow of the searchbar |
| --color | #000 |
Text color of the searchbar |
| --placeholder-color | #999 |
Color of the placeholder text |
| --padding | 0 |
Padding inside the searchbar |
| --text-align | left |
Text alignment inside the searchbar |
Example: Customizing Searchbar Style
.custom-searchbar {
--background: #f5f5f5;
--border-radius: 8px;
--box-shadow: 0 2px 4px rgba(0,0,0,0.1);
--color: #333;
--placeholder-color: #666;
--padding: 10px;
--text-align: center;
}
Note: The
--background property can be used to change the background color of the searchbar. It is recommended to use a light color for better contrast.
9. Using Cancel Button
| Property | Description | Default | Notes |
|---|---|---|---|
| cancelButton | Enables or disables the cancel button in the search bar | false |
Set to true to show a cancel button that clears the search input |
| cancelButtonColor | Defines the color of the cancel button | medium |
Accepts any valid Ionic color (e.g., primary, secondary) |
Example: Cancel Button Usage
<ion-searchbar
[cancelButton]="true"
cancelButtonColor="danger"
(ionClear)="onClear()"
></ion-searchbar>
Note: The cancel button is only visible when the search input has content.
10. Handling Search Submit
| Property | Description | Default |
|---|---|---|
| ionSearchbar | Searchbar component for input | - |
| ionSearchbarSubmit | Event emitted on search submission | - |
Example: Handling Search Submit
import { Component } from '@angular/core';<br><br>@Component({<br> selector: 'app-search',<br> template: `<ion-searchbar (ionChange)="onSearch($event)" (ionSearchbarSubmit)="onSubmit($event)"></ion-searchbar>`<br>})<br>export class SearchPage {<br> onSearch(event: any) {<br> console.log('Search term:', event.detail.value);<br> }<br><br> onSubmit(event: any) {<br> console.log('Submitted search term:', event.detail.value);<br> // Perform actual search logic here<br> }<br>}
Note: The
ionSearchbarSubmit event is emitted when the user presses Enter or clicks the search button.