0

I have a json file containing an array with some objects called json.bc shown by fetch() request. I want to sort all the objects based on total field but the data will be sort in every page not all the pages.How can I sort them?

class App extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        library: null,
        perPage: 20,
        currentPage: 1,
        maxPage: null,
    }
}
componentDidMount() {
    fetch('/json.bc', {
        method: 'get',
    })
        .then(response => response.text())
        .then(text => {
            let Maindata = JSON.parse(text.replace(/\'/g, '"'))
            this.setState(state => ({
                ...state,
                data: Maindata
            }), () => {
                this.reorganiseLibrary()
            })
        }).catch(error => console.error(error))
}

reorganiseLibrary = () => {
    const { perPage, data } = this.state;
    let library = data;
    library = _.chunk(library, perPage);
    this.setState({
        library,
        currentPage: 1,
        maxPage: library.length === 0 ? 1 : library.length
    })
}
// Previous Page
previousPage = event => {
    this.handleClick(event)
    this.setState({
        currentPage: this.state.currentPage - 1
    })
}
// Next Page 
nextPage = event => {
    this.handleClick(event)
    this.setState({
        currentPage: this.state.currentPage + 1
    })
}

// handle per page
handlePerPage = (evt) =>
    this.setState({
        perPage: evt.target.value
    }, () => this.reorganiseLibrary());

// handle render of library
renderLibrary = () => {
    const { library, currentPage } = this.state;

    if (!library || (library && library.length === 0)) {
        return <div>NoResult</div>
    }
    return library[currentPage - 1].sort((a, b) => a.total - b.total).map((item, i) => (
        <div className="Item">
            {item.total}
        </div>
    ))
}

render() {
    const { library, currentPage, perPage, maxPage } = this.state;
    return (
        <div>
            {this.renderLibrary()}
            <ul id="page-numbers">
                <li className="nexprevPage">
                    {currentPage !== 1 && (
                        <button onClick={this.previousPage}><span className="fa-backward"></span></button>
                    )}
                </li>
                <li className="controlsPage activeCnt">{this.state.currentPage}</li>
                <li className="restControls">...</li>
                <li className="controlsPage">{this.state.maxPage}</li>
                <li className="nexprevPage">
                    {(currentPage < maxPage) && (
                        <button onClick={this.nextPage}><span className="fa-forward"></span></button>
                    )}
                </li>
            </ul>
        </div>
    )
}}
ReactDOM.render(<App />, document.getElementById('Result'))
5
  • Use this.state.data in your renderLibrary() instead of library[currentPage - 1] could solve your problem? Use it and get only needed page from sorted data. Commented May 4, 2019 at 8:04
  • @G_S It could not solve my problem , the data is not shown in pagination. Commented May 4, 2019 at 8:11
  • If I am not understanding it wrong, data is in state of your component. You can use it in your component to perform the needed pagination. Commented May 4, 2019 at 8:12
  • Sorry but can you show what you mean in my code? Commented May 4, 2019 at 8:15
  • What is the problem you are facing. You are already sorting the data per page in renderLibrary function. Commented May 4, 2019 at 8:43

1 Answer 1

0

The whole loaded data is saved in this.state.data (in componentDidMount).

setState callback calls then this.reorganiseLibrary() - this is the place where pagination occurs, result is stored in this.state.library and used for rendering.

To change sorting (whole set) you must sort data before pagination, in this.reorganiseLibrary().

Steps needed:

  • save sorting order in the state (like perPage);
  • provide event handler for order change - remember to use this.reorganiseLibrary() in setState callback (like in handlePerPage);
  • use ordering value for sorting in this.reorganiseLibrary() (before library = _.chunk(library, perPage);)
  • remove sort parts from renderLibrary()

Thats all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.