亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Reactjs實現(xiàn)通用分頁組件的實例代碼

 更新時間:2017年01月19日 11:07:04   作者:天真的好藍啊  
這篇文章主要介紹了Reactjs實現(xiàn)通用分頁組件的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

大家多少都自己寫過各種版本的分頁工具條吧,像純服務版的,純jsWeb板的,Angular版的,因為這個基礎得不能再基礎的功能太多地方都會用到,下面我給出以個用ReactJS實現(xiàn)的版本,首先上圖看下效果:

   

    注意這個組件需要ES6環(huán)境,最好使用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}條數(shù)據(jù)</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這個分頁數(shù)據(jù)對象的結構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實現(xiàn)通用分頁組件的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

  • react自定義實現(xiàn)狀態(tài)管理詳解

    react自定義實現(xiàn)狀態(tài)管理詳解

    這篇文章主要為大家詳細介紹了react如何自定義實現(xiàn)狀態(tài)管理,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01
  • React組件創(chuàng)建與事件綁定的實現(xiàn)方法

    React組件創(chuàng)建與事件綁定的實現(xiàn)方法

    react事件綁定時。this并不會指向當前DOM元素。往往使用bind來改變this指向,今天通過本文給大家介紹React事件綁定的方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • react的context和props詳解

    react的context和props詳解

    這篇文章主要介紹了react的context和props的相關資料,幫助大家更好的理解和學習使用React,感興趣的朋友可以了解下
    2021-11-11
  • 使用React?MUI庫實現(xiàn)用戶列表分頁功能

    使用React?MUI庫實現(xiàn)用戶列表分頁功能

    MUI是一款基于React的UI組件庫,可以方便地構建美觀的用戶界面,使用MUI的DataTable組件和分頁器組件可以輕松實現(xiàn)用戶列表分頁功能,這篇文章使用MUI庫實現(xiàn)了用戶列表分頁功能,感興趣的同學可以參考下文
    2023-05-05
  • React函數(shù)式組件Hook中的useEffect函數(shù)的詳細解析

    React函數(shù)式組件Hook中的useEffect函數(shù)的詳細解析

    useEffect是react v16.8新引入的特性。我們可以把useEffect hook看作是componentDidMount、componentDidUpdate、componentWillUnmounrt三個函數(shù)的組合
    2022-10-10
  • react生命周期(類組件/函數(shù)組件)操作代碼

    react生命周期(類組件/函數(shù)組件)操作代碼

    react代碼模式分為兩種類組件和函數(shù)組件(生命周期也有所不同),這篇文章主要介紹了react生命周期(類組件/函數(shù)組件),需要的朋友可以參考下
    2023-01-01
  • 如何使用Redux Toolkit簡化Redux

    如何使用Redux Toolkit簡化Redux

    這篇文章主要介紹了如何使用Redux Toolkit簡化Redux,幫助大家更好的理解和學習使用React框架,感興趣的朋友可以了解下
    2021-04-04
  • React?Hook中的useState函數(shù)的詳細解析

    React?Hook中的useState函數(shù)的詳細解析

    Hook 就是 JavaScript 函數(shù),這個函數(shù)可以幫助你鉤入(hook into) React State以及生命周期等特性,這篇文章主要介紹了React?Hook?useState函數(shù)的詳細解析的相關資料,需要的朋友可以參考下
    2022-10-10
  • 實例講解React 組件

    實例講解React 組件

    這篇文章主要介紹了React 組件的相關資料,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • React 子組件向父組件傳值的方法

    React 子組件向父組件傳值的方法

    本篇文章主要介紹了React 子組件向父組件傳值的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論