当前位置:网站首页>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>
}
边栏推荐
- Drozer tool
- 如何构建属于自己的知识引擎?社群开放申请
- qt下开发mqtt的访问程序
- 我们认为消费互联网发展到最后,依然会局限于互联网行业本身
- Developing mqtt access program under QT
- 【MATLAB】通信信号调制通用函数 — 傅里叶变换
- Annex II: confidentiality agreement for offensive and defensive drills docx
- 全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
- 【MATLAB】MATLAB 仿真模拟调制系统 — AM 已调信号的功率谱与相干解调
- Annexe VI: exposé sur les travaux de défense. Docx
猜你喜欢
分享一些我的远程办公经验
What is context?
Deep understanding of redis -- bloomfilter
Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises
Introduction and application of rampax in unity: optimization of dissolution effect
郑州正清园文化传播有限公司:针对小企业的7种营销技巧
Correct the classpath of your application so that it contains a single, compatible version of com. go
技术管理 - 学习/实践
附件六:防守工作简报.docx
中職組網絡安全—內存取證
随机推荐
LeetCode136+128+152+148
C language one-way linked list exercise
Qt QTableView数据列宽度自适应
Annex I: power of attorney for 202x XXX attack and defense drill
6-4 vulnerability exploitation SSH banner information acquisition
【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
关闭的数据能用dbca删除吗? 能
Intersection traffic priority, illustration of intersection traffic rules
Developing mqtt access program under QT
RPC - gRPC简单的demo - 学习/实践
Download kicad on Alibaba cloud image station
MySQL indexes and transactions
[go] database framework Gorm
Sécurité du réseau dans les écoles professionnelles secondaires - preuve de mémoire
附件一:202x年xxx攻防演习授权委托书
【MATLAB】MATLAB 仿真数字带通传输系统 — QPSK 和 OQPSK 系统
技术管理 - 学习/实践
令人头痛的延时双删
What should a novice pay attention to when looking for an escort
appliedzkp zkevm(13)中的Public Inputs