当前位置:网站首页>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>
}
边栏推荐
- RPC - grpc simple demo - learn / practice
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- 在代碼中使用度量單比特,從而生活更美好
- Precautions for accompanying driving these 23 points should be paid attention to!
- Introduction and application of rampax in unity: optimization of dissolution effect
- Y55. Chapter III kubernetes from entry to proficiency -- HPA controller and metrics server (28)
- 【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
- 技术管理 - 学习/实践
- VSCode的有用插件
- 郑州正清园文化传播有限公司:针对小企业的7种营销技巧
猜你喜欢

Annexe VI: exposé sur les travaux de défense. Docx

RPC - grpc simple demo - learn / practice

中职组网络安全—内存取证

Notes on the paper "cross view transformers for real time map view semantic segmentation"

qt下开发mqtt的访问程序

Intersection traffic priority, illustration of intersection traffic rules

The five pictures tell you: why is there such a big gap between people in the workplace?

STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管

技术管理 - 学习/实践
![[Yugong series] go teaching course 002 go language environment installation in July 2022](/img/a8/79c48e426ce909db960d446f923795.png)
[Yugong series] go teaching course 002 go language environment installation in July 2022
随机推荐
"Don't care too much about salary when looking for a job", this is the biggest lie I've ever heard
Annex 2-2 confidentiality commitment docx
ADB tools
Rollup各组件作用
How do good test / development programmers practice? Where to go
Drozer tool
6-5 vulnerability exploitation SSH weak password cracking and utilization
附件三:防守方评分标准.docx
rac删除损坏的磁盘组
Niuke Xiaobai monthly race 49
Correct the classpath of your application so that it contains a single, compatible version of com.go
【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
【MATLAB】通信信号调制通用函数 — 窄带高斯白噪声的生成
Create ASM disk through DD
20000 words will take you to master multithreading
简单g++和gdb调试
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
Dp83848+ network cable hot plug
[go] database framework Gorm