reactjs - 以编程方式刷新远程数据时处理表分页
问题描述
我正在尝试将相同的场景(https://github.com/mbrn/material-table/issues/383)与我自己的 API 端点一起使用,在我的响应结果中我没有 totalCount 和 page 值。没有它们,我的表格分页说NaN-NaN of undefined
我可以通过 result.length 处理 totalCount,处理完这个后,我可以看到分页为NaN-NaN of 38
(38 是数组项的计数),但我无法继续到下一页。这是我的代码:
data={query =>
new Promise((resolve, reject) => {
fetch('https://xxxxxxxxxxxxxxxxxx/dev/getcustomerinstructors', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': this.state.id_token
},
body: JSON.stringify({
"refresh_token": this.state.refresh_token,
"customer_id": this.state.customer_id,
})
})
.then(response => response.json())
.then(result => {
console.log(result); //array of objects
resolve({
data: result,
// page: 0, **//how to address this**
totalCount: result.length,
})
});
})
}
任何帮助将不胜感激。谢谢
解决方案
回答我自己的问题:
const page = (query.page + 1);
page: page - 1
并在响应后更新页面值就可以了。
data={query =>
new Promise((resolve, reject) => {
let url = 'https://xxxxxxxxxxxxxxxxxxxxxx/getcustomerinstructors';
fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': this.state.id_token
},
body: JSON.stringify({
"refresh_token": this.state.refresh_token,
"customer_id": this.state.customer_id,
"role": this.state.role
})
})
.then(response => response.json())
.then(result => {
var res = result.slice(query.page * query.pageSize, (query.page * query.pageSize) + query.pageSize);
resolve({
data: res,
page: query.page,
totalCount: result.length,
})
})
})
}
推荐阅读
- python - Python从输入文件中绘制数据
- python - SciPy stats lognormal distribution 获得具有给定对数正态分布均值和对数正态分布几何标准差的pdf
- json - 如何将 json 参数变量编码为一个休息尖锐的请求体
- gatling - 在 Gatling Scala 中将提取的数据从会话写入 CSV
- powershell - powershell:倒数计时器
- javascript - 为什么我在未构建时出现错误,而在构建时却没有?
- c - 在 C 中,对 void* 变量进行数学运算的最有效方法是什么
- c++ - 如何在 Mediapipe 中更改或设置“配置选项”?
- swift - 迅速。您如何计算从 tvOS 遥控器滑动的速度?
- javascript - Immer Produce 不会导致 Redux 重新渲染。在 Reducer 中修改拔模和基本状态