glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
613 stars 79 forks source link

Multiple search strings in global search? #222

Closed shahreyar-abeer closed 2 years ago

shahreyar-abeer commented 2 years ago

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. https://github.com/tannerlinsley/react-table/discussions/2247
glin commented 2 years ago

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:

sam-israel commented 1 year ago

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)