Reactjs实现通用分页组件的实例代码


Posted in Javascript onJanuary 19, 2017

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

    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 = 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实现通用分页组件的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
网站上面有这种切换效果
Jun 26 Javascript
判断多个input type=file是否有已经选择好文件的代码
May 23 Javascript
基于mootools插件实现遮罩层新手引导
May 24 Javascript
jQuery实现类似滑动门切换效果的层切换
Sep 23 Javascript
js获得网页背景色和字体色的方法
Mar 21 Javascript
bootstrap datetimepicker日期插件使用方法
Jan 13 Javascript
JS获取当前地理位置的方法
Oct 25 Javascript
浅谈Node.js 子进程与应用场景
Jan 24 Javascript
深入探讨JavaScript的最基本部分之执行上下文
Feb 12 Javascript
微信小程序渲染性能调优小结
Jul 30 Javascript
Jquery实现获取子元素的方法分析
Aug 24 jQuery
24个解决实际问题的ES6代码片段(小结)
Feb 02 Javascript
react.js 翻页插件实例代码
Jan 19 #Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
Jan 19 #Javascript
AngularJS使用angular.bootstrap完成模块手动加载的方法分析
Jan 19 #Javascript
创建一般js对象的几种方式
Jan 19 #Javascript
用JavaScript实现让浏览器停止载入页面的方法
Jan 19 #Javascript
AngularJS使用ng-inlude指令加载页面失败的原因与解决方法
Jan 19 #Javascript
快速掌握jQuery插件开发
Jan 19 #Javascript
You might like
自己在做项目过程中学到的PHP知识收集
2012/08/20 PHP
PHP中使用foreach和引用导致程序BUG的问题介绍
2012/09/05 PHP
PHP中strtr字符串替换用法详解
2014/11/26 PHP
推荐十款免费 WordPress 插件
2015/03/24 PHP
js获取页面传来参数的方法
2014/09/06 Javascript
jquery实现的代替传统checkbox样式插件
2015/06/19 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
bootstrap table分页模板和获取表中的ID方法
2017/01/10 Javascript
js实现仿购物车加减效果
2017/03/01 Javascript
js手机号4位显示空格,银行卡每4位显示空格效果
2017/03/23 Javascript
jquery3和layui冲突导致使用layui.layer.full弹出全屏iframe窗口时高度152px问题
2019/05/12 jQuery
javascript设计模式 ? 代理模式原理与用法实例分析
2020/04/16 Javascript
详细介绍Python语言中的按位运算符
2013/11/26 Python
Python字符和字符值(ASCII或Unicode码值)转换方法
2015/05/21 Python
python读取二进制mnist实例详解
2017/05/31 Python
1分钟快速生成用于网页内容提取的xslt
2018/02/23 Python
Python音频操作工具PyAudio上手教程详解
2019/06/26 Python
解决django FileFIELD的编码问题
2020/03/30 Python
Python高并发和多线程有什么关系
2020/11/14 Python
Selenium关闭INFO:CONSOLE提示的解决
2020/12/07 Python
CSS3 Media Queries(响应式布局可以让你定制不同的分辨率和设备)
2013/06/06 HTML / CSS
英国口碑最好的的维他命胶囊品牌:Myvitamins(有中文站)
2016/12/03 全球购物
比利时的在线灯具店:Lampen24.be
2019/07/01 全球购物
Janie and Jack美国官网:GAP旗下的高档童装品牌
2019/09/09 全球购物
集团公司总经理岗位职责
2013/12/20 职场文书
车间副主任岗位职责
2013/12/24 职场文书
玩具公司的创业计划书
2013/12/31 职场文书
企业管理毕业生求职信范文
2014/03/07 职场文书
企业精细化管理实施方案
2014/03/23 职场文书
公司副总经理岗位职责
2014/10/01 职场文书
2014年业务员工作总结范文
2014/11/17 职场文书
巾帼文明岗事迹材料
2014/12/24 职场文书
活动总结书怎么写
2015/05/11 职场文书
二十年同学聚会感言
2015/07/30 职场文书
2016新年晚会开场白
2015/12/03 职场文书
使用vuex-persistedstate本地存储vuex
2022/04/29 Vue.js