Filtering
The Dashboard passes the search text to query as filter and returns
to page 1 when it changes. The List query decides which records match.
Filtering in memory
Section titled “Filtering in memory”Replace the query in the Web orders example with
this version to match order IDs and SKUs without regard to case:
query: ({ page, limit, filter }) => { const search = filter?.trim().toLowerCase() const matches = search ? orders.filter( ({ id, sku }) => id.toLowerCase().includes(search) || sku.toLowerCase().includes(search), ) : orders
const offset = (page - 1) * limit return Effect.succeed({ items: matches.slice(offset, offset + limit), totalCount: matches.length, })},Filter before paginating. totalCount is the number of matching records
across all pages, not just the number on the current page.
Filtering large datasets
Section titled “Filtering large datasets”For a database-backed List, filter and paginate in the database instead
of loading every order into the application. For example, make the
Web orders filter an exact SKU search. If you store SKUs in uppercase,
trim and uppercase the search text before binding it as :sku.
Create an index on (sku, id) so the database can find orders for the
SKU and read them in order by ID:
CREATE INDEX web_orders_sku_id_idx ON web_orders (sku, id);In the List’s query callback, run a page query and a count query with
the same filter:
SELECT id, sku, quantityFROM web_ordersWHERE sku = :skuORDER BY idLIMIT :limit OFFSET :offset;
SELECT COUNT(*) AS total_countFROM web_ordersWHERE sku = :sku;Bind :limit to limit and :offset to (page - 1) * limit. Return
the page rows as items and the count as totalCount. For an empty
filter, omit the WHERE clause from both queries.
Unlike the in-memory example, this matches a whole SKU, not a
substring. A search like LIKE '%123%' generally can’t use an ordinary
index to find matching rows efficiently. Check the query plan with
representative data: exact counts for broad filters and large offsets
can still be expensive.