Collections (Checkboxes, Radios, Select)
Learn how to work with checkboxes, radio buttons, select elements, and advanced collection patterns in Aurelia forms.
Overview
Aurelia provides sophisticated support for collection-based form controls, going beyond simple arrays to support Sets, Maps, and custom collection types with optimal performance.
Checkboxes
Boolean Checkboxes
The simplest checkbox pattern binds to boolean properties:
export class PreferencesForm {
emailNotifications = false;
smsNotifications = true;
pushNotifications = false;
get hasValidNotificationPrefs(): boolean {
return this.emailNotifications || this.smsNotifications || this.pushNotifications;
}
}<form>
<fieldset>
<legend>Notification Preferences</legend>
<label>
<input type="checkbox" checked.bind="emailNotifications" />
Email notifications
</label>
<label>
<input type="checkbox" checked.bind="smsNotifications" />
SMS notifications
</label>
<label>
<input type="checkbox" checked.bind="pushNotifications" />
Push notifications
</label>
</fieldset>
<div if.bind="!hasValidNotificationPrefs" class="warning">
Please select at least one notification method.
</div>
</form>Key Points:
Use
checked.bindfor boolean checkboxesWorks with any boolean property
Great for independent on/off toggles
Array-Based Multi-Select
For multi-select scenarios, bind arrays to checkbox groups using model.bind:
How It Works:
model.bindtells Aurelia what value to add to the arraychecked.bindpoints to the array that holds selected valuesAurelia automatically adds/removes values when checkboxes are toggled
Use Cases:
Multi-select forms (select multiple skills, interests, tags)
Batch operations (select multiple items for deletion)
Filter selections (select multiple categories to filter by)
Set-Based Collections
For high-performance scenarios with frequent additions/removals, use Set collections:
Why Use Sets:
O(1) lookup performance with
.has()Efficient for large collections
Natural for unique value storage
Better performance for frequent add/remove operations
Map-Based Collections
For complex key-value selections, Maps provide the most flexibility:
When to Use Maps:
Nested selection scenarios (resource → actions)
Complex key-value relationships
Grouped permissions or settings
Multi-dimensional selections
Radio Buttons
Radio buttons are for single-selection from multiple options.
Basic Radio Buttons
Radio Buttons with Objects
Key Points:
Use same
nameattribute for all radios in a groupmodel.binddefines the value when selectedchecked.bindholds the currently selected valueUse
matcher.bindfor complex object comparison
Select Elements
Basic Select
Select with Objects
Select with Optgroups
Multi-Select
Performance Considerations
Choose the right collection type for your use case:
Array
General purpose, small-medium collections
Good
Set
Frequent additions/removals, uniqueness
Excellent (O(1) lookups)
Map
Key-value pairs, nested selections
Excellent (O(1) lookups)
Performance Tips:
Use Set for large collections with frequent changes
Implement efficient matcher functions for object comparison
Avoid creating new objects in templates—use computed properties
Consider virtualization for very large checkbox/radio lists
Matchers Explained
By default, Aurelia compares values with strict equality (===). That works for primitives and for objects that are the exact same instance. It does not work when your selected value and your option values are different instances that represent the same logical entity (for example, data reloaded from an API). A matcher lets you define what "equal" means so selections stay in sync.
Matchers tell Aurelia how to compare values:
When to use matchers:
Binding objects to checkboxes/radios
Working with Sets containing objects
Need custom equality logic
Comparing by properties other than reference
Common Patterns
Select All / Deselect All
Conditional Options
Related
Form Basics - Basic form inputs
Validation - Validate form inputs
Form Examples - Complete examples
List Rendering - Using repeat.for
Last updated
Was this helpful?