|
| 1 | +# Accent-Insensitive Search |
| 2 | + |
| 3 | +This feature allows DataTables to perform accent-insensitive searches, which is particularly useful for Portuguese and other languages that use accented characters. |
| 4 | + |
| 5 | +## Problem |
| 6 | + |
| 7 | +Users often don't type accents when searching but expect to find results with accented characters. For example: |
| 8 | +- Searching for "simoes" should find "Simões" |
| 9 | +- Searching for "joao" should find "João" |
| 10 | +- Searching for "sao paulo" should find "São Paulo" |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +To enable accent-insensitive search, update your `config/datatables.php` file: |
| 15 | + |
| 16 | +```php |
| 17 | +return [ |
| 18 | + 'search' => [ |
| 19 | + 'ignore_accents' => true, // Enable accent-insensitive search |
| 20 | + // ... other search options |
| 21 | + ], |
| 22 | + // ... other configurations |
| 23 | +]; |
| 24 | +``` |
| 25 | + |
| 26 | +## Supported Characters |
| 27 | + |
| 28 | +This feature currently supports Portuguese Brazilian accents: |
| 29 | + |
| 30 | +| Accented Characters | Base Character | |
| 31 | +|-------------------|----------------| |
| 32 | +| Ã/ã/Á/á/À/à/Â/â | a | |
| 33 | +| É/é/Ê/ê | e | |
| 34 | +| Í/í | i | |
| 35 | +| Ó/ó/Ô/ô/Õ/õ | o | |
| 36 | +| Ú/ú | u | |
| 37 | +| Ç/ç | c | |
| 38 | + |
| 39 | +## How It Works |
| 40 | + |
| 41 | +When `ignore_accents` is enabled: |
| 42 | + |
| 43 | +1. **For Collection DataTables**: Both the search term and the data values are normalized to remove accents before comparison |
| 44 | +2. **For Query/Eloquent DataTables**: Database-specific functions are used to normalize characters in SQL queries |
| 45 | + |
| 46 | +### Database Support |
| 47 | + |
| 48 | +- **MySQL**: Uses cascaded `REPLACE()` functions |
| 49 | +- **PostgreSQL**: Uses `UNACCENT()` extension if available, falls back to `REPLACE()` |
| 50 | +- **SQLite**: Uses cascaded `REPLACE()` functions |
| 51 | +- **SQL Server**: Uses cascaded `REPLACE()` functions |
| 52 | + |
| 53 | +## Examples |
| 54 | + |
| 55 | +### Basic Usage |
| 56 | + |
| 57 | +```php |
| 58 | +use DataTables; |
| 59 | + |
| 60 | +public function getUsersData() |
| 61 | +{ |
| 62 | + return DataTables::of(User::query()) |
| 63 | + ->make(true); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +With `ignore_accents => true` in config: |
| 68 | +- Searching "simoes" will match "Simões" |
| 69 | +- Searching "jose" will match "José" |
| 70 | +- Searching "coracao" will match "Coração" |
| 71 | + |
| 72 | +### Collection Example |
| 73 | + |
| 74 | +```php |
| 75 | +$users = collect([ |
| 76 | + ['name' => 'João Silva'], |
| 77 | + ['name' => 'María González'], |
| 78 | + ['name' => 'José Santos'] |
| 79 | +]); |
| 80 | + |
| 81 | +return DataTables::of($users)->make(true); |
| 82 | +``` |
| 83 | + |
| 84 | +With accent-insensitive search enabled: |
| 85 | +- Searching "joao" will find "João Silva" |
| 86 | +- Searching "jose" will find "José Santos" |
| 87 | + |
| 88 | +## Performance Considerations |
| 89 | + |
| 90 | +- **Collection DataTables**: Minimal impact as normalization is done in PHP |
| 91 | +- **Query DataTables**: May have slight performance impact due to database function calls |
| 92 | +- Consider adding database indexes on frequently searched columns |
| 93 | +- The feature can be toggled per DataTable instance if needed |
| 94 | + |
| 95 | +## Extending Support |
| 96 | + |
| 97 | +To add support for other languages/accents, modify the `Helper::normalizeAccents()` method in `src/Utilities/Helper.php`: |
| 98 | + |
| 99 | +```php |
| 100 | +public static function normalizeAccents(string $value): string |
| 101 | +{ |
| 102 | + $map = [ |
| 103 | + // Portuguese |
| 104 | + 'Ã' => 'a', 'ã' => 'a', 'Á' => 'a', 'á' => 'a', |
| 105 | + // Add more mappings for other languages |
| 106 | + 'Ñ' => 'n', 'ñ' => 'n', // Spanish |
| 107 | + 'Ü' => 'u', 'ü' => 'u', // German |
| 108 | + // ... more mappings |
| 109 | + ]; |
| 110 | + return strtr($value, $map); |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Testing |
| 115 | + |
| 116 | +The feature includes comprehensive unit tests. To run them: |
| 117 | + |
| 118 | +```bash |
| 119 | +./vendor/bin/phpunit tests/Unit/HelperTest.php --filter test_normalize_accents |
| 120 | +``` |
| 121 | + |
| 122 | +## Backward Compatibility |
| 123 | + |
| 124 | +This feature is fully backward compatible: |
| 125 | +- Default configuration has `ignore_accents => false` |
| 126 | +- Existing applications continue to work unchanged |
| 127 | +- No breaking changes to existing APIs |
0 commit comments