英文:
Why page navigation numbers are not working correctly in my React Code?
问题
我怀疑这一行出现了问题:
{Array.from({length: totalPages+1}, (_, i) => i)
.slice(selectedPageNumber <= 5 ? 1 : selectedPageNumber - 5, selectedPageNumber < 5 ? 10 : selectedPageNumber + 6)
}
当我在最后的按钮上调用changeSelectedPageNumber()
函数时,可能我遗漏了一些东西。
我在哪里犯了错误?
如果我理解得正确,您的问题是关于页面导航数字显示的问题。您希望在输入页数后,页面导航数字能够正确显示。
在上述代码中,selectedPageNumber
表示当前选定的页数,而 totalPages
表示总页数。这段代码的目的是生成一个包含导航数字的数组,以便在页面上显示导航按钮。
首先,它使用 Array.from
创建了一个包含数字的数组,从1到 totalPages
。然后,使用 .slice
方法根据 selectedPageNumber
的值截取需要显示的部分。
问题可能出在 slice
方法的参数上。如果 selectedPageNumber
小于或等于5,它截取的范围是从1到10。但如果 selectedPageNumber
大于5,它截取的范围可能会超出总页数。
为了修复问题,您可以尝试修改这段代码,以确保 slice
方法不会截取超出范围的部分。您可以使用 Math.min
和 Math.max
函数来限制截取范围,如下所示:
{Array.from({length: totalPages}, (_, i) => i + 1)
.slice(Math.max(1, selectedPageNumber - 5), Math.min(totalPages, selectedPageNumber + 6))
}
这将确保 slice
方法不会截取超出总页数范围的部分,同时保持导航按钮的显示正确。希望这对您有所帮助。
英文:
I do not like questions like "Why code is not working?" but I cannot find solution to my problem. I have a react component including a table and pagination numbers as follow:
Pagination numbers are working good but my problem is that, when I navigate to specific page by entering Page number into input field, and if it is more than 5 then the numbers 1,2,3,4,5,6... going too much as follows:
Here is my code:
import React, {useState, useEffect} from "react";
import Table from 'react-bootstrap/Table';
import Pagination from 'react-bootstrap/Pagination';
import 'bootstrap/dist/css/bootstrap.min.css';
import * as cityService from "../service/CityService";
import Button from "react-bootstrap/Button";
import {useNavigate} from "react-router-dom";
import HeaderPanel from "./HeaderPanel";
function CityTable() {
const [selectedPageNumber, setSelectedPageNumber] = useState(1);
const [perPage, setPerPage] = useState(5);
const [totalElement, setTotalElement] = useState(0);
const [currentData, setCurrentData] = useState([]);
const [pageNavigationNumber, setPageNavigationNumber] = useState(1);
const [cityName, setCityName] = useState('');
const totalPages = Math.ceil(totalElement / perPage);
let navigate = useNavigate();
const changeSelectedPageNumber = (pageNumber) => {
// setPageNavigationNumber(pageNumber);
setSelectedPageNumber(pageNumber);
};
const changePageSize = (event) => {
const newPerPage = parseInt(event.target.value, 10);
setSelectedPageNumber(1);
setPerPage(newPerPage);
};
const findCities = async (cityName) => {
let result = await cityService.getCityPage(cityName, selectedPageNumber - 1, perPage, sessionStorage.getItem('jwtToken'));
setCurrentData(result.data.content);
setTotalElement(result.data.totalElements);
};
const handleRowClick = (item) => {
sessionStorage.setItem('id', item.id);
sessionStorage.setItem('cityname', item.name);
sessionStorage.setItem('photo', item.photo);
navigate("/details")
}
const addDefaultSrc = (ev) =>
{
ev.target.src = require('../assets/nophoto.png');
}
useEffect(() => {
findCities(cityName, selectedPageNumber, perPage);
}, [selectedPageNumber, perPage]);
return (
<div>
<HeaderPanel></HeaderPanel>
<input type={"search"}
placeholder={"Search City"}
onChange={e => setCityName(e.target.value)}
></input>
<Button onClick={() => findCities(cityName, selectedPageNumber, perPage)}>Search</Button>
<Table striped bordered hover>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
{currentData.map((city) => (
<tr key={city.id} onClick={() => handleRowClick(city)}>
<td>{city.id}</td>
<td>{city.name}</td>
<td><img onError={addDefaultSrc} width={50} src={city.photo}/></td>
</tr>
))}
</tbody>
</Table>
<div className="d-flex justify-content-between align-items-center">
<Pagination className="justify-content-lg-center">
<select value={perPage} onChange={changePageSize}>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<Pagination.Prev
onClick={() => changeSelectedPageNumber(selectedPageNumber - 1)}
disabled={selectedPageNumber === 1}
/>
{Array.from({length: totalPages+1}, (_, i) => i)
.slice(selectedPageNumber <= 5 ? 1 : selectedPageNumber - 5, selectedPageNumber < 5 ? 10 : selectedPageNumber + 6)
.map(page => (
<Pagination.Item
key={page}
active={page === selectedPageNumber}
onClick={() => changeSelectedPageNumber(page)}
>
{page}
</Pagination.Item>
))
}
<Pagination.Next
onClick={() => changeSelectedPageNumber(selectedPageNumber + 1)}
disabled={selectedPageNumber === totalPages}
/>
</Pagination>
</div>
Page number:
<input type={"number"}
placeholder={"Go to page"}
value={pageNavigationNumber}
onChange={e => setPageNavigationNumber(e.target.value)}></input>
<Button onClick={() => changeSelectedPageNumber(pageNavigationNumber)}>Go to page</Button>
</div>
);
}
export default CityTable;
I am suspicious of the line:
{Array.from({length: totalPages+1}, (_, i) => i)
.slice(selectedPageNumber <= 5 ? 1 : selectedPageNumber - 5, selectedPageNumber < 5 ? 10 : selectedPageNumber + 6)
When I call changeSelectedPageNumber()
function on button at last, maybe I am missing something.
Where am I making mistake?
答案1
得分: 1
每当您在输入字段中输入内容时,它都会被转换为字符串,这就是您在此处遇到问题的原因,您需要将输入从字符串转换为数字,然后进行数学计算。
您可以使用 parseInt
或 +
(缩写运算符)来将其转换为数字。
onChange={e => setPageNavigationNumber(+e.target.value)}></input>
英文:
Whenever you enter something in the input field, it is getting converting into string
and that is why you are getting a problem here, you need to convert your input from string
to number
and do the Math
calculation.
You can use parseInt
or +
(shortened operator) for converting into a number.
onChange={e => setPageNavigationNumber(+e.target.value)}></input>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论