How to programmatically reset searchtext
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:
You see that 777 is still visible when the tableRef.current.state is printed out the searchText is reset:
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?
Hello maybe you can use the following snippet:
tableRef.current.onSearchChange("")
Exito
OnsearchChanged is the trigger for the user to make use of a filter change not to trigger a change
+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.
@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?
It got renamed to onSearchChangeDebounce it seems like.
Is onSearchChangeDebounce documented ? I tried to use it but nothing happens. Sandbox
@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
searchText definitely not reacting to updates. Is this expected?
Anyone have a fix for this or know which commit is breaking onSearchChange and why it was changed?
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 : '',
...
}}
these lines solves the problem:
tableRef.current.dataManager.changeSearchText(searchText);
tableRef.current.setState({ searchText: searchText });
tableRef.current.setState(tableRef.current.dataManager.getRenderState());
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.
why was this labled as wontfix? it's still an issue
Thanks its works for me