The Filter System is a core component of AFS that provides flexible and powerful filtering capabilities for DOM elements. It supports both single and multiple filter criteria, different filter modes (AND/OR), and filter groups.
- Installation
- Basic Usage
- Filter Modes
- Filter Groups
- API Reference
- Events
- Examples
- TypeScript
- Best Practices
import { Filter } from 'advanced-filter-system';
// As part of AFS
const afs = createAFS({
filterButtonSelector: '.btn-filter',
filterMode: 'OR'
});
// Standalone usage
const filter = new Filter(afs);
<!-- Filter Buttons -->
<button class="btn-filter" data-filter="category1">Category 1</button>
<button class="btn-filter" data-filter="category2">Category 2</button>
<!-- Filterable Items -->
<div class="filter-item" data-categories="category1">Item 1</div>
<div class="filter-item" data-categories="category1,category2">Item 2</div>
// Initialize with options
const afs = createAFS({
filterButtonSelector: '.btn-filter',
filterMode: 'OR',
activeClass: 'active',
hiddenClass: 'hidden'
});
// Manual filter control
afs.filter.addFilter('category1');
afs.filter.removeFilter('category2');
afs.filter.clearAllFilters();
Items match if they have any of the active filters.
afs.filter.setFilterMode('OR');
// or
afs.filter.setLogic(false);
Items must match all active filters.
afs.filter.setFilterMode('AND');
// or
afs.filter.setLogic(true);
Groups allow logical grouping of filters with their own operators.
// Add filter group
afs.filter.addFilterGroup('colors', {
filters: ['red', 'blue'],
operator: 'OR'
});
afs.filter.addFilterGroup('sizes', {
filters: ['small', 'medium'],
operator: 'AND'
});
// Set how groups interact
afs.filter.setGroupMode('AND');
// Remove group
afs.filter.removeFilterGroup('colors');
Add a new filter to the active filters.
afs.filter.addFilter('category1');
Remove a filter from the active filters.
afs.filter.removeFilter('category1');
Clear all active filters and reset to default state.
afs.filter.clearAllFilters();
Set the filter logic mode.
afs.filter.setFilterMode('AND');
Add a new filter group.
afs.filter.addFilterGroup('prices', {
filters: ['0-100', '101-500'],
operator: 'OR'
});
Get the current active filters.
const activeFilters = afs.filter.getActiveFilters();
activeFilters: Set<string>
- Currently active filtersfilterGroups: Map<string, FilterGroup>
- Active filter groupsfilterMode: 'AND' | 'OR'
- Current filter modegroupMode: 'AND' | 'OR'
- Current group mode
// Filter toggled
afs.on('filterToggled', (data) => {
console.log('Filter:', data.filter);
console.log('Active filters:', data.activeFilters);
});
// Filters applied
afs.on('filter', (data) => {
console.log('Visible items:', data.visibleItems);
console.log('Hidden items:', data.hiddenItems);
});
// Filters cleared
afs.on('filtersCleared', () => {
console.log('All filters cleared');
});
// Initialize
const afs = createAFS({
filterButtonSelector: '.btn-filter'
});
// Add event listeners
afs.on('filter', (data) => {
updateUI(data.visibleItems);
});
// Add filters programmatically
afs.filter.addFilter('category1');
afs.filter.addFilter('category2');
// Create price ranges
afs.filter.addFilterGroup('price', {
filters: ['0-100', '101-500', '501-1000'],
operator: 'OR'
});
// Create category filters
afs.filter.addFilterGroup('categories', {
filters: ['electronics', 'books'],
operator: 'AND'
});
// Set group interaction
afs.filter.setGroupMode('AND');
// Add filter button dynamically
const button = document.createElement('button');
button.className = 'btn-filter';
button.dataset.filter = 'newCategory';
afs.filter.addFilterButton(button);
// Remove filter button
afs.filter.removeFilterButton(button);
interface FilterOptions {
mode: 'AND' | 'OR';
activeClass: string;
hiddenClass: string;
}
interface FilterGroupOptions {
filters: string[];
operator: 'AND' | 'OR';
}
interface FilterEvent {
filter: string;
activeFilters: string[];
visibleItems: number;
hiddenItems: number;
}
-
Performance
// Use filter groups for better organization afs.filter.addFilterGroup('price', { filters: ['0-100', '101-500'], operator: 'OR' });
-
Error Handling
try { afs.filter.addFilter('category1'); } catch (error) { console.error('Filter error:', error); // Handle error appropriately }
-
State Management
// Save filter state const state = { activeFilters: Array.from(afs.filter.getActiveFilters()), groups: Array.from(afs.filter.getFilterGroups()) }; // Restore filter state state.activeFilters.forEach(filter => { afs.filter.addFilter(filter); });
-
URL Integration
// Update URL when filters change afs.on('filter', () => { afs.urlManager.updateURL(); });