Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filterFunc to model #175

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 8 additions & 2 deletions table/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ func (m Model) getFilteredRows(rows []Row) []Row {
filteredRows := make([]Row, 0)

for _, row := range rows {
if isRowMatched(m.columns, row, filterInputValue) {
filteredRows = append(filteredRows, row)
if m.filterFunc != nil {
if m.filterFunc(row, filterInputValue) {
filteredRows = append(filteredRows, row)
}
} else {
if isRowMatched(m.columns, row, filterInputValue) {
filteredRows = append(filteredRows, row)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions table/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Model struct {
// Filter
filtered bool
filterTextInput textinput.Model
filterFunc func(Row, string) bool

// For flex columns
targetTotalWidth int
Expand Down Expand Up @@ -110,6 +111,7 @@ func New(columns []Column) Model {
unselectedText: "[ ]",

filterTextInput: filterInput,
filterFunc: nil,
baseStyle: lipgloss.NewStyle().Align(lipgloss.Right),

paginationWrapping: true,
Expand Down
10 changes: 10 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ func (m Model) WithFilterInputValue(value string) Model {
return m
}

// WithFilterFunc adds a filter function to the model. If the function returns
// true, the row will be included in the filtered results. If the function
// is nil, the function won't be used. The filter input is passed as the second
// argument to the function.
func (m Model) WithFilterFunc(shouldInclude func(row Row, filterInput string) bool) Model {
m.filterFunc = shouldInclude

return m
}

// WithFooterVisibility sets the visibility of the footer.
func (m Model) WithFooterVisibility(visibility bool) Model {
m.footerVisible = visibility
Expand Down
Loading