material-table icon indicating copy to clipboard operation
material-table copied to clipboard

How to programmatically reset searchtext

Open LeanderCoevoet opened this issue 6 years ago • 15 comments

I am trying to reset my searchText programmatically I can reset the searchText in the state op the table with a tableRef but in the searchfield the searchtext is still visible. I reset the searchText with the following code:

        if (tableRef.current){
            tableRef.current.setState({ searchText: "" });
            tableRef.current.dataManager.changeSearchText("");
            console.log(tableRef.current);
        }

When it is executed the data in table is good but the searchText is still visible and this will give some confusion. Picture of the table after it was reset programmatically: image You see that 777 is still visible when the tableRef.current.state is printed out the searchText is reset: image I have also looked in the code of the reset action of the iconButton and the same logic is used in the onSearchChange function of the m-table-toolbar. I am on the version v.1.63.X Why is the text not removed from the field?

LeanderCoevoet avatar Aug 11 '20 09:08 LeanderCoevoet

Hello maybe you can use the following snippet:

tableRef.current.onSearchChange("")

Exito

amatiasdev avatar Aug 20 '20 16:08 amatiasdev

OnsearchChanged is the trigger for the user to make use of a filter change not to trigger a change

LeanderCoevoet avatar Aug 20 '20 17:08 LeanderCoevoet

+1. We also need this. We would also like to pass the searchText as a prop so that we load from outside query. How can we set searchtext programmatically so the table renders after that? Is this possible? We are unable to pass new values to searchText programmatically.

jinman avatar Sep 22 '20 22:09 jinman

This works for me.

SandBox

amatiasdev avatar Sep 22 '20 23:09 amatiasdev

@rheazyk thanks! but for some reason, we are getting onSearchChange is not a function in the latest version. Does this work in your latest version as well?

jinman avatar Sep 23 '20 16:09 jinman

It got renamed to onSearchChangeDebounce it seems like.

Domino987 avatar Oct 01 '20 12:10 Domino987

Is onSearchChangeDebounce documented ? I tried to use it but nothing happens. Sandbox

emanuelef avatar Nov 08 '20 21:11 emanuelef

@rheazyk thanks! but for some reason, we are getting onSearchChange is not a function in the latest version. Does this work in your latest version as well?

I have tried this on my view and found that setting the text directly (as show in @rheazyk's sandbox) does work as far as applying the filter, but the text value in the search box itself does not render the search text.

I am using version -- 1.67.0

freddymercury avatar Dec 18 '20 00:12 freddymercury

searchText definitely not reacting to updates. Is this expected?

brymon68 avatar Jan 06 '21 00:01 brymon68

Anyone have a fix for this or know which commit is breaking onSearchChange and why it was changed?

lnorton89 avatar Feb 25 '21 22:02 lnorton89

A workaround is to programmatically generate a mouse click event on the 'Clear Search' button of that text field.

The following code works for me.

  const [searchText, setSearchText] = useState('')

  const click = (node) => {
    var evt = new MouseEvent('click', {
      view: window,
      bubbles: true,
      cancelable: true,
    })
    node.dispatchEvent(evt)
  }
  
  const handleClearAll = (event) => {
    ...
    const buttons = document.getElementsByTagName('button')
    for (var i = 0; i < buttons.length; i += 1) {
      if (buttons[i].getAttribute('aria-label') === 'Clear Search') {
        setSearchText('')
        click(buttons[i])
        break
      }
    }
    ...
  }

  options={{
    searchText: searchText ? searchText : '',
    ...
  }}

sx20 avatar May 06 '21 17:05 sx20

these lines solves the problem:

tableRef.current.dataManager.changeSearchText(searchText); tableRef.current.setState({ searchText: searchText }); tableRef.current.setState(tableRef.current.dataManager.getRenderState());

Sandbox

hbarna55 avatar Jul 21 '21 11:07 hbarna55

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Oct 20 '21 02:10 stale[bot]

why was this labled as wontfix? it's still an issue

nosajoahs avatar Jan 18 '22 23:01 nosajoahs

Thanks its works for me

r0binxp avatar Mar 30 '22 20:03 r0binxp