溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Reactjs如何實(shí)現(xiàn)通用分頁(yè)組件

發(fā)布時(shí)間:2021-06-18 14:41:03 來(lái)源:億速云 閱讀:129 作者:小新 欄目:web開(kāi)發(fā)

這篇文章給大家分享的是有關(guān)Reactjs如何實(shí)現(xiàn)通用分頁(yè)組件的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

大家多少都自己寫(xiě)過(guò)各種版本的分頁(yè)工具條吧,像純服務(wù)版的,純jsWeb板的,Angular版的,因?yàn)檫@個(gè)基礎(chǔ)得不能再基礎(chǔ)的功能太多地方都會(huì)用到,下面我給出以個(gè)用ReactJS實(shí)現(xiàn)的版本,首先上圖看下效果:

    Reactjs如何實(shí)現(xiàn)通用分頁(yè)組件

    注意這個(gè)組件需要ES6環(huán)境,最好使用NodeJS結(jié)合Webpack來(lái)打包:webpack --display-error-details --config webpack.config.js

    此React版分頁(yè)組件請(qǐng)親們結(jié)合redux來(lái)使用比較方便,UI = Fn(State)

    基本流程就是:用戶(hù)交互->Action->Reduce->Store->UIRender

    了解以上基礎(chǔ)知識(shí)卻非常必要,但不是我此次要說(shuō)的重點(diǎn),以上相關(guān)知識(shí)請(qǐng)各位自行補(bǔ)腦,廢話(huà)就不多說(shuō),直接上代碼。

   filename: paging-bar.js

 import React, { Component } from 'react'
import Immutable from 'immutable'
import _ from 'lodash'
/* ================================================================================
 * React GrxPagingBar 通用分頁(yè)組件
 * author: 天真的好藍(lán)啊
 * ================================================================================ */
class GrxPagingBar extends Component {
 render() {
  var pagingOptions = {
   showNumber: 5,
   firstText: "<<",
   firstTitle: "第一頁(yè)",
   prevText: "<",
   prevTitle: "上一頁(yè)",
   beforeTitle: "前",
   afterTitle: "后",
   pagingTitle: "頁(yè)",
   nextText: ">",
   nextTitle: "下一頁(yè)",
   lastText: ">>",
   lastTitle: "最后一頁(yè)",
   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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)條頭組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)條前后頁(yè)組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)條頁(yè)碼列表組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)條尾部組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)頁(yè)碼組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 分頁(yè)條信息組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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}頁(yè),共{totalRecord}條數(shù)據(jù)</span>
  )
 }
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 從此模塊導(dǎo)出分頁(yè)條組件
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
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這個(gè)分頁(yè)數(shù)據(jù)對(duì)象的結(jié)構(gòu)paging.go服務(wù)端的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
}

感謝各位的閱讀!關(guān)于“Reactjs如何實(shí)現(xiàn)通用分頁(yè)組件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI