아마 다들 순수 서비스 버전, 순수 jsWeb 보드 버전, Angular 버전 등 다양한 버전의 페이징 툴바를 작성해 보셨을 텐데요, 이 지극히 기본적인 기능이 여러 곳에서 사용되기 때문에 다음과 같은 버전을 드립니다. 먼저 ReactJS로 구현된 효과를 살펴보세요.
이 구성 요소에는 패키징을 위해 NodeJS와 Webpack을 결합하는 것이 가장 좋습니다. webpack --display-error-details --config webpack.config.js
이 React 버전의 페이징 구성 요소는 redux와 함께 사용하는 것이 더 편리합니다. UI = Fn(State)
기본 프로세스는 다음과 같습니다. 사용자 상호 작용->Action->Reduce->Store->UIRender
위의 기본 지식을 이해하는 것이 매우 필요하지만 그렇지 않습니다. 이번에 이야기하고 싶은 내용은 위의 관련 지식을 여러분의 두뇌에 채워넣으시고, 헛소리는 하지 않고 바로 코드로 넘어가겠습니다.
파일 이름: paging-bar.js
import React, { Component } from 'react' import Immutable from 'immutable' import _ from 'lodash' /* ================================================================================ * React GrxPagingBar 通用分页组件 * author: 天真的好蓝啊 * ================================================================================ */ class GrxPagingBar extends Component { render() { var pagingOptions = { showNumber: 5, firstText: "<<", firstTitle: "第一页", prevText: "<", prevTitle: "上一页", beforeTitle: "前", afterTitle: "后", pagingTitle: "页", nextText: ">", nextTitle: "下一页", lastText: ">>", lastTitle: "最后一页", classNames: "grx-pagingbar pull-right", } $.extend(pagingOptions, this.props.pagingOptions) return ( <div className={pagingOptions.classNames} > <GrxPagingFirst {...pagingOptions} {...this.props} /> <GrxPagingBeforeAfter isBefore="true" {...pagingOptions} {...this.props} /> <GrxPagingList {...pagingOptions} {...this.props} /> <GrxPagingBeforeAfter isBefore="false" {...pagingOptions} {...this.props} /> <GrxPagingLast {...pagingOptions} {...this.props} /> <GrxPagingInfo {...this.props} /> </div> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页条头组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingFirst extends Component { render() { var ids = [] let paging = this.props.items.get('Paging') let current = paging.get('PagingIndex') let pagingIndex = current - 1 if(paging.get('PagingIndex') != 1){ ids.push(1) } let html = ids.map( (v,i) => <span> <GrxPagingNumber title={this.props.firstTitle} text={this.props.firstText} pagingIndex={1} {...this.props}/> <GrxPagingNumber title={this.props.prevTitle} text={this.props.prevText} pagingIndex={pagingIndex} {...this.props}/> </span> ) return ( <span className="grx-pagingbar-fl"> {html} </span> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页条前后页组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingBeforeAfter extends Component { render() { var ids = [] let isBefore = this.props.isBefore == "true" ? true : false let paging = this.props.items.get('Paging') let pagingCount = paging.get('PagingCount') let current = paging.get('PagingIndex') let pagingIndex = isBefore ? current - this.props.showNumber : current + this.props.showNumber let title = (isBefore ? this.props.beforeTitle : this.props.afterTitle) + (this.props.showNumber + 1) + this.props.pagingTitle if(isBefore && current > this.props.showNumber + 1){ ids.push(1) }else if(!isBefore && current < pagingCount - this.props.showNumber){ ids.push(1) } var html = ids.map( (v,i) => <a href="###" onClick={this.props.actions.pagingAction.bind(this, pagingIndex)} title={title}>..</a> ) return ( <span> {html} </span> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页条页码列表组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingList extends Component { render(){ let paging = this.props.items.get('Paging') let count = paging.get('PagingCount') let current = paging.get('PagingIndex') let start = current - this.props.showNumber let end = current + this.props.showNumber var pageIndexs = new Array(); for(var i = start; i < end; i ++) { if( i == current){ pageIndexs.push(i) }else if(i > 0 & i <= count) { pageIndexs.push(i) } } var pagingList = pageIndexs.map( (v,i) => current == v ? count > 1 ? <span className="grx-pagingbar-current">{v}</span> : "" : <GrxPagingNumber pagingIndex={v} {...this.props} /> ) return( <span> {pagingList} </span> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页条尾部组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingLast extends Component { render() { var ids = [] let paging = this.props.items.get('Paging') let pagingCount = paging.get('PagingCount') let current = paging.get('PagingIndex') let pagingIndex = current + 1 if(paging.get('PagingIndex') < paging.get('PagingCount')){ ids.push(1) } let html = ids.map( (v,i) => <span> <GrxPagingNumber title={this.props.nextTitle} text={this.props.nextText} pagingIndex={pagingIndex} {...this.props}/> <GrxPagingNumber title={this.props.lastTitle} text={this.props.lastText} pagingIndex={pagingCount} {...this.props} /> </span> ) return ( <span className="grx-pagingbar-fl"> {html} </span> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页页码组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingNumber extends Component { render(){ let pagingIndex = this.props.pagingIndex let title = "第"+ pagingIndex + this.props.pagingTitle let text = pagingIndex if(this.props.title){ title = this.props.title } if(this.props.text){ text = this.props.text } return( <a href="###" onClick={this.props.actions.pagingAction.bind(this, pagingIndex)} title={title}> {text} </a> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分页条信息组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ class GrxPagingInfo extends Component { render() { let paging = this.props.items.get('Paging') let pagingIndex = paging.get('PagingIndex') let pagingCount = paging.get('PagingCount') let totalRecord = paging.get('TotalRecord') return ( <span className="grx-pagingbar-info">第{pagingIndex}/{pagingCount}页,共{totalRecord}条数据</span> ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 从此模块导出分页条组件 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ export default GrxPagingBar 使用方法: import React, { Component } from 'react' import _ from 'lodash' import classNames from 'classnames' import PagingBar from '.paging-bar' /* ================================================================================ * React PagingBar使用范例组件 * ================================================================================ */ class PagingBarExample extends Component { render() { let pagingOptions = { showNumber: 3 } return ( <table className="table table-condensed"> <tbody> <tr> <td> <PagingBar pagingOptions={pagingOptions} {...this.props} /> </td> </tr> </tbody> </table> ) } }
페이징 데이터 개체 Paging의 구조가 첨부되어 있습니다. paging.go 서버의 데이터 구조체:
package commons import ( "math" ) type ( Paging struct { PagingIndex int64 PagingSize int64 TotalRecord int64 PagingCount int64 Sortorder string } ) func (paging *Paging) SetTotalRecord(totalRecord int64) { //paging.PagingIndex = 1 paging.PagingCount = 0 if totalRecord > 0 { paging.TotalRecord = totalRecord paging.PagingCount = int64(math.Ceil(float64(paging.TotalRecord) / float64(paging.PagingSize))) } } func (paging *Paging) Offset() int64 { if paging.PagingIndex <= 1 || paging.PagingSize == 0 { return 0 } offset := (paging.PagingIndex * paging.PagingSize) - paging.PagingSize + 1 return offset } func (paging *Paging) EndIndex() int64 { if paging.PagingIndex <= 1 { return paging.PagingSize } offset := paging.PagingIndex * paging.PagingSize return offset }
위 내용입니다. Reactjs에서 Universal Paging 컴포넌트를 구현하는 예제 코드를 소개하고 싶습니다. 궁금한 점이 있으면 메시지를 남겨주시면 편집자가 시간에 맞춰 답변해 드리겠습니다. . 또한 PHP 중국어 웹사이트를 지원해 주신 모든 분들께 감사드립니다!
Reactjs를 사용하여 범용 페이징 구성 요소를 구현하는 예제 코드에 대한 더 많은 관련 기사를 보려면 PHP 중국어 웹사이트에 주목하세요!