Skip to content

Commit c725470

Browse files
committed
feat: Add multi-column search with OR and AND logic
1 parent 031ffdc commit c725470

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

SimpleDataGrid/Pagination/PagedCollection.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,67 @@ public void SetSearchAll(IEnumerable<Func<T, string>> selectors, string? term, b
216216
_debounceTimer?.Dispose();
217217
_debounceTimer = new System.Threading.Timer(_ =>
218218
{
219-
System.Windows.Application.Current.Dispatcher.Invoke(ApplyFiltering);
219+
System.Windows.Application.Current.Dispatcher.Invoke(ApplyFilteringAll);
220220
IsSearching = false;
221221
}, null, debounceMilliseconds, System.Threading.Timeout.Infinite);
222222
}
223223
else
224224
{
225-
ApplyFiltering();
225+
ApplyFilteringAll();
226226
SearchChanged?.Invoke(this, EventArgs.Empty);
227227
}
228228
}
229229

230+
private void ApplyFilteringAll()
231+
{
232+
IEnumerable<T> query = _source;
233+
234+
foreach (var filter in _filters.Values)
235+
{
236+
query = query.Where(filter);
237+
}
238+
239+
if (!string.IsNullOrWhiteSpace(_searchTerm) && _searchSelectors.Any())
240+
{
241+
var term = _searchTerm;
242+
Func<string, bool> matches;
243+
244+
if (_useWildcards)
245+
{
246+
var regex = new Regex(WildcardToRegex(term), RegexOptions.IgnoreCase);
247+
matches = s => regex.IsMatch(s);
248+
}
249+
else
250+
{
251+
matches = s => s.Contains(term, StringComparison.OrdinalIgnoreCase);
252+
}
253+
254+
// AND logic for multi-column search
255+
query = query.Where(item => _searchSelectors.All(selector => matches(selector(item) ?? string.Empty)));
256+
}
257+
258+
if (_sorts.Count > 0)
259+
{
260+
IOrderedEnumerable<T>? orderedQuery = null;
261+
foreach (var (selector, ascending) in _sorts)
262+
{
263+
if (orderedQuery == null)
264+
{
265+
orderedQuery = ascending ? query.OrderBy(selector) : query.OrderByDescending(selector);
266+
}
267+
else
268+
{
269+
orderedQuery = ascending ? orderedQuery.ThenBy(selector) : orderedQuery.ThenByDescending(selector);
270+
}
271+
}
272+
query = orderedQuery ?? query;
273+
}
274+
275+
_filtered = [.. query];
276+
_currentPage = 0;
277+
RaiseAllChanged();
278+
}
279+
230280
/// <summary>
231281
/// Clears the search criteria from the collection.
232282
/// </summary>

0 commit comments

Comments
 (0)