stone rock
stone rock

Reputation: 1953

How to slice JSON data in ReactJS?

I have made 2 components 1)Content.js 2)Pagination.js I want to display the content when user clicks on pagination component. There are total 16 objects per page. I have used .slice(0,16) method for page 1. Now I want to change the parameters passed in slice function based on page number eg:
.slice(0,16) --> for page 1 .slice(17,33) ---> for page 2 and so on till page 5 each page should display 16 objects per page and total 5 pages.

Content.js :

import React, { Component } from 'react';
import Matchinfo from './matchinfo';
import './content.css';

class Content extends Component {
    constructor(props){
        super(props);
        this.state = {
            matches:[],
            loading:true,
            callmatchinfo: false,
            matchid:''
        };
    }

    componentDidMount(){
        fetch('api/matches')
        .then(res => res.json())
        .then(res => {
      console.log(res)
      this.setState({
        matches:res.slice(0,16),     <----Parameters inside it must be changed for each page
        loading:false
      });
    })
    }

  viewstats(matchid){
    this.setState({
        callmatchinfo: true,
        matchid: matchid
    });
  }

  rendermatchinfo(){
    return <Matchinfo matchid={this.state.matchid} />
  }

    renderMatches() {
        return this.state.matches.map(match => {
            return (
                <div className="col-lg-3">
                    <div id="content">
                        <p className="match">MATCH {match.id}</p>
                        <h4>{match.team1}</h4>
                        <p>VS</p>
                        <h4>{match.team2}</h4>
                        <div className="winner">
                            <h3>WINNER</h3>
                            <h4>{match.winner}</h4>
                        </div>
                        <div className="stats">
                            <button type="button" onClick= {()=>{this.viewstats(match.id)}} className="btn btn-success">View Stats</button>
                        </div>
                    </div>
                </div>
            );
        })
    }

    render() {

        if (this.state.loading) {
            return <img src="https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif" />
        }
        else if(this.state.callmatchinfo){
        return <Matchinfo match_id={this.state.matchid} />
        }

    return (
      <div>
          <div className="row">
            {this.renderMatches()}
              </div>
          <div className="row">
            {this.state.callmatchinfo ? this.rendermatchinfo() : ''}
          </div>
      </div>
    );
  }
}

export default Content;

Pagination.js :

import React, { Component } from 'react';
import Content from './content';

class Pagination extends Component {

  render() {
    return (
      <div>
          <div className="container">                 
              <ul className="pagination">
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
              </ul>
          </div>

      </div>
    );
  }
}

export default Pagination;

For more clarity see the screenshot of page 1.This functionality must be extended for 5 pages.

enter image description here

enter image description here

Upvotes: 2

Views: 3732

Answers (1)

Danish
Danish

Reputation: 596

Import the Pagination component and attach an event like below :

import React, { Component } from 'react';
import Content from './content';

class Pagination extends Component {

  render() {
    return (
      <div>
          <div className="container">                 
              <ul className="pagination">
                <li onClick={this.props.onPageNext.bind(this, 1)} ><a href="#">1</a></li>
            <li onClick={this.props.onPageNext.bind(this, 2)} ><a href="#">2</a></li>
 <li onClick={this.props.onPageNext.bind(this, 3)} ><a href="#">3</a></li>
              </ul>
          </div>

      </div>
    );
  }
}

export default Pagination;

In Content.js :

...

return (
      <div>
          <div className="row">
            {this.renderMatches()}
              </div>
          <div className="row">
            {this.state.callmatchinfo ? this.rendermatchinfo() : ''}
          </div>
            <Pagination onPageNext={this.onPageNext} /> 

      </div>
    );

...

  onPageNext(nextPage) {
    // logic to set the setState method for pagination...
  }

Upvotes: 1

Related Questions