這篇文章給大家分享的是有關(guān)Reactjs如何實(shí)現(xiàn)通用分頁(yè)組件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)天水免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
大家多少都自己寫過(guò)各種版本的分頁(yè)工具條吧,像純服務(wù)版的,純jsWeb板的,Angular版的,因?yàn)檫@個(gè)基礎(chǔ)得不能再基礎(chǔ)的功能太多地方都會(huì)用到,下面我給出以個(gè)用ReactJS實(shí)現(xiàn)的版本,首先上圖看下效果:
注意這個(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)
基本流程就是:用戶交互->Action->Reduce->Store->UIRender
了解以上基礎(chǔ)知識(shí)卻非常必要,但不是我此次要說(shuō)的重點(diǎn),以上相關(guān)知識(shí)請(qǐng)各位自行補(bǔ)腦,廢話就不多說(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 () } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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) =>) return ( {html} ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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) => .. ) return ( {html} ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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 ? {v} : "" : ) return( {pagingList} ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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) => ) return ( {html} ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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( {text} ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 分頁(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 ( 第{pagingIndex}/{pagingCount}頁(yè),共{totalRecord}條數(shù)據(jù) ) } } /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * 從此模塊導(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 (
|
附上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í),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!