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 相关文章推荐
Jquery 扩展方法
May 06 Javascript
jquery实现每个数字上都带进度条的幻灯片
Feb 20 Javascript
JS防止用户多次提交的简单代码
Aug 01 Javascript
js showModalDialog参数的使用详解
Jan 07 Javascript
js闭包所用的场合以及优缺点分析
Jun 22 Javascript
IE6-IE9使用JSON、table.innerHTML所引发的问题
Dec 22 Javascript
jQuery中的Deferred和promise 的区别
Apr 03 Javascript
javascript解决小数的加减乘除精度丢失的方案
May 31 Javascript
bootstrapValidator bootstrap-select验证不可用的解决办法
Jan 11 Javascript
vue2.0项目中使用Ueditor富文本编辑器示例代码
Aug 14 Javascript
JS代码触发事件代码实例
Jan 02 Javascript
jQuery zTree如何改变指定节点文本样式
Oct 16 jQuery
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 unicode编码和字符串互转的方法
2020/08/12 PHP
解决laravel5.4下的group by报错的问题
2019/10/16 PHP
JavaScript脚本性能的优化方法
2007/02/02 Javascript
Add a Picture to a Microsoft Word Document
2007/06/15 Javascript
innerHTML与jquery里的html()区别介绍
2012/10/12 Javascript
用jquery中插件dialog实现弹框效果实例代码
2013/11/15 Javascript
jQuery中ajax的post()方法用法实例
2014/12/26 Javascript
详解JavaScript中循环控制语句的用法
2015/06/03 Javascript
详解javascript实现瀑布流列式布局
2016/01/29 Javascript
点击按钮出现60秒倒计时的简单js代码(推荐)
2016/06/07 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
jQuery实现的右下角广告窗体跟随效果示例
2016/09/16 Javascript
JS实现颜色梯度与渐变效果完整实例
2016/12/30 Javascript
JavaScript实现打印星型金字塔功能实例分析
2017/09/27 Javascript
Vue单页应用引用单独的样式文件的两种方式
2018/03/30 Javascript
vue-cli的build的文件夹下没有dev-server.js文件配置mock数据的方法
2019/04/17 Javascript
vue 引用自定义ttf、otf、在线字体的方法
2019/05/09 Javascript
vue使用better-scroll实现滑动以及左右联动
2020/06/30 Javascript
python获取指定网页上所有超链接的方法
2015/04/04 Python
浅谈Python中的私有变量
2018/02/28 Python
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
如何实现删除numpy.array中的行或列
2018/05/08 Python
python 实现视频流下载保存MP4的方法
2019/01/09 Python
python db类用法说明
2020/07/07 Python
HTML5的结构和语义(5):交互
2008/10/17 HTML / CSS
驴妈妈旅游网:中国新型的B2C旅游电子商务网站
2016/08/16 全球购物
Hello Molly美国:女性时尚在线
2019/08/26 全球购物
什么是Deployment descriptors;都有什么类型的部署描述符
2015/07/28 面试题
历史专业个人求职信分享
2013/12/20 职场文书
学子宴答谢词
2014/01/25 职场文书
《花木兰》教学反思
2014/04/09 职场文书
民事申诉状范本
2015/05/20 职场文书
python源码剖析之PyObject详解
2021/05/18 Python
用Python将GIF动图分解成多张静态图片
2021/06/11 Python
vue实现锚点定位功能
2021/06/29 Vue.js
Python字符串的转义字符
2022/04/07 Python