Skip to main content
Login

Sign in to access your account

Selection - National Design System

Selection lets users pick items in any list, so one action can apply to all of them

Overview

Selection works on any list: table rows, cards or a plain list. Users pick items, and one action (export, delete, assign) applies to all of them. Each item holds a checkbox. A select-all checkbox picks every item on the current page, and a counter shows how many are picked: “2 selected of 48 requests”. The select-all and the counter name the list through data-selection-target. Your code reads the selection through NDS.Selection and the nds:selection:change event.

Pick another component when:

  • you only show how many items match a search or a filter: Filter
  • you only show which items are on the current page: Pagination
  • the user picks options in a form field: Checkbox

Markup

Preview
0 selected of 3 services
<div class="nds-toolbar"> <div class="nds-toolbar-row"> <div class="nds-toolbar-start"> <div class="nds-form-container nds-checkbox"> <div class="nds-form-header"> <label for="selection-services-all"> <span class="nds-label" data-i18n="selection.select_all">Select all</span> </label> </div> <div class="nds-form-control"> <input type="checkbox" id="selection-services-all" class="nds-checkbox-input" data-selection-target="selection-services"> </div> </div> <span class="nds-toolbar-text" data-selection-target="selection-services"> <b data-selection-count>0</b> selected of <b data-selection-total>3</b> services <button type="button" data-selection-all hidden>(<b data-i18n="selection.select_all">Select all</b>)</button> <button type="button" data-selection-clear hidden>(<b data-i18n="selection.clear_all">Clear all</b>)</button> </span> </div> </div> </div> <div id="selection-services" class="nds-grid" style="--max-col: 3; --mid-col: 2; --min-col: 1;"> <label class="nds-card nds-stroke"> <div class="nds-card-content"> <div class="nds-card-text"> <div class="nds-card-corner"> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" name="services" value="passport" class="nds-checkbox-input" aria-label="Select Passport Renewal"> </div> </div> </div> <span class="nds-card-title">Passport Renewal</span> <p class="nds-card-description">General Directorate of Passports</p> </div> </div> </label> <label class="nds-card nds-stroke"> <div class="nds-card-content"> <div class="nds-card-text"> <div class="nds-card-corner"> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" name="services" value="licence" class="nds-checkbox-input" aria-label="Select Driving Licence"> </div> </div> </div> <span class="nds-card-title">Driving Licence</span> <p class="nds-card-description">General Department of Traffic</p> </div> </div> </label> <label class="nds-card nds-stroke"> <div class="nds-card-content"> <div class="nds-card-text"> <div class="nds-card-corner"> <div class="nds-form-container nds-checkbox"> <div class="nds-form-control"> <input type="checkbox" name="services" value="registration" class="nds-checkbox-input" aria-label="Select Commercial Registration"> </div> </div> </div> <span class="nds-card-title">Commercial Registration</span> <p class="nds-card-description">Ministry of Commerce</p> </div> </div> </label> </div>

Behavior

Select All

A checkbox with data-selection-target selects every item on the current page, or clears them all. It shows a half-checked state when only some items on the page are selected, and it updates when the user turns the page. Items selected on other pages stay selected, so each page the user selects adds to the selection. While a filter hides items, it selects only the items the filter shows. Put it outside the list, so it does not count as an item.

Put a button with data-selection-all in the counter, after the nds-selection-view. A click selects every item the filter shows, on every page. Ship it with hidden: the script shows it only while some of those items are selected, but not all.

A button with data-selection-clear after it clears every item, on every page. Ship it with hidden too: the script shows it once every item the filter shows is selected. So the two links never show together: Select all takes its place until everything is selected.

A disabled checkbox is yours to set. The select-all, both links and clear() leave it as it is. The half-checked state and the links look only at the items users can change, while the selected count still includes a checked disabled item.

Records Swap

Use it on a paged list, so one line serves both browsing and selecting. The counter holds two lines: a nds-records-view for Pagination and a nds-selection-view for the selection. Give it both data-paged-target and data-selection-target, with the same list id. While anything is selected, the script hides the records line and shows the selection line. Ship the selection line with hidden, so the line is correct before the script loads.

Built-in Features

  • Auto-initialization

    The script starts when the page has a data-selection-target element. Tables add one to their header checkbox, so a table with a select-all needs no attribute.

  • Content-Agnostic Selection

    An item is selected when it holds a checked input.nds-checkbox-input, or carries data-state="selected". Table rows, cards and plain checkbox lists work the same way.

  • Selected State

    The script sets data-state="selected" on each item whose checkbox is checked, and removes it when the box is cleared. Use it to style selected items.

  • Full-Selection Count

    The count includes selected items on other pages and items a filter hides. It matches what Export sends when its scope is the selection.

  • Live Updates

    When items with a checkbox are added or removed, the count, the total and the select-all update on their own.

  • Number Formatting

    Numbers get the thousand separators of the page's language, in Latin digits, the same rule as the Pagination records line.

  • Programmatic Control

    Select or clear a list, read its selected items and listen for each change through NDS.Selection.

Best Practices

  • Add a counter wherever a bulk action exists (export, delete, assign). The number tells users what the action will change.
  • Write the counter’s sentence in the page’s language, with your own noun, such as «0 محدد من 3 خدمات». The script fills in only the numbers.
  • Put the select-all and the counter in a Toolbar above the list. Users look there for record counts.
  • Write the sentence in your markup, in your language. The script writes only the numbers, so plurals and Arabic phrasing are yours.
  • Do not show a count for the current page only. Bulk actions act on the whole selection.
  • Turn a bulk action on and off from nds:selection:change. Do not add your own change listeners to the checkboxes.
  • Give every item checkbox an aria-label that names the item: “Select REQ-1001”. Give the select-all a visible label, or an aria-label in a table header.

API

Classes

Only the records swap uses these. A counter that always shows needs neither.

Class Element Effect
nds-records-view child of the counter The script hides it while anything is selected
nds-selection-view child of the counter The script shows it while anything is selected

Data Attributes

The list is the element with the id that data-selection-target names. Its items are its .nds-page-item elements. A list without them uses its direct children. A <tbody> uses only its own rows, and its detail rows (tr.nds-sub) are never items, so a table nested in a detail row is a list of its own. An item’s checkbox is the first input.nds-checkbox-input in it, so a checklist inside a card never selects the card.

Attribute Element Effect
data-selection-target="id" any element except a checkbox Makes the element a counter for the list
data-selection-all a button in a counter A click selects every item the filter shows, on every page. The script removes hidden from it while some of those items are selected, but not all, and sets it otherwise. Write hidden on it at page load
data-selection-clear a button in a counter A click runs clear(). The script removes hidden from it while every item the filter shows is selected, and sets it otherwise. Write hidden on it at page load
data-selection-count an element in a counter The script writes the number of selected items into it. Write the count at page load in it (usually 0), so the line shows a number before the script runs
data-selection-total an element in a counter The script writes the number of items in the list into it. On a paged list, data-paged-count from Pagination can take its place: it counts only the items that match the filter
data-state~="has-selection" a counter The script sets it while anything is selected
data-selection-target="id" an input.nds-checkbox-input Makes the checkbox the list’s select-all. It acts on the current page only. Tables add it to the header checkbox, and give the <tbody> an id if it has none
data-state~="selected" an item The script sets it when the item’s checkbox is checked, and removes it when the box is cleared. Set it yourself on an item with no checkbox. Select all sets it too, and clear() removes it from every item except one with a disabled checkbox

JavaScript

The NDS.Selection script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

The script starts on its own. Call refresh(list) after a change that fires no change event: a checked set from code, or an item marked only with data-state. A method that takes a list takes its id or the element.

Method Effect
NDS.Selection.init() Adds the document listeners if they are missing. The loader calls it
NDS.Selection.selectAll(list, on, { scope }) Selects every item the filter shows, or clears them when on is false. on defaults to true. With scope: 'page', it acts only on the items on the current page
NDS.Selection.clear(list) Clears every item, including items a filter hides and items with no checkbox. An item with a disabled checkbox keeps its state. Call it after a bulk action
NDS.Selection.getSelected(list) Returns the selected items, in page order
NDS.Selection.isSelected(item) Returns true when the item is selected. Export uses it to pick the selected rows
NDS.Selection.refresh(list) Updates one list’s states, select-all and counters, and adds the listeners if they are missing, so a list added after the page loads works. With no list, it updates every list. NDS.init.refresh() calls it that way
NDS.Selection.destroy() Removes the listeners. Use it when a single-page app tears the view down
Event Fired on Detail
nds:selection:change the list, and it bubbles { list, items, count, total }. items holds the selected items. It fires when the user checks a box or the select-all, when selectAll() or clear() runs, and when added or removed items change the count. It does not fire on page load
var list = document.getElementById('selection-services'); var removeButton = document.getElementById('remove-selected'); // Show the bulk action only while something is selected list.addEventListener('nds:selection:change', function (e) { removeButton.hidden = e.detail.count === 0; }); // Remove the selected items: the count drops, so the event hides the button again removeButton.addEventListener('click', function () { NDS.Selection.getSelected(list).forEach(function (item) { item.remove(); }); });

When you replace the list’s content, call NDS.init.refresh(container): it updates Selection and every other component on the list, such as Pagination. See Init. The full API is in the banner of _js/nds-selection.js.

Last Modified Date: 05/11/2026 - 12:51 AM
Was this page useful?
60% of users said Yes from 2843 Feedbacks