大家多少都自己写过各种版本的分页工具条吧,像纯服务版的,纯jsWeb板的,Angular版的,因为这个基础得不能再基础的功能太多地方都会用到,下面我给出以个用ReactJS实现的版本,首先上图看下效果:
   

注意这个组件需要ES6环境,最好使用NodeJS结合Webpack来打包:webpack --display-error-details --config webpack.config.js

此React版分页组件请亲们结合redux来使用比较方便,UI = Fn(State)
    基本流程就是:用户交互->Action->Reduce->Store->UIRender

了解以上基础知识却非常必要,但不是我此次要说的重点,以上相关知识请各位自行补脑,废话就不多说,直接上代码。

filename: 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服务端的Data Struct:

 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 =
      {
         paging.TotalRecord = totalRecord
         paging.PagingCount = int64(math.Ceil(float64(paging.TotalRecord) / float64(paging.PagingSize)))
     }
 }

 func (paging *Paging) Offset() int64 {
      || paging.PagingSize ==  {

     }

     offset := (paging.PagingIndex * paging.PagingSize) - paging.PagingSize + 

     return offset
 }

 func (paging *Paging) EndIndex() int64 {
      {
         return paging.PagingSize
     }

     offset := paging.PagingIndex * paging.PagingSize

     return offset
 }

感谢园子里的亲们,2016新年快乐*_^。

ReactJS实现的通用分页组件的更多相关文章

  1. asp.net MVC通用分页组件 使用方便 通用性强

    asp.net MVC通用分页组件 使用方便 通用性强   该分页控件的显示逻辑: 1 当前页面反色突出显示,链接不可点击 2 第一页时首页链接不可点击 3 最后一页时尾页链接不可点击 4 当前页面左 ...

  2. 基于Vue的简单通用分页组件

    分页组件是每一个系统里必不可少的一个组件,分页组件分为两部分.第一部分是模版部分,用于显示当前分页组件的状态,例如正在获取数据.没有数据.没有下一页等等:第二部分是分页数据对象,用于封装一个分页组件的 ...

  3. 详解分页组件中查count总记录优化

    1 背景 研究mybatis-plus(以下简称MBP),使用其分页功能时.发现了一个JsqlParserCountOptimize的分页优化处理类,官方对其未做详细介绍,网上也未找到分析该类逻辑的只 ...

  4. Mvc分页组件MvcSimplePager代码重构

    1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...

  5. Mvc分页组件MvcSimplePager代码重构及使用

    1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...

  6. vuejs2.0实现分页组件,使用$emit进行事件监听数据传递

    上一篇文章介绍了vuejs实现的简单分页,如果我有几个页面都需要有分页效果,不可能每个页面都去复制一下这段代码吧,意思是封装一下,变成通用的组件. 首先使用基础 Vue 构造器,创建一个“子类”,Vu ...

  7. SNF快速开发平台MVC-瀑布式分页组件

    1.   瀑布式分页 目前已经比较流行了,以往的这种点击分页已经不能满足广大网民的需求了.像百度图片等等,网站都有滚动滚轮直接分页的功能,这样体验也确实好了不少,所以我们也决定在我们的框架内进行集成此 ...

  8. 手把手教你使用Vue/React/Angular三大框架开发Pagination分页组件

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  9. 基于Vue.js的表格分页组件

    有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...

随机推荐

  1. C++编程小知识随手记

    C++编程小知识点: (1)queue和vector类型: 加入元素 : queue是queue.push(),vector是vector.push_back(), 删除元素: queue是queue ...

  2. ios视图frame和bounds的对比

    bounds坐标:自己定义的坐标系统,setbound指明了本视图左上角在该坐标系统中的坐标,   默认值(0,0) frame坐标:  子视图左上角在父视图坐标系统(bounds坐标系统)中的坐标, ...

  3. SAP 设置屏幕字段的隐藏、显示、必填和可选,以设置物料组为例

    1.事务码MM01,把物料组设为选填字段. 2.找到物料组的屏幕字段. 3.在后台根据屏幕字段找到对应字段组.后台路径:后勤-常规—物料主数据—字段选择—给字段组分配字段.点击后面的箭头进入下一屏幕. ...

  4. T-SQL、JET SQL、PL-SQL

    数据库分为网状.层状.关系.对象四种类型,目前的数据库一般都是属于关系数据库(包括MYSQL和SQL SERVER),网状.层状基本上已成历史,对象类型尚未普及. SQL 语言是有 ANSI 标准的. ...

  5. Thinkphp 3.2中字符串截取

    将此方法放到Thinkphp/Common/function.php里/* * 字符串截取函数 * 大白驴 * 2016-11-29 qq 675835721 * */function msubstr ...

  6. MYSQL命令行使用指南

    一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -u ...

  7. ftgl 绘制文字

    FTFont* ftfont = new FTGLPixmapFont(); ftfont->Open("D:/SIMHEI.ttf"); ftfont->FaceSi ...

  8. 大米网赚项目介绍,官方唯一客服QQ:712994168

    大米平台项目来源   QQ:712994168 大米软件本质上是一个高质量网赚项目收集和发布平台,该平台的所有项目都是经过专业的测试团队实测有效的项目和教程,只要去做绝对可以赚钱.平台里面的项目类型包 ...

  9. 四核exynos4412开发板使用网线上网注意事项

    问:RP4412开发板板子可以插网线上网? 答:可以.支持WIFI.LAN.3/4G上网的.插网线没? 问:我插了,他还是提示让我连wifi. 答:你是上网页还是其他的APP. 网页可以直接打开,有部 ...

  10. sqlserver行列转换问题(网上搜集)

    (列->行) 一.FOR XML PATH 简单介绍              那么还是首先来介绍一下FOR XML PATH ,假设现在有一张兴趣爱好表(hobby)用来存放兴趣爱好,表结构如 ...