简体   繁体   English

如何在我的table.js页面中包括分页?

[英]How can I include Pagination in my table.js page?

I am working on a project that fetches data from an API and displays it in the form of table. 我正在研究一个从API提取数据并以表格形式显示数据的项目。 The project is working fine. 该项目运行良好。 I am facing a problem with pagination. 我面临分页问题。 I want to display max 5 items on a page and do something that allows my application to create multiple pages based on the data that is fetched. 我想在一个页面上显示最多5个项目,并执行一些操作,使我的应用程序可以基于获取的数据创建多个页面。 this is my posts.js page. 这是我的posts.js页面。

import React from 'react'
import MyTable from './table'

export default class Posts extends React.Component {
    constructor(props) {
        super(props);

        this.columns = [
            {
                name: "ID",
                key: "id"
            }, {
                name: "Title",
                key: "title"
            }, {
                name: "Body",
                key: "body"
            }
        ];

        this.maxItems = 5; 
    };

    state = {
        pgNo: 0,
        table: [],
        isFetching: true

    };
    componentDidMount() {
        fetch("https://jsonplaceholder.typicode.com/posts")
            .then(response => response.json())
            .then(res => {
                this.setState({table: res, isFetching: false});

            });



        }



    render() {
        return this.state.isFetching
            ? (
                <div
                    className="loader"
                    style={{
                    marginLeft: "50%"
                }}>
                    <img src="/assets/index.svg"/>
                </div>
            )
            : (

                <MyTable pgNo ={this.state.pgNo}
                         maxItems = {this.maxItems}
                         columns={this.columns} 
                         data={this.state.table}
                         />

            )
    }

}

And here is my table.js page 这是我的table.js页面

import React from 'react'


export default class MyTable extends React.Component {

    constructor(props) {
        super(props);

    }


    createTable = () => {


        let tableHeader = <thead>
            <tr>
                {this.props.columns.map(column => {
                        return <th key={column.name}>
                            {column.name}
                        </th>
                    })}
            </tr>
        </thead>;

        let tableRows = [];
        for (let i = this.props.pgNo*this.props.maxItems ; i <(this.props.pgNo + 1)*this.props.maxItems-1; i++) {


                let row = <tr key={i}>
                    {this.props.columns.map(column => {
                            return <td key={column.key}>
                                {this.props.data[i][column.key]}
                            </td>
                        })}
                </tr>

                tableRows.push(row)
            }


        let tableBody = <tbody>{tableRows}</tbody>;
        return <table
            className="table table-bordered"
            style={{
            marginLeft: "33%",
            marginRight: "5%"
        }}>{tableHeader}{tableBody}</table>;
    }
    render() {
        return (
            <div className="col-md-6">
                <div className="container-fluid">
                    <div className="content">
                        {this.createTable()}

                    </div>
                </div>
            </div>
        )
    }

}

Also feel free to point out any mistakes i have made in this code.All the help is appreciated. 也可以随时指出我在此代码中犯的任何错误。感谢所有帮助。

This is the correct Code 这是正确的代码

users page. 用户页面。

class Table extends React.Component {
    constructor(props) {
        super(props);

        this.columns = [
            {
                name: "ID",
                key: "id"
            }, {
                name: "Name",
                key: "name"
            }, {
                name: "Username",
                key: "username"
            }, {
                name: "Email",
                key: "email"
            }, {
                name: "Website",
                key: "website"
            }
        ];

        this.maxItems = 5; 
    };

    state = {
        pgNo: 0,
        table: [],

        url:"/user"

    };
    componentDidMount() {
        this.props.fetchuser()
        console.log(this.props.user);
        console.log("columns",this.columns);
    }




    render() {
        console.log(this.props.user);
        return (
            this.props.user.length === 0 ?
                ( <div
                    className="loader"
                    style={{
                    marginLeft: "50%"
                }}>
                    <img src="/assets/index.svg"/>
                </div>) :
                (
                    <MyTable pgNo ={this.state.pgNo}
                         maxItems = {this.maxItems}
                         columns={this.columns} 
                         data={this.props.user}
                         url={this.state.url}/>
                )
        )
    }

}

Table page. 表格页面。

export default class MyTable extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            currentPage: this.props.pgNo,
            details: [],
            id: null
        }
        this.MaxPages = 0;

    }

    PrevButton() {

        if (this.state.currentPage === 0) {
            return (null);
        } else {
            return (
                <button
                    className="btn btn-primary"
                    type="button"
                    key={this.state.currentPage}
                    style={{
                    float: "left"
                }}
                    onClick=
                    { () => { this.setState({ currentPage: this.state.currentPage - 1 }) } }>
                    Previous Page
                </button>
            );
        }

    }

    NextButton() {
        if (this.state.currentPage === this.MaxPages - 1) {
            return (null);
        } else {
            return (

                <button
                    className="btn btn-primary"
                    style={{
                    float: "right"
                }}
                    key={this.props.pgNo}
                    onClick={() => {
                    this.setState({
                        currentPage: this.state.currentPage + 1
                    })
                }}>
                    Next Page
                </button >
            );
        }
    }

    AddButton() {
        return (
            <Link to={`${this.props.url}/addnew${this.props.url}`}>

                <button
                    style={{
                    float: "right"
                }}
                    className="btn btn-primary"><Faplus/>
                </button>

            </Link>
        );
    }

    createTable = () => {

        let tableHeader = <thead>
            <tr>
                {this.props.columns.map(column => {
                        return <th key={column.name}>
                            {column.name}
                        </th>
                    })}
            </tr>

        </thead>;
        this.state.number = this.state.number + 1;
        let tableRows = [];
        for (let i = this.state.currentPage * this.props.maxItems; (i < (this.state.currentPage + 1) * this.props.maxItems) && (i <= this.props.data.length); i++) {


            let row = <tr key={i}>
                {this
                    .props
                    .columns
                    .map(column => {

                        return (
                            <td key={column.key}>

                                <Link to={`${this.props.url}/details/${i + 1}`}>
                                    {this.props.data[i][column.key]}</Link>

                            </td>
                        )
                    })}

            </tr>

            tableRows.push(row)
        }
        for (let i = 0; i <= Math.ceil(this.props.data.length / this.props.maxItems); i++) {
            this.MaxPages = i;

        }

        let tableBody = <tbody className="name">{tableRows}</tbody>;
        return <table
            style={{
            marginTop: "10%",
            width: "100%"
        }}>{tableHeader}{tableBody}
        </table>;
    }

    render() {

        return (
            <div className="row">
                <div className="col-md-2 offset-md-10">{this.AddButton()}</div>
                <div className="col-md-8 offset-md-3 table ">

                    {this.createTable()}

                </div>
                <div className="col-md-8 offset-md-3">
                    {/* {this.FirstButton()} */}
                    {this.PrevButton()}

                    {this.NextButton()}
                    {/* {this.LastButton()} */}
                </div>
            </div>
        )
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM