Simple paging component (react) implementation of a paging effect (code)

Source: Internet
Author: User
This article brings you the content is about the simple paging component (react) implementation of a paging effect (code), there is a certain reference value, the need for friends can refer to, I hope to help you.

A simple paging component
... For work reasons, write a section of Vue, and now start with jquery. Oneself is the war five Slag contestant, in order to consolidate previously learned react, deliberately use react to realize, actually want to use jquery. GitHub L6zt
Code effects:

Ideas:
Component Basic Properties:
Cur current page number,
All total page numbers
Space page shows number +1 is the total quantity
Component Overall Status

1, connected with home page, cur < space basically meet

2, middle status, cur > Space && cur < all-space

3, connected with the end, cur > All-space

React Basic operation
subcomponents pagination update status through props, regardless of state.
Show Code

Determine if the number is const ISNUMBER = (num) and {return typeof num = = ' numbers ';}    Class Pagination extends React.component {constructor (props) {super (props);    }//Click Callback event Handleclick (item) {//Parent component Callback event THIS.PROPS.CB (item);        } render () {Let {cur, space, all} = This.props;        Let pgobj = [];            Assign a value of class const CHECKCLASS = (role, active) to different elements = {Const Defaultclass = ' Pg-span '; if (active) {return ' ${defaultclass} Active '} switch (role) {CAs E 0: {return ' ${defaultclass} '} Case 1: {return ' ${  Defaultclass} '} default: {}}}//        Initial check if (all < space) {all = space} if (cur <= 0) {cur = 0} if (cur >= all) {cur = alL}//stage to determine if (cur < space) {if (space = = = All) {for (Let i = 1; I & Lt;= space;                        i++) {Pgobj.push ({page:i, role:0, KEY:I})}} else {for (Let i = 1; I <= space; i + +) {Pgobj.push ({page:i, role:0, K                    EY:I})} pgobj.push ({page: ' ... '),                    Role:1, key: ' Next ') Pgobj.push ({page:all, role:0, Key:all})}} else if (cur >= space &            & cur <= all-space + 1) {Let odd = parseint (SPACE/2);          Pgobj.push ({page:1,      role:0, key:1});            Pgobj.push ({page: ' ... ', role:1, key: ' Pre '});                    for (Let i = cur-odd; I <= cur + odd; i + +) {Pgobj.push ({page:i,                 Role:1, key:i})} pgobj.push ({page: ' ... '),            Role:1, key: ' Next ');            Pgobj.push ({page:all, role:1, Key:all})} else {            Pgobj.push ({page:1, role:0, key:1});            Pgobj.push ({page: ' ... ', role:1, key: ' Pre '});                    for (Let i = All-space + 1; I <= all; i + +) {Pgobj.push ({page:i,          role:0,          KEY:I})};                        } return (<section> {pgobj.map (item =                               (<span Key={item.key} classname={checkclass (item.role, item.page = = = cur)} onclick={() = {This.handleclick (item)}} > {ITEM.PA GE} </span>)} </section>)}}class Root Exten      DS React.component {Constructor (props) {super (props);      This.state = {Cur:1};  This.handlepagination = This.handlePagination.bind (this);      } handlepagination (item) {const {page} = Item; if (Isnumber (page)) {this.setstate ({cur:page})}} render () {let {cur} = thi    S.state;    Console.log (cur); Return (<p> <pagination cur={cur} all={100} SPACE={8} cb={this.handlepagination}/> </p>)}}; Reactdom.render (<root></root>, document.getElementById (' Root '));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.