Skip to content

Commit d9ae185

Browse files
committed
table: support filtering rows using FilterBy
1 parent 66db774 commit d9ae185

File tree

9 files changed

+919
-22
lines changed

9 files changed

+919
-22
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[![Coverage Status](https://coveralls.io/repos/github/jedib0t/go-pretty/badge.svg?branch=main)](https://coveralls.io/github/jedib0t/go-pretty?branch=main)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/jedib0t/go-pretty/v6)](https://goreportcard.com/report/github.com/jedib0t/go-pretty/v6)
77
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jedib0t_go-pretty&metric=alert_status)](https://sonarcloud.io/dashboard?id=jedib0t_go-pretty)
8-
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
98

109
Utilities to prettify console output of tables, lists, progress bars, text, and more
1110
with a heavy emphasis on customization and flexibility.

table/EXAMPLES.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,77 @@ to get:
310310

311311
---
312312

313+
<details>
314+
<summary><strong>Filtering</strong></summary>
315+
316+
Filtering can be done on one or more columns. All filters are applied with AND logic (all must match).
317+
Filters are applied before sorting.
318+
319+
```golang
320+
t.FilterBy([]table.FilterBy{
321+
{Name: "Salary", Operator: table.GreaterThan, Value: 2000},
322+
{Name: "First Name", Operator: table.Contains, Value: "on"},
323+
})
324+
```
325+
326+
The `Operator` field in `FilterBy` supports various filtering operators:
327+
- `Equal` / `NotEqual` - Exact match
328+
- `GreaterThan` / `GreaterThanOrEqual` - Numeric comparisons
329+
- `LessThan` / `LessThanOrEqual` - Numeric comparisons
330+
- `Contains` / `NotContains` - String search
331+
- `StartsWith` / `EndsWith` - String prefix/suffix matching
332+
- `RegexMatch` / `RegexNotMatch` - Regular expression matching
333+
334+
You can make string comparisons case-insensitive by setting `IgnoreCase: true`:
335+
```golang
336+
t.FilterBy([]table.FilterBy{
337+
{Name: "First Name", Operator: table.Equal, Value: "JON", IgnoreCase: true},
338+
})
339+
```
340+
341+
For advanced filtering requirements, you can provide a custom filter function:
342+
```golang
343+
t.FilterBy([]table.FilterBy{
344+
{
345+
Number: 2,
346+
CustomFilter: func(cellValue string) bool {
347+
// Custom logic: include rows where first name length > 3
348+
return len(cellValue) > 3
349+
},
350+
},
351+
})
352+
```
353+
354+
Example: Filter by salary and name
355+
```golang
356+
t := table.NewWriter()
357+
t.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
358+
t.AppendRows([]table.Row{
359+
{1, "Arya", "Stark", 3000},
360+
{20, "Jon", "Snow", 2000},
361+
{300, "Tyrion", "Lannister", 5000},
362+
{400, "Sansa", "Stark", 2500},
363+
})
364+
t.FilterBy([]table.FilterBy{
365+
{Number: 4, Operator: table.GreaterThan, Value: 2000},
366+
{Number: 3, Operator: table.Contains, Value: "Stark"},
367+
})
368+
t.Render()
369+
```
370+
to get:
371+
```
372+
+-----+------------+-----------+--------+
373+
| # | FIRST NAME | LAST NAME | SALARY |
374+
+-----+------------+-----------+--------+
375+
| 1 | Arya | Stark | 3000 |
376+
| 400 | Sansa | Stark | 2500 |
377+
+-----+------------+-----------+--------+
378+
```
379+
380+
</details>
381+
382+
---
383+
313384
<details>
314385
<summary><strong>Sorting</strong></summary>
315386

table/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ If you want very specific examples, look at the [EXAMPLES.md](EXAMPLES.md) file.
7676

7777
### Sorting & Filtering
7878

79-
- Sort by one or more Columns (`SortBy`)
79+
- **Sorting**
80+
- Sort by one or more Columns (`SortBy`)
8081
- Multiple column sorting support
81-
- Various sort modes: alphabetical, numeric, alphanumeric, numeric-alpha
82+
- Various sort modes: Alphabetical, Numeric, Alpha-numeric, Numeric-alpha
8283
- Case-insensitive sorting option (`IgnoreCase`)
8384
- Custom sorting functions (`CustomLess`) for advanced sorting logic
85+
- **Filtering**
86+
- Filter by one or more Columns (`FilterBy`)
87+
- Multiple filters with AND logic (all must match)
88+
- Various filter operators: Equal, NotEqual, GreaterThan, LessThan, Contains, StartsWith, EndsWith, RegexMatch
89+
- Case-insensitive filtering option (`IgnoreCase`)
90+
- Custom filter functions (`CustomFilter`) for advanced filtering logic
91+
- Filters are applied before sorting
8492
- Suppress/hide columns with no content (`SuppressEmptyColumns`)
8593
- Hide specific columns (`ColumnConfig.Hidden`)
8694
- Suppress trailing spaces in the last column (`SuppressTrailingSpaces`)

0 commit comments

Comments
 (0)