Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Forms and Data

Ionic Searchbar, Filtering and Infinite Scroll

Build search and paging in Ionic: ion-searchbar with debounce, local filtering, server search, ion-infinite-scroll with pages, and the end of the list.

Once a list grows past a screenful, users need two tools: search to find one item, and paging to avoid loading everything at once. Ionic's ion-searchbar and ion-infinite-scroll are the UI for both. This lesson covers debounced search, local filtering versus server search, and loading pages as the user scrolls, including the details that go wrong: duplicates, stale results and the end of the data.

HTML
<ion-header>
  <ion-toolbar>
    <ion-searchbar
      placeholder="Search notes"
      [debounce]="300"
      (ionInput)="onSearch($event)"
      (ionClear)="onSearch(null)"
      showCancelButton="focus"
    ></ion-searchbar>
  </ion-toolbar>
</ion-header>

Important properties:

  • debounce waits the given milliseconds after the last keystroke before emitting ionInput. 250 to 400 ms is a good range for server searches.
  • showCancelButton (never, focus, always) matches platform conventions.
  • inputmode, enterkeyhint="search" and type="search" choose the right keyboard and key label.
  • The value is in event.target.value, which may be null or undefined when cleared.

Debounce, explained

Without debounce, typing "ionic" sends five requests: "i", "io", "ion", "ioni", "ionic". With a 300 ms debounce, the search runs once, 300 ms after the last key. The logic is a timer that restarts on every call. Ionic does this for you in the searchbar; the next stand-in shows it so you can use it elsewhere.

JS Stand-in: debounce a search

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Local filtering

For small lists that are already in memory, filter on the client. Normalise the query and the text so searching is forgiving:

JS Local filter: case, accents and multiple words

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Ionic's own ion-list does not filter for you. You keep two arrays, all and visible, and replace visible when the query changes. Show a friendly empty state ("No notes match") when visible is empty.

For large data, send the query to the API: GET /notes?q=ionic&page=1. Combine it with:

  • the debounce above;
  • ignoring stale responses (see the data lesson);
  • resetting to page 1 whenever the query changes;
  • a minimum length (2 or 3 characters) for expensive searches.

Infinite scroll

ion-infinite-scroll fires an event near the bottom of the content so you can append the next page:

HTML
<ion-content>
  <ion-list>
    <ion-item *ngFor="let n of notes; trackBy: trackById">
      <ion-label>{{ n.title }}</ion-label>
    </ion-item>
  </ion-list>

  <ion-infinite-scroll threshold="120px" (ionInfinite)="loadMore($event)" [disabled]="done">
    <ion-infinite-scroll-content loadingText="Loading more..."></ion-infinite-scroll-content>
  </ion-infinite-scroll>
</ion-content>
TypeScript
async loadMore(event: InfiniteScrollCustomEvent) {
  try {
    const page = await getNotesPage(this.nextPage);
    this.notes = [...this.notes, ...page.items];
    this.nextPage++;
    this.done = !page.hasMore;
  } finally {
    await event.target.complete(); // allow the next trigger
  }
}

Rules:

  • Call complete() every time, even on error, or no further loads occur.
  • Set disabled when there is no more data. Do not show a spinner forever.
  • Append to the array immutably; keep stable ids so rows are not redrawn.
  • On pull to refresh, reset to page 1 and re-enable the infinite scroll.

Stand-in: a paged loader

This runnable model paginates a fake dataset and prevents the classic bugs: duplicate loads while one is running, and loading past the end.

JS Stand-in: paging with a guard

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Search and paging together

Combine them by treating the query as part of the paging key. When the query changes, reset the list and the page; when it stays the same, append.

JS Stand-in: reset paging when the query changes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Performance and UX details

  • Cap in-memory items for very long sessions, or use virtual scrolling (lists lesson).
  • Keep the scroll position after navigating to a detail page and back: since pages stay alive, this works for free.
  • Show the result count or a "that's everything" footer to signal the end.
  • On slow networks, disable the infinite trigger while an error banner with Retry is visible.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the debounce property on ion-searchbar do?

    Debounce delays the event until typing pauses, reducing requests.
  2. 2. What must you call in an ionInfinite handler?

    complete() re-arms the trigger; without it no further pages load.
  3. 3. What should happen to paging when the query changes?

    A new query means a new result set, so start again at page 1.
  4. 4. How do you stop infinite scroll when data ends?

    Disabling the component stops further triggers when no more pages exist.

Summary

  • ion-searchbar with debounce emits ionInput after typing pauses.
  • Filter small lists locally with normalised text; use server search for big data.
  • ion-infinite-scroll triggers page loads; always call complete() and disable at the end.
  • Guard against double loads, stale responses and duplicates.
  • Reset paging whenever the search query changes.

Next: Capacitor Overview and Project Setup