Skip to content

Commit c487e82

Browse files
add filterFunc to model (#175)
* feat: add `FilterFunc` function * feat: add ability to hide columns * fix: removed `hidden` from branch * fix: revert non-modified files * fix: move per-column `filterFunc` to model level * fix: improve documentation for `filterFunc` * fix: pass filter input to `filterFunc` * fix: either use standard row-matching or filterFunc but not both * fix: updated docs
1 parent f9795db commit c487e82

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

table/filter.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ func (m Model) getFilteredRows(rows []Row) []Row {
1414
filteredRows := make([]Row, 0)
1515

1616
for _, row := range rows {
17-
if isRowMatched(m.columns, row, filterInputValue) {
18-
filteredRows = append(filteredRows, row)
17+
if m.filterFunc != nil {
18+
if m.filterFunc(row, filterInputValue) {
19+
filteredRows = append(filteredRows, row)
20+
}
21+
} else {
22+
if isRowMatched(m.columns, row, filterInputValue) {
23+
filteredRows = append(filteredRows, row)
24+
}
1925
}
2026
}
2127

table/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type Model struct {
7878
// Filter
7979
filtered bool
8080
filterTextInput textinput.Model
81+
filterFunc func(Row, string) bool
8182

8283
// For flex columns
8384
targetTotalWidth int
@@ -125,6 +126,7 @@ func New(columns []Column) Model {
125126
unselectedText: "[ ]",
126127

127128
filterTextInput: filterInput,
129+
filterFunc: nil,
128130
baseStyle: lipgloss.NewStyle().Align(lipgloss.Right),
129131

130132
paginationWrapping: true,

table/options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ func (m Model) WithFilterInputValue(value string) Model {
364364
return m
365365
}
366366

367+
// WithFilterFunc adds a filter function to the model. If the function returns
368+
// true, the row will be included in the filtered results. If the function
369+
// is nil, the function won't be used. The filter input is passed as the second
370+
// argument to the function.
371+
func (m Model) WithFilterFunc(shouldInclude func(row Row, filterInput string) bool) Model {
372+
m.filterFunc = shouldInclude
373+
374+
return m
375+
}
376+
367377
// WithFooterVisibility sets the visibility of the footer.
368378
func (m Model) WithFooterVisibility(visibility bool) Model {
369379
m.footerVisible = visibility

0 commit comments

Comments
 (0)