当前位置:网站首页>定制一个自己项目里需要的分页器
定制一个自己项目里需要的分页器
2022-07-04 03:57:00 【新生代农民工官方认证码农小拽】
效果图:
代码:
/**简易分页器组件 * * API: * @currentPage 当前页码 * @pageSize 每页显示多少条 * @total 数据总数 * @onPageChange 当前页发生变化时触发的回调,参数是更新后的页码。子传父的关键。 */
import React, {
useState } from 'react'
import styles from './index.less'
const defaultCurrent = 1 // 默认当前页
const defaultPageSize = 20 // 默认每页20条数据
const defaultTotal = 0 // 默认总数为0
const valueLimit = /^\+?[1-9]\d*$/ // 限定输入页码为正整数
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}>上一页</div>
<div className={
styles.current}>{
current}</div>
<div className={
`${
styles.next} ${
current >= Math.ceil(total / pageSize) ? '' : styles.active}`} onClick={
handleNext}>下一页</div>
</div>
<div className={
styles.rig}>
<span className={
styles.total}>共{
Math.ceil(total / pageSize)}页</span>
<div className={
styles.jump_wrap}>
<span className={
styles.jump_btn} onClick={
jumpToPage}>前往</span>
<input
type="text"
autoComplete='off'
className={
styles.jump_num}
value={
page}
onChange={
e => {
handleChange(e)}}
/>
<span>页</span>
</div>
</div>
</div>
}
</>
}
export default Pagin
样式:
.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;
}
}
}
}
}
使用示例:
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>
}
边栏推荐
- Network equipment emergency response Guide
- Rhcsa 07 - user and group management
- Maui introductory tutorial series (5.xaml and page introduction)
- 戳气球和布尔运算问题(巨难)
- Distributed cap theory
- 【MATLAB】MATLAB 仿真模拟调制系统 — VSB 系统
- Statistical genetics: Chapter 3, population genetics
- 【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(余弦滚降成形脉冲)的眼图
- MySQL indexes and transactions
- PostgreSQL 正式超越 MySQL,这家伙也太强了吧!
猜你喜欢
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
关于solidworks standard无法获得许可 8544问题的总结
Rhcsa 04 - process management
RPC - gRPC简单的demo - 学习/实践
RAC delete damaged disk group
分享一些我的远程办公经验
Developing mqtt access program under QT
Can closed data be deleted by DBCA? can
Beipiao programmer, 20K monthly salary, 15W a year, normal?
YoloV6实战:手把手教你使用Yolov6进行物体检测(附数据集)
随机推荐
[cloud native] those lines of code that look awesome but have a very simple principle
Niuke Xiaobai monthly race 49
戳气球和布尔运算问题(巨难)
Deep parsing structured exception handling (SEH) - by Matt Pietrek
B. All Distinct
附件二:攻防演练保密协议.docx
Select function variable column name in dplyr of R language
Create ASM disk through DD
RPC Technology
Sample template of software design document - learning / practice
C language one-way linked list exercise
Operate the server remotely more gracefully: the practice of paramiko Library
电子元器件商城与数据手册下载网站汇总
First knowledge of batch processing
Annex VI: defense work briefing docx
Talking about what a high-quality little red book copy needs to have
【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
(pointer) write function void fun (int x, int *pp, int *n)
Unity中RampTex介绍和应用: 溶解特效优化
CRS-4013: This command is not supported in a single-node configuration.