[英]How can I display data from a server in a material-table?
I use React js on the client side and Node js on the server side and I'm new.我在客户端使用 React js,在服务器端使用 Node js,我是新手。 On the client side, I use a button and a table (I use the material-table component)
在客户端,我使用了一个按钮和一个表格(我使用了 material-table 组件)
export default function UserProfile() {
const handleClick = () => {
return axios({
method: "post",
url: "/searchResult",
data: { data }
});
};
return (
<div className="App">
<button onClick={handleClick}> search <button/>
<RemoteData />
</div>
);
}
And RemoteData...和远程数据...
import MaterialTable from 'material-table';
class RemoteData extends React.Component {
render() {
return (
<MaterialTable
title=""
columns={[
{ title: 'Id', field: 'id' },
]}
data={query =>
new Promise((resolve, reject) => {
let url = '/searchResult1'
fetch(url)
.then(res => res.json())
.then(result => {
resolve({
data: result.data,
})
})
})
}
/>
)
}
}
export default RemoteData;
Clicking the button sends the data from the client to the server and is processed.(To save your time, I write part of the code).单击按钮将数据从客户端发送到服务器并进行处理。(为了节省您的时间,我编写了部分代码)。
And Server side...和服务器端...
method.post('/searchResult1', searchResult1);
searchResult1: (req, res) => {
let query = "SELECT id FROM `information` WHERE 1=1"
db.query(query, (err, result) => {
if (err) {
res.redirect('/');
}
console.log(result)
})
}
Here we display the 'result' without any problems.在这里,我们毫无问题地显示“结果”。 My question is: how can I update and display the 'result' in the material-table with each click of the button?
我的问题是:如何在每次单击按钮时更新和显示材料表中的“结果”?
In material-table
documentation you have a section called Remote Data.在
material-table
文档中,您有一个名为 Remote Data 的部分。 There is an example with a Refresh button material-ui-table-remote有一个带有刷新按钮material-ui-table-remote的示例
The code that may interest you:您可能感兴趣的代码:
class RefreshData extends React.Component {
constructor(props) {
super(props);
this.tableRef = React.createRef();
}
render() {
return (
<MaterialTable
title="Refresh Data Preview"
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,
})
})
})
}
actions={[
{
icon: 'refresh',
tooltip: 'Refresh Data',
isFreeAction: true,
onClick: () => this.tableRef.current && this.tableRef.current.onQueryChange(), // This line
}
]}
/>
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.