Overview
A filter is a set of elements that share one data-filter-target: the id of the container that holds the items. Each element is a surface of the filter: a search box, a nds-filter Dropmenu of option groups, a row of applied filters, a count. The surfaces need no common parent, so a Toolbar usually holds them above the items. Each item carries data-filter marks with its values, and the menu’s options match against them.
Pick another component when:
- the user moves between sections, not values in one list: Tabs or Side Menu
- the field suggests matches as the user types: Autocomplete
- the user picks values for a form, not for a list: Multiselect
Markup
-
Commercial registration Updated200
-
Building permit Updated1500
-
Vehicle registration renewal Updated100
-
Driver's license renewal Updated400
-
Municipal license Updated800
-
Housing support request UpdatedFree
-
Trade name reservation Updated200
-
Truck operating card Updated2500
Variants
Every card carries three marks: sector, fee and updated. A group in the menu filters by one of them, and a mark with no group is ignored, so the cards stay the same for every choice. #flt-sector is the Sector group built by the script: the Sector type, Fixed list and Collapsible rows need Auto. Each Group options choice puts back the Sector and Fee groups, one row each: write both. The Date group is a Date Picker you write in either choice. Group and part rows go into .nds-dropmenu-scroll or .nds-toolbar.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Group options | Auto (default) (hint: The script builds the groups) | canon #filter-part-sector |
.nds-dropmenu-scroll |
data-filter-type on empty groups: the script builds the Sector options from the cards’ sector marks, and the Fee slider from its attributes |
| Group options | Auto (default) (hint: The script builds the groups) | canon #filter-part-fee |
.nds-dropmenu-scroll |
|
| Group options | Manual (hint: You write the groups in the HTML) | canon #filter-part-sector-manual |
.nds-dropmenu-scroll |
Groups you write, with no data-filter-type: a <fieldset> of checkboxes for Sector, and a range Slider for Fee. For options that need their own markup |
| Group options | Manual (hint: You write the groups in the HTML) | canon #filter-part-fee-manual |
.nds-dropmenu-scroll |
|
| Date group | Date group (default) (hint: An Updated date range in the menu) | canon #filter-part-updated |
.nds-dropmenu-scroll |
A Date Picker in a group filters by the cards’ updated days: a range, a day, a month or a year. Give the picker the two .nds-date-value inputs shown. See Date Range |
| Sector type | Checkbox (default) | — | #flt-sector |
Any number of values. A card matches when it has one of them |
| Sector type | Radio | [data-filter-type="radio"] |
#flt-sector |
One value. The script adds All as the first option |
| Sector type | Switch | [data-filter-type="switch"] |
#flt-sector |
Matches like Checkbox, drawn as switches. For on/off features |
| Menu (any) | Fixed list (hint: The options are a list you write, not read from the cards) | [data-filter-values='["Business","Health","Housing","Transport"]'] |
#flt-sector |
The options come from data-filter-values, in its order, also when no card has the value (Health). The script reads no cards for them. For a list the server owns |
| Menu (any) | Collapsible (hint: The Sector group opens and closes) | [data-filter-accordion] |
#flt-sector |
The group becomes an Accordion item, closed, with a count of the picked values on its header. For a group with many options |
| Menu (any) | Option search (hint: A search box above the options) | [data-search] |
.nds-filter |
A search box at the top of the menu narrows the options. data-search="10" shows it only from 10 options |
| Menu (any) | Option search (hint: A search box above the options) | [data-search-item] |
.nds-checkbox |
Only on a Sector group you write yourself: the search finds options that carry it |
| Menu (any) | No menu (hint: A search box alone is a filter) | remove | .nds-filter |
Search only. The search box needs no nds-filter element |
| Search box | Search box (default) (id: search) | canon #filter-part-search |
.nds-toolbar (start) |
Searches the text of each card. Enter or the Search button applies it |
| Applied chips | Applied chips (default) | canon #filter-part-applied |
.nds-toolbar |
One removable chip per applied value and for the search term. Hidden while nothing is applied |
| Suggestions | Suggestions (demo: + search) (hint: Chips that fill the search box) | canon #filter-part-suggest |
.nds-toolbar |
Chips that write a search term into the search box. Hidden once a filter is applied. Needs the Search box |
| Result count | Result count | canon #filter-part-count |
.nds-toolbar |
The script writes the number of matching items into [data-filter-count] |
Behavior
Option Groups
An element with data-filter in the menu is a group. Its options come from one of four places: the marks on the items (data-filter-type), a list on the group (data-filter-values), inputs you write, or your script (renderFilter()). From the marks, the script makes one option per value it finds, sorted, in a <fieldset> that replaces the element.
The user picks values in the menu, and Apply applies them all at once. Closing the menu without Apply puts back the values that were applied, so the search box or a chip never applies a value the user did not apply. In a surface with no Apply button, a pick applies at once, and closing the menu keeps it. A checkbox or switch group matches an item that has any picked value. An item must match every group that has a value.
Values and Labels
A mark’s text is its value. data-filter-value on the mark sets a value apart from the text: the URL and the match use the value, and the option and the chip show the text. data-filter-values on a group lists its options: an array of values, or an object of value-label pairs. The script then reads no items for that group.
A filter can also drive a view that holds no items, such as a Calendar or a map. Point data-filter-target at the view, give each group data-filter-values, and update the view from criteria.filters in nds:filter:change. The view has no .nds-card (or nothing that matches its data-filter-items), so the filter hides nothing in it and shows no alert.
Range Slider
data-filter-type="slider" builds a Slider over the number in each item’s data-filter-value. With data-filter-min and data-filter-max it has two thumbs. With data-filter-max alone it has one thumb, “up to”, from 0. The applied range shows as one chip, and removing it puts both thumbs back at the ends. In the URL and the request, a range is two keys: fee-min=200&fee-max=1500. An “up to” slider sends fee-max only. For other keys, write the slider yourself and name its thumbs (see Manual Options). The chip groups thousands only when the slider’s output does: with data-filter-currency or data-filter-unit, or when your own output has .nds-number-format.
Date Range
A group that holds a Date Picker filters by date. In range mode (nds-date-range) the user picks a first and a last day. A single picker picks one day, or a whole month or year when its format has no day (MM/YYYY, YYYY), Hijri months and years too. Give the picker two .nds-date-value inputs (one for a single day) with data-date-format="YYYY-MM-DD" and data-calendar="gregory": the script reads the days there, so the field may show Hijri. Each item’s mark carries its day in data-filter-value as a Gregorian YYYY-MM-DD, or as a date and time; any other format never matches, and the console says so. The applied range shows as one chip in the field’s format, and removing it clears the field. A range with one end, from a link or setFilterValues(), shows ≥ or ≤ before its day, as the slider’s “up to” does. In the URL and the request it is two keys, updated-min=2026-10-06&updated-max=2026-10-18, and an open end is left out. A single picker sends the first and the last day of its pick. A name on a .nds-date-value sets its key, such as from and to.
The date group can also sit outside the menu, in the toolbar beside the search box: give its <fieldset> the filter’s data-filter-target, and leave out nds-dropmenu-group. There no Apply button reaches it, so a date applies as soon as the user saves it in the calendar or types it, as a search applies on Enter. In a filter form (server mode), or in a panel with its own Apply button, it waits for the submit or the Apply like the other groups, and the request carries its two keys.
Manual Options
Leave out data-filter-type and write the inputs in the group yourself, as a Checkbox or Radio group. The script reads every checkbox, radio and .nds-switch-input in it, and the chip shows the option’s .nds-label. Each value matches a mark’s value, in any letter case. Inputs added later join the group by themselves. For Option search, add data-search-item to each .nds-form-container.
A group that holds a range Slider (.nds-slider-container) is a range filter on your slider: its min and max are the full range. Put data-filter-currency or data-filter-unit on the group for the chip. A name on a thumb sets its key in the URL and the request.
Options from a Script
renderFilter(name, values, type) builds a group’s options from values your script fetched, and a second call replaces them. Use it for cascading groups: call it again when the parent group changes. Leave data-filter-type off that group, as below. refresh() rebuilds every group that has it from the items, and would replace your values.
Collapsible Groups
data-filter-accordion turns one group into an Accordion item, closed at load, with data-filter-legend as its header. A tag on the header counts the picked values and hides at zero. Each such group is its own accordion. Wrap several in one <div class="nds-accordion"> to make them one. With Option search on the menu, a search term opens every group. Clearing it, or closing the menu, closes the groups it opened.
Applied Chips
A nds-filter-applied row with a nds-chips element inside shows one chip per applied value, and one for the search term. Removing a chip removes its value and applies the rest at once. data-chip-class on the row sets the chips’ classes. With a [data-filter-query] element on the filter, the search term shows there, in quotes, and gets no chip.
Suggestions
A nds-auto-fill row holds nds-item chips. A click writes the chip’s text into the field named by data-target, and data-autofill-apply also runs the search. Give the row data-filter-target too. When the filter also has an applied-chips row, it hides the suggestions while a value or a search term is applied. For suggestions that come from a server as the user types, add Autocomplete to the search box.
Form Submission
For results from a server, add a <form> with the same data-filter-target and data-filter-submit. Apply, the search box, the menu’s Reset and every chip removal then submit the form, and the server returns the filtered page. The script writes the form’s id into the form attribute of each control in the surfaces, so a surface outside the form still submits. Give the form an id. The request and the URL are one query, under the names you write: a group sends one key per value (sector=business§or=housing), and a range one key per end. The script puts a range end with no name, and a search field with no name, in hidden inputs.
A group on a server page lists its options with data-filter-values: the page holds only one page of results, and options read from it would miss the others. Generated options submit under the group’s name, one key per value, as in the URL. The page the form loads reads them back, so the options stay picked and the chips show.
AJAX Submission
data-ajax on the form sends the request without a page load. For an HTML response, the script swaps the item container for the element with the same id in the response. It stops the components in the old container and starts the ones in the new container before nds:filter:success fires, so the page calls nothing for them. A response without that element counts as a failure. For a response with a JSON Content-Type, the script changes no items: render them in nds:filter:success. While a request runs, the menu takes no clicks and the button that sent it shows a spinner. A newer request cancels the one in flight.
On a failure the items stay as they were, and so do the chips, the badge, the options and the URL: the script puts them back to match the items. An unapplied pick in the open menu is lost. The script shows an error toast, unless a listener calls preventDefault() on nds:filter:error. The no-results alert never shows in form mode: call showNoResultsAlert() from your response handler.
Your Own Request
Call preventDefault() on nds:filter:request to send the request yourself, such as a POST with a JSON body. The event fires for Apply, a chip removal, the menu’s Reset, reset() and the search box. The chips, the badge and the URL already show the new filters when it fires: call detail.rollback() if your request fails.
The event fires on the filter and bubbles, so a form that does not hold the filter never hears it. Listen on document and check detail.form. In detail.criteria, each group in filters is an array of values, even with one value picked. A range is one min-max string in it. The search term is the search string.
The filter reads the address at load and shows its filters in the search box, the chips and the menu, but it sends no request. When your script loads the first items from your own API, send that first request with the same filters: NDS.Filter.whenReady() gives you the filter, and getCriteria() its filters. Turn on the Pagination URL sync (data-page-param) so the page stays in the address too, and send the first request for that page: a shared link then opens the same page.
Sort
A filter also drives the Sort triggers that carry its data-filter-target. Copy the Sort menu from its page, and write data-filter-target on each trigger where Sort has data-sort-target. Put the menu beside the filter. Each item carries the value it sorts by in data-sort-<key>, such as data-sort-title="Commercial registration".
The sort stays in the URL as sort, with order=desc for a descending order. Ascending writes no order. A sort in the URL wins over a sorted-asc or sorted-desc state written on a trigger, so a shared link opens in its own order. On a page with sort buttons, do not use sort or order for anything else, not even as a group name: a descending sort writes order=desc over it.
Sort is not a filter value. Decide by what the server returns. All matching items, paged in the browser: add data-sort triggers, and the browser sorts. One page of items: the server sorts, so add no data-sort. Put the Dropmenu picker in the form instead, with data-select-name="sort" and the filter’s data-filter-target, and give each item the value your server reads, such as data-value="title-asc". A pick sends the form. The request and the URL carry its value, and a reload picks it again.
Built-in Features
-
Auto-initialization
The script starts one filter for each
data-filter-targetvalue and joins every element that carries it. A search box alone is a working filter. -
Shareable URL State
The search term and the applied values stay in the URL query, so a reload or a shared link opens the same list. The search term's key is the search field's
name, orsearchwhen it has none. A group's key is itsdata-filtername, once per value, and a range has a key for each end. Links that joined the values with commas still open, so a checkbox or switch value must not contain a comma. The sort issortandorder. -
No Results Alert
When no item matches, a warning alert with a Clear filters button shows in the item container. It closes when items match again.
data-filter-no-resultson the container sets its text. A container with no items at all shows no alert: give it Empty for that message. -
Filter Count Badge
When a filter changes the count, a screen reader hears the count line, in your words. The
nds-filter-btntrigger shows a badge with the number of applied values. The Apply button adds the number of picked values to its label. -
Any Item Type
Cards by default.
data-filter-itemssets the items: a class, a tag such astr, or any selector. On a table, put it and the container'sidon the<tbody>: it takes only the body's own rows. -
Pagination Support
In a Pagination container, the pages hold only the matching items, and a new filter goes back to page 1.
-
No Flash on Load
A container with
data-filter-itemsstays hidden until its filter has applied the values in the URL. A shared link never shows the full list first. -
Deferred Option Build
Options for a group in the closed menu are built on its first open, not at page load. A group with a value in the URL is built at load.
-
Keyboard
Enter in the search box runs the search. Enter in the open menu runs Apply. Every option is a real input in a
<fieldset>with its legend. Slider thumbs have labels and move with the arrow keys. -
Programmatic Control
Set values and the search term, read the criteria and the matching items, or reset the filter from a script, through
NDS.Filter.
Best Practices
- Filter in the browser when every item is on the page and the list is short, up to a few hundred items. For more, use form submission.
- Pick the group type by the question. Checkbox: any of several values. Radio: exactly one, with All to undo it. Switch: on/off features. Slider: a number range.
- Give a yes-or-no field a Radio group, so All undoes the pick. Give a number field a Slider.
- Keep a Reset button (
data-filter-action="clear") next to Apply in the menu footer. - Keep
hiddenon the applied-chips row. The script shows it once a value is applied. - Keep legends to one or two words.
data-filter-legendis the group’s heading in the menu. - Make a group with many options collapsible, and keep a group of 3 or 4 options open.
- Give each mark a value with
data-filter-valuewhen its text is translated or formatted, such as a price that reads “SAR 250”. - Put
data-filter-itemsonly on a container that has a filter. The container stays hidden until a filter starts on it. - On a paged list, show the count with the Pagination records counter in place of
data-filter-count: it counts the matching items and adds the range shown. - Give each filter on a page its own group names: the names are the URL keys, and two filters with one name share it.
- To keep a URL key that links already use, write it as the group name, or as the search field’s
namefor the search term. - Add
data-filter-ignoreto a text field inside a surface that is not the filter’s search, so the script does not take it as one.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-filter |
the filter’s .nds-dropmenu |
Marks the menu of option groups. The filter’s events fire on it |
nds-filter-btn |
the menu’s trigger button | Shows the count badge, and the spinner while a form submits. data-filter-btn does the same |
nds-filter-applied |
a surface with a .nds-chips inside |
The row of applied chips |
nds-auto-fill |
a surface with .nds-item chips |
The row of suggestions. Hidden while a filter is applied, when the filter has an applied-chips row |
nds-filter-menu |
the filter’s .nds-dropmenu-menu |
The script adds it. Style the menu by this class: it can move to <body> |
nds-filter-range |
the <fieldset> of a slider group |
The script builds it |
nds-filter-hidden-inputs |
a <div> in the submission form |
The script builds it and writes the hidden inputs into it on each submit |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-filter-target |
every surface, and the submission <form> |
The id of the item container. Every element with the same value is part of one filter |
data-search |
.nds-filter |
A search box at the top of the menu narrows the options. A number shows it only from that many options. See Dropmenu |
data-filter |
an element in a surface | A group. The value is the group’s name and its URL key |
data-filter="search" |
a text field in a surface, or its wrapper | The search field. In the menu, Apply runs it. A .nds-search-input outside the menu needs no attribute |
name |
the search field | The search term’s URL key. With no name, the key is search. name="q" keeps a q key that links already use |
data-filter-type |
a group | checkbox, radio, switch or slider: the script builds the options. Without it, the script uses the inputs you write in the group, or the options of renderFilter() |
data-filter-legend |
a group | The group’s heading |
data-filter-values |
a group with checkbox, radio or switch |
The options as JSON: an array of values, or an object of value-label pairs. The script reads no items for this group |
data-filter-variant |
a group | A class the script adds to each input (on a switch, to its .nds-switch-toggle), such as nds-primary |
data-filter-all-label |
a radio group | The label of the All option. The default is All, or الكل on an Arabic page |
data-filter-no-all |
a radio group | No All option |
data-filter-accordion |
a group | Makes it a collapsible Accordion item |
data-filter-min, data-filter-max |
a slider group | The ends of the range. With data-filter-max alone, the slider has one thumb from 0. data-filter-max is required and must be above the minimum |
data-filter-step |
a slider group | The step of a thumb. The default is 1 |
data-filter-currency |
a slider group | A currency code, such as SAR, on the slider values and the chip |
data-filter-unit |
a slider group | A unit after the slider values and the chip, such as km. For a value that is not money |
data-filter-action |
a button in a surface | apply applies the picked values and closes the menu. clear clears the menu’s values and applies the result, with the menu open. reset clears every value and the search term |
data-filter-count |
an element in a surface | The script writes the number of matching items into it |
data-filter-query |
an element in a surface | The script writes the search term into it, in quotes. The search term then gets no chip |
data-chip-class |
.nds-filter-applied |
The classes of each chip. The default is nds-primary |
data-target |
.nds-auto-fill |
The id or name of the field a chip fills |
data-autofill-apply |
.nds-auto-fill |
A chip click also runs the search |
data-filter-ignore |
a text field, or an element around it | The script does not use the field as the search field |
data-filter-items |
the item container | The items: a class name without the dot, a tag, or a selector. The default is .nds-card. With it, the container stays hidden until the filter starts |
data-filter-no-results |
the item container | The no-results alert’s text, in place of “No item matches the current filters.” Say what the user can change, such as “No requests match. Try another status” |
data-total-count |
the item container | A count from the server. [data-filter-count] shows it in place of the matching items |
data-filter |
an item, or an element inside one | A mark: the item’s value for the group with that name. Its text is the value |
data-filter-value |
a mark | The value in place of the text. A slider group reads it as a number |
data-filtered |
an item | The script sets it on an item that does not match, and removes it when the item matches again. CSS hides the item. Do not write it |
data-filter-submit |
a <form> with data-filter-target |
The filter submits this form in place of filtering in the browser |
data-ajax |
the submission <form> |
Sends it without a page load |
data-state="submitting" |
.nds-filter |
The script sets it when the form submits, and removes it when the response arrives or fails. While it is set, the menu takes no clicks |
data-state="loading" |
.nds-filter-btn, or the search button |
The script sets it on the button that submitted the form, and removes it when the response arrives or fails |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--dropmenu-min-width |
250px |
The menu’s minimum width. Set it on .nds-filter: the menu keeps it when it moves to <body> |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_filter.scss.
| Token | Preview | Value |
|---|---|---|
--text-displayRead by color | var(--colors-neutral-800)Dark mode var(--colors-neutral-100) |
JavaScript
NDS.Filter loads after the page shows. Until then each call returns a Promise, so a sync read such as getInstance() returns no instance. Use whenReady() from your own scripts.
| Method | Effect |
|---|---|
NDS.Filter.init() |
Starts a filter for each data-filter-target value that has none |
NDS.Filter.create(element) |
Starts the filter of one surface and returns it. On a started filter, it returns that one |
NDS.Filter.destroy(element) |
The same as destroy(). Any element of the filter’s surfaces works |
NDS.Filter.getInstance(element) |
The filter of any surface, by element or selector |
NDS.Filter.getByTarget(id) |
The filter of an item container id |
NDS.Filter.whenReady(element, callback) |
Runs callback with the filter, now or once it starts |
NDS.Filter.refresh(container) |
Runs refresh() on each filter whose item container is container, is inside it, or holds it. Starts the filters of new surfaces in it first. Skips form-mode filters. NDS.init.refresh(container) calls it for you |
setFilterValues(name, values) |
Picks these values in the group, and applies them. An empty array clears the group. On a slider group, pass one lo-hi string, for example ['200-1500']. On a date group, pass one from..to string of YYYY-MM-DD days, either end left out for an open range: ['2026-10-01..2026-10-15'], ['..2026-10-15'] |
setSearchValue(text) |
Sets the search term, and applies it |
removeFilterValue(name, value) |
Removes one value, and applies the rest |
removeSearchFilter() |
Clears the search term, and applies |
resetRangeFilter(name) |
Puts a slider group back to its full range, and applies |
renderFilter(name, values, type) |
Builds the group’s options from values. type is checkbox by default |
getCriteria() |
A copy of the criteria: { search, filters: { name: [values] } } |
getVisibleItems(), getHiddenItems() |
The matching items, and the others |
syncFilters() |
Applies the picked values. In form mode it updates the chips, the badge and the URL, and does not submit |
submitForm() |
Submits the form in form mode |
clear() |
Clears every value, the search term and the sort, and applies nothing |
reset() |
Clears everything and shows every item. In form mode it submits the form again |
refresh() |
Reads the items again and rebuilds the groups that have data-filter-type and no data-filter-values, keeping the current page. New items join the active sort |
reapplyUrlParamsForFilter(name) |
Picks the URL’s values in a group whose inputs you added after load |
showNoResultsAlert(), dismissNoResultsAlert() |
Shows or closes the no-results alert in the item container. Call them from your response handler in form mode. Without an item container they do nothing |
clearDropmenuFilters() |
Empties every dropmenu group |
syncUrlParams() |
Writes the criteria to the URL |
queryAll(selector) |
Returns the matches inside the filter’s own surfaces only |
targetId, items, criteria, filterInputs, sort, isFormMode, isAjaxMode |
Properties. Read-only |
destroy() |
Removes the listeners and shows every item |
In form mode, each setter submits the form. Setters in one script run share one submit, and a submitForm() in that run replaces it.
| Event | Fired on | Detail |
|---|---|---|
nds:filter:ready |
.nds-filter, or the first surface |
{ instance }: the filter’s instance |
nds:filter:change |
the same | { filter, criteria, totalItems, visibleItems, hiddenItems }. filter is the element the event fires on. It fires after each apply, also when nothing is applied, and after reset(), once the URL, the chips and the page have updated. The last three are numbers, null in form mode. The values are in criteria.filters, not in criteria |
nds:filter:reset |
the same | { filter, totalItems } |
nds:filter:clear |
the same | { filter }, on clear(), reset() and the menu’s Reset |
nds:filter:submit |
the same | { criteria, form }, before a form submits with a page load, after its fields pass. preventDefault() stops it |
nds:filter:request |
the same | { criteria, form, hiddenInputsContainer, rollback }, before an AJAX request. preventDefault() stops the request. Add your own fields to hiddenInputsContainer here |
nds:filter:success |
the same | { success, isJson, data, html, fullHtml, form }, after an AJAX response. data is the JSON, html the new container’s HTML, fullHtml the whole response, to update parts outside the container, form the element the event fires on |
nds:filter:error |
the same | { error, form }, after a network error, an error status, a timeout, or a response without the item container. preventDefault() stops the error toast |
nds:form:valid, nds:form:invalid |
the same | As in Forms, for a submission form without data-ajax |
The full API is in the banner of _js/nds-filter.js.