-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
How to refresh remote data programmatically #383
Comments
Hi @billBeOK , Your requirement makes sense. I resolved it with tableRef with new version 1.28.0 import { Grid, MuiThemeProvider } from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core/styles';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import MaterialTable from './material-table';
class App extends Component {
tableRef = React.createRef();
render() {
return (
<div style={{ maxWidth: '100%', direction }}>
<MaterialTable
tableRef={this.tableRef}
columns={[
{
title: 'Avatar',
field: 'avatar',
render: rowData => (
<img
style={{ height: 36, borderRadius: '50%' }}
src={rowData.avatar}
/>
),
},
{ title: 'Id', field: 'id' },
{ title: 'First Name', field: 'first_name' },
{ title: 'Last Name', field: 'last_name' },
]}
data={query =>
new Promise((resolve, reject) => {
let url = 'https://reqres.in/api/users?';
url += 'per_page=' + query.pageSize;
url += '&page=' + (query.page + 1);
fetch(url)
.then(response => response.json())
.then(result => {
resolve({
data: result.data,
page: result.page - 1,
totalCount: result.total,
})
});
})
}
title="Remote Data Example"
/>
<button
onClick={() => {
this.tableRef.current.onQueryChange();
}}
>
ok
</button>
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
module.hot.accept(); |
it's work like a charm ! Bravo |
Help me please I am getting this |
I also had the error initially and then realized I missed |
Is there a way to change query before refreshing data? |
In addition to having the search trigger on pressing the Enter key, I also added a "Clear" button with
|
the ref and this.tableRef.current.onQueryChange() saved my life bro! It works with functional component too using useRef! |
Could you please share a functional component example? |
using mbrn's previous code but with hooks: import { Grid, MuiThemeProvider } from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core/styles';
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import MaterialTable from './material-table';
const App = () => {
tableRef = useRef();
return (
<div style={{ maxWidth: '100%', direction }}>
<MaterialTable
tableRef={tableRef}
columns={[
{
title: 'Avatar',
field: 'avatar',
render: rowData => (
<img
style={{ height: 36, borderRadius: '50%' }}
src={rowData.avatar}
/>
),
},
{ title: 'Id', field: 'id' },
{ title: 'First Name', field: 'first_name' },
{ title: 'Last Name', field: 'last_name' },
]}
data={query =>
new Promise((resolve, reject) => {
let url = 'https://reqres.in/api/users?';
url += 'per_page=' + query.pageSize;
url += '&page=' + (query.page + 1);
fetch(url)
.then(response => response.json())
.then(result => {
resolve({
data: result.data,
page: result.page - 1,
totalCount: result.total,
})
});
})
}
title="Remote Data Example"
/>
<button
onClick={() => tableRef.current.onQueryChange()}
>
ok
</button>
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('app')
); A quick edit, but I think you get the point. |
When, I use the tableRef.current.OnQueryChange(), it throws an error saying _this.props.data is not a function. Can Someone Help on it Here's a screenshot |
Post your code? |
Sry, I forgot in hurry const colHeads = columns.map((cl)=>{
The data is displayed properly, But when I try to use state for data and use setState(), it causes another error so, I opted for this approach. |
It's hard to follow what your doing, but it seems like you don't need to use data in this manner in the first place. If you aren't dealing directly with remote data like in the previous example, you should fix your state problem. Good luck! |
Really Thanks, for the quick reply. I'll go with it then! |
Just a sidenote. If you want to trigger the data reload on the table from one of your action button handlers which holds some async functions, and you have a problem like your reference is null, than the following might help you: // Example is for a functional component
const yourRef = React.createRef()
// ...
tableRef: {yourRef},
actions: {[
rowData => ({
// ...
onClick: (event, rowData) => {
const scopedRef = yourRef.current
anAsyncFunction(/*...*/).then(() => scopedRef.onQueryChange())
}
})
]}
// ... |
For any one who is looking for doing the same with their custom API you can check this |
I'm trying to add data from one table to another on doubleclick. everything is working fine but when I try to refresh the other table using _this.props.data is not a function. ... I'm tired of dealing with this problem. I this is a bug or I missed something. This is the code in material-table.js line 342: |
Help me pls,
I have bind data to MaterialTable with remote feature without problems.
But now i want to be able to refresh this data with a trigger. I don't know if it is possible?
I know how to do it when data is an array. (with state)
but when data is a promise ....
Thks,
The text was updated successfully, but these errors were encountered: