当前位置:网站首页>Customize a pager needed in your project
Customize a pager needed in your project
2022-07-04 04:58:00 【Official certification code of the new generation of migrant wo】
design sketch :
Code :
/** Simple pager assembly * * API: * @currentPage The current page number * @pageSize How many items are displayed per page * @total The total number of data * @onPageChange The callback triggered when the current page changes , The parameter is the updated page number . The key to passing from son to father . */
import React, {
useState } from 'react'
import styles from './index.less'
const defaultCurrent = 1 // Default current page
const defaultPageSize = 20 // Default per page 20 Data
const defaultTotal = 0 // The default total is 0
const valueLimit = /^\+?[1-9]\d*$/ // Limit the input page number to a positive integer
const Pagin = props => {
let {
currentPage, pageSize, total, onPageChange } = props
currentPage = currentPage || defaultCurrent
pageSize = pageSize || defaultPageSize
total = total || defaultTotal
const [current, setCurrent] = useState(currentPage)
const [page, setPage] = useState(1)
const handlePre = () => {
if (onPageChange && current > 1) {
onPageChange(current - 1)
setCurrent(current - 1)
}
}
const handleNext = () => {
if (onPageChange && current < Math.ceil(total / pageSize)) {
onPageChange(current + 1)
setCurrent(current + 1)
}
}
const handleChange = e => {
const val = e.target.value
if (valueLimit.test(val) || val === '') {
setPage(val)
}
}
const jumpToPage = () => {
if (onPageChange && page >= 1 && page <= Math.ceil(total / pageSize)) {
onPageChange(page)
setCurrent(Number(page))
}
}
return <>
{
total > 0
&&
<div className={
styles.pagin}>
<div className={
styles.left}>
<div className={
`${
styles.pre} ${
current <= 1 ? '' : styles.active}`} onClick={
handlePre}> The previous page </div>
<div className={
styles.current}>{
current}</div>
<div className={
`${
styles.next} ${
current >= Math.ceil(total / pageSize) ? '' : styles.active}`} onClick={
handleNext}> The next page </div>
</div>
<div className={
styles.rig}>
<span className={
styles.total}> common {
Math.ceil(total / pageSize)} page </span>
<div className={
styles.jump_wrap}>
<span className={
styles.jump_btn} onClick={
jumpToPage}> Go to </span>
<input
type="text"
autoComplete='off'
className={
styles.jump_num}
value={
page}
onChange={
e => {
handleChange(e)}}
/>
<span> page </span>
</div>
</div>
</div>
}
</>
}
export default Pagin
style :
.pagin {
position: relative;
display: flex;
justify-content: space-between;
height: 38px;
margin-top: 40px;
box-sizing: border-box;
padding: 0 10px;
font-size: 14px;
font-weight: 400;
color: #222;
.left {
width: 210px;
display: flex;
justify-content: space-between;
.current {
width: 38px;
height: 100%;
border-radius: 4px;
text-align: center;
line-height: 38px;
color: #fff;
background-color: #673ab7cc;
}
.pre, .next {
width: 75px;
height: 100%;
text-align: center;
line-height: 38px;
color: #c0c4cc;
background-color: #f4f4f5;
border-radius: 4px;
user-select: none;
-webkit-user-select: none;
cursor: not-allowed;
&.active {
color: #fff;
background-color: #673ab7cc;
cursor: pointer;
}
}
}
.rig {
display: flex;
align-items: center;
gap: 10px;
.jump_wrap {
display: flex;
gap: 3px;
align-items: center;
margin-top: 0;
.jump_btn {
color: #673ab7cc;
user-select: none;
-webkit-user-select: none;
cursor: pointer;
}
.jump_num {
width: 46px;
height: 28px;
text-align: center;
color: #606266;
border: 1px solid #dcdfe6;
border-radius: 3px;
&:focus {
border: 1px solid #673ab7cc;
}
}
}
}
}
Examples of use :
import React from 'react'
import styles from './index.less'
import Pagin from '@pages/pagin'
const App = () => {
const onPageChange = (page) => {
console.log('page: ', page)
}
return <div>
<Pagin onPageChange={
onPageChange} total={
41} />
</div>
}
边栏推荐
猜你喜欢
Secondary vocational group network security - memory Forensics
PostgreSQL 正式超越 MySQL,这家伙也太强了吧!
2022年6月总结
在代碼中使用度量單比特,從而生活更美好
C basic (VII) document operation
Niuke Xiaobai monthly race 49
Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it
Formatted text of Kivy tutorial (tutorial includes source code)
电子元器件商城与数据手册下载网站汇总
中科磐云—D模块解析以及评分标准
随机推荐
PostgreSQL 正式超越 MySQL,这家伙也太强了吧!
COMP1721 Creating Classes
cmake
Rollup各组件作用
How to build your own knowledge engine? Community open application
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
The paddlehub face recognition scheme is deployed, and the trained model is deployed and applied in pytchrom
通过dd创建asm disk
Technology Management - learning / practice
附件二:攻防演练保密协议.docx
【MATLAB】MATLAB 仿真 — 低通高斯白噪声
Maui introductory tutorial series (5.xaml and page introduction)
网络设备应急响应指南
The first introduction, stages and methods of defense system breakthrough from the perspective of the red team
【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(第 I 类部分响应波形)的眼图
海力士EMMC5.0及5.1系列对比详解
STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
LeetCode136+128+152+148
Unity中RampTex介绍和应用: 溶解特效优化
GUI application: socket network chat room