Home  >  Article  >  Web Front-end  >  A paging effect (code) implemented by a simple paging component (react)

A paging effect (code) implemented by a simple paging component (react)

不言
不言Original
2018-09-01 16:15:573658browse

The content of this article is about a paging effect (code) implemented by a simple paging component (react). It has a certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Simple paging component
...Due to work reasons, I wrote a piece of vue, and now I’m starting with jquery. I am a five-scum player. In order to consolidate what I have learned before React, I specifically use react to implement it, but I actually want to use jquery. github L6zt
The code effect is as shown in the figure:

A paging effect (code) implemented by a simple paging component (react)

## Idea:


Basic properties of the component: cur Current page number,
all total page number
space page display number 1 is the total number

Overall component status

1. Connected to the homepage, cur < ; space basically satisfies

A paging effect (code) implemented by a simple paging component (react)

2. Intermediate state, cur > space && cur A paging effect (code) implemented by a simple paging component (react)

3. Connect to the end, cur > all - space

A paging effect (code) implemented by a simple paging component (react)

##Basic operations of reactThe sub-component Pagination updates the state through props and has nothing to do with state.
show code

// 判断 是不是数字
const isNumber = (num) => {
    return typeof num === &#39;number&#39;;
}
class Pagination extends  React.Component {
    constructor (props) {
        super(props);
    }
    // 点击回调事件    
    handleClick (item) {
      // 父组件回调事件
      this.props.cb(item);
    }
    render () {
        let {cur, space, all} = this.props;
        let pgObj = [];
        // 给不同的元素 赋值class
        const checkClass = (role, active) => {
            const defaultClass = &#39;pg-span&#39;;
            if (active) {
                return    `${defaultClass} active`
            }
            switch (role) {
                case 0: {
                    return `${defaultClass}`
                }
                case 1: {
                    return `${defaultClass}`
                }
                default: {
                
                }
            }
        }
        // 初始检查
        if (all < space) {
            all =  space
        }
        if (cur <= 0) {
            cur = 0
        }
        if (cur >= all) {
        cur = all
        } 
        // 阶段判断
        if (cur < space) {
            if (space === all) {
                for (let i = 1; i <= space; i++) {
                    pgObj.push({
                        page: i,
                        role: 0,
                        key: i
                    })
                }
            } else {
                for (let i = 1; i <= space; i ++) {
                    pgObj.push({
                        page: i,
                        role: 0,
                        key: i
                    })
                }
                pgObj.push({
                    page: &#39;...&#39;,
                    role: 1,
                    key: &#39;next&#39;
                })
                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: &#39;...&#39;,
                role: 1,
                key: &#39;pre&#39;
            });
            for (let i = cur - odd; i <= cur + odd ; i ++) {
                pgObj.push({
                    page: i,
                    role: 1,
                    key: i
                })
            }
            pgObj.push({
                page: &#39;...&#39;,
                role: 1,
                key: &#39;next&#39;
            });
            pgObj.push({
                page: all,
                role: 1,
                key: all
            })
        } else {
            pgObj.push({
                page: 1,
                role: 0,
                key: 1
            });
            pgObj.push({
                page: &#39;...&#39;,
                role: 1,
                key: &#39;pre&#39;
            });
            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.page}
                        </span>))
                }
            </section>
        )
    }
    
}
class Root extends 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} = this.state;
    console.log(cur);
    return  (
      <p>
         <Pagination cur={cur} all={100} space={8} cb={this.handlePagination} />
      </p>
    )
  }
};
ReactDOM.render(
  <Root></Root>,
  document.getElementById(&#39;root&#39;)
);
Related recommendations:

Example of writing a paging component using react


Share a simple javascript paging component written by yourself_javascript skills

The above is the detailed content of A paging effect (code) implemented by a simple paging component (react). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn