首页 > 解决方案 > 如果我在“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}
  />   

标签: javascriptreactjspagination

解决方案


您需要在服务器端添加一个方法来返回待办事项的总数。
例如可通过/api/v1/project/todos/count

或者将服务器的响应更改为包含总数量和 20 个选定 todos 的数组的对象: { length: 3000, todos: [first, second, ..., twentieth] }

否则,您的客户端无法知道有多少待办事项。


推荐阅读