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

Multiple search strings in global search? #222

Closed
shahreyar-abeer opened this issue Jan 11, 2022 · 2 comments
Closed

Multiple search strings in global search? #222

shahreyar-abeer opened this issue Jan 11, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@shahreyar-abeer
Copy link

shahreyar-abeer commented Jan 11, 2022

Is there a way to use multiple search strings in global search with an OR functionality?

For example,
Search input: 'this that'
Should match all instances of 'this' OR 'that'

I see a couple of solutions in the original package, but having a hard time integrating it with R

  1. https://codesandbox.io/s/3x51yzollq?file=/index.js
  2. Is it possible to filter one column by multiple values TanStack/table#2247
@shahreyar-abeer shahreyar-abeer changed the title Multiple search string in global search? Multiple search strings in global search? Jan 11, 2022
@glin glin added the enhancement New feature or request label Feb 20, 2022
@glin
Copy link
Owner

glin commented May 14, 2022

This is now possible with custom search methods in the latest development version (v0.2.3.9000):

  • Column filtering and table searching can now be customized. Learn more in the Custom Filtering guide.
    • reactable() gains a searchMethod argument to use a custom JavaScript function for global table searching (#222).
    • colDef() gains a filterMethod argument to use a custom JavaScript function for column filtering (#9, #90, #145).
    • colDef() gains a filterInput argument to render a custom filter input for column filtering (#9).

There's no specific example of using multiple search strings, but there are a couple examples that might help with writing your own search method:

@glin glin closed this as completed May 14, 2022
@sam-israel
Copy link

Here is a JS function I wrote for OR functionality in global search. It searches (case sensitive) for either one of the terms (separated by a space)


myFilterMethod <- JS('function(rows, columnIds, filterValue) {
  
  // pattern defines a RegEx text based on the users input. In this users input,  all occurences of spacebar are replaced by an OR sign. 
//This is done so that if at least one his keywords is true, the row is displayed
  // However, if the expression ends with an OR sign, the OR sign should be removed. If this is not done, 
// then expressions ending in a spacebar will end in an OR sign and will always give true. This is what the second replace does.
  
  const pattern = new RegExp(filterValue.replace(/ /g, "|").replace(/\\|$/, ""))
  
  
    return rows.filter(function(row) {
      return columnIds.some(function(columnId) {
          return pattern.test(row.values[columnId]) 
// Use the pattern defined above to test the rows and return those that pass the pattern
      })
  })
}')

  reactable(
    data, searchable=TRUE, 
    searchMethod=myFilterMethod)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants