Advanced 20 min read

Pagination, Searching, and Filtering

By the end of this lesson, you'll be able to:

  • Compare client-side and server-side pagination approaches
  • Implement a simple search/filter pattern above a datatable
  • Recognize when server-side filtering via Apex becomes necessary

Prerequisites: "Sorting, Row Actions, and Inline Editing"

Client-Side vs. Server-Side Pagination

  • Client-side: all matching data is loaded once, and pagination just slices which portion is displayed. Simple to implement, but doesn't scale — loading thousands of records up front is wasteful and slow.
  • Server-side: each page is fetched from Apex on demand (a "load more" or page-number pattern, using LIMIT/OFFSET in SOQL). More code, but scales to any dataset size — the right choice once record counts grow past what's reasonable to load all at once.

A Simple Client-Side Search/Filter Pattern

searchTerm = '';

handleSearchChange(event) {
    this.searchTerm = event.target.value;
}

get filteredAccounts() {
    if (!this.searchTerm) {
        return this.accounts;
    }
    const term = this.searchTerm.toLowerCase();
    return this.accounts.filter(account => account.name.toLowerCase().includes(term));
}
<lightning-input label="Search Accounts" onchange={handleSearchChange}></lightning-input>
<lightning-datatable data={filteredAccounts} columns={columns} key-field="id"></lightning-datatable>

A getter (Module 3) recomputes the filtered list reactively whenever searchTerm or accounts changes — a filteredAccounts property, not the raw accounts, is what's bound to the datatable's data.

When to Move Filtering to Apex

Client-side filtering works well once all the candidate data is already loaded — but if the full dataset is too large to load up front, the search itself needs to happen server-side, via a cacheable=true Apex method (Module 7) taking the search term as a parameter and returning only matching records.

Exercise

Write a filteredContacts getter that filters this.contacts by a searchTerm property, matching against the contact's email field.

Show hint

Follow the same pattern as the Account example.

JAVASCRIPT

Exercise

Challenge: explain, as a comment, why client-side filtering becomes a poor choice once an org has hundreds of thousands of Accounts.

Show hint

Think about what has to happen before filtering can even begin.

JAVASCRIPT

Pagination, Searching, and Filtering Quiz

1. What is the main drawback of client-side pagination for large datasets?

2. What does a filteredAccounts getter do differently from directly binding accounts?

3. When does server-side filtering via Apex become the better approach?

4. What SOQL keywords are commonly used for server-side pagination?

5. What binds a datatable to the filtered rather than the raw dataset?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Client-side pagination and filtering are simple and fast for small datasets, but server-side approaches — querying only what's needed — become necessary as data grows.