【问题标题】:I could not set which elements would display on the page in which js pagination react我无法设置哪些元素将显示在 js 分页反应的页面上
【发布时间】:2019-05-30 02:43:10
【问题描述】:

我尝试进行分页,但没有成功。 我想我无法弄清楚json数据会出现多少页。

下面是我尝试做的示例的链接。

感谢那些感兴趣的人。

https://codesandbox.io/embed/kategori-detay-pagination-zpzdt

 handlePageChange(pageNumber) {
    console.log(`active page is ${pageNumber}`);
    this.setState({ activePage: pageNumber });
  }
 <Pagination
          prevPageText="prev"
          nextPageText="next"
          firstPageText="first"
          lastPageText="last"
          pageRangeDisplayed={10}
          activePage={this.state.activePage}
          itemsCountPerPage={10}
          totalItemsCount={totalPosts}
          pageRangeDisplayed={10}
          onChange={this.handlePageChange}
        />

【问题讨论】:

    标签: reactjs pagination


    【解决方案1】:

    你没有用正确的'this'绑定函数handlePageChange()

    constructor(props) {
        super(props);
        this.state = {
          CategorySlug: "",
          CategoryBrief: [],
          Posts: []
        };
    
        // Need to bind the function to proper context
        this.handlePageChange = this.handlePageChange.bind(this);
    }
    
    handlePageChange(pageNumber) {
        console.log(`active page is ${pageNumber}`);
    
        // Without binding, 'this' refers <Pagination /> instance
        // With binding, 'this' refers to current <PageCategoryDetail /> instance
        this.setState({ activePage: pageNumber });
    }
    

    或者,如果您愿意,您可以使用 at 较新的语法,那么您不必绑定 每个功能单独

    handlePageChange = (pageNumber) => {
        // Your code here
    }
    

    编辑

    要显示item,根据选中的页面,需要操作Posts数组,过滤出需要的item

    // You only filter by the selected Category, did not handle the paging
    {Posts.filter(b => b.CategoryName === CategorySlug).map(item => (
       <li key={item.PostID}>{item.Title}</li>
    ))}
    
    // You need further handle the pagination
    const POST_PER_PAGE = 5
    const pageOffset = this.state.activePage > 0 ? this.state.activePage - 1 : 0;
    const startIndex = pageOffset * POST_PER_PAGE;
    const endIndex = startIndex + POST_PER_PAGE;
    {Posts.filter(b => b.CategoryName === CategorySlug).slice(startIndex, endIndex).map(item => (
       <li key={item.PostID}>{item.Title}</li>
    ))}
    
    

    https://codesandbox.io/s/kategori-detay-pagination-f72px

    【讨论】:

    • 我认为问题是错误的当我点击分页部分的1我想显示5个项目时,我点击5个项目后是2。这部分我做不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2017-08-02
    • 2021-11-30
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多