javascript - 如果我在“url”地址中有“perPage = 20”(获取 20)并且有 1000 个元素,如何检查数组长度?
问题描述
在params
我有:
const params = {
expand: 'project',
'per-page': 20
}
每页获取 20 个项目。
如果我删除了,'per-page': 20
我会得到 2000-3000 件物品。我不想在黑板上积累这么多。有什么方法可以在不下载它们的情况下确定 2000-3000 元素的长度。我需要totalItemsCount
在分页中指定所有元素。有没有办法确定这个长度,元素是2000-3000,而不是20?
this.state = {
todos: []
}
getTodos = (id) => {
const params = {
expand: 'project',
'per-page': 20,
}
axios({
url: `/api/v1/project/todos`,
method: "GET"
})
.then(res => {
this.setState({
todos: res.data
});
}).catch(error => {
console.log(error);
})
}
<Pagination
itemsCountPerPage={20}
totalItemsCount={this.state.todos.length}
/>
解决方案
您需要在服务器端添加一个方法来返回待办事项的总数。
例如可通过/api/v1/project/todos/count
或者将服务器的响应更改为包含总数量和 20 个选定 todos 的数组的对象:
{ length: 3000, todos: [first, second, ..., twentieth] }
否则,您的客户端无法知道有多少待办事项。
推荐阅读
- push-notification - 如何在更多设备、更多浏览器中处理同一用户的推送通知
- mongodb - 如何让 Trino (Presto) 更快地从 MongoDB 中获取大量数据?
- javascript - 如果用户会话不存在,是否可以隐藏 JS 和 CSS 文件
- python - 使用套接字的 HTTP 客户端,似乎只适用于谷歌
- python - 熊猫重复数据删除不起作用。多处理和权限错误
- react-native - 有什么方法可以让它“只听” react-native-push-notification 库
- python - Python 时间复杂度:For 循环
- node.js - 使用 npm start 启动我的反应项目会出错
- matlab - 需要帮助为连续过度松弛方法的不同容差值循环
- javascript - 为什么useRef解决了stale state的问题?