From 3b189d42704ac9b43b6488c7b9824192a8f931c3 Mon Sep 17 00:00:00 2001 From: Harshitha Shetty <141444342+HarshithaShetty27@users.noreply.github.com> Date: Sat, 25 Jan 2025 00:22:06 +0530 Subject: [PATCH] Pagination --- client/src/Pages/ConsolidatedTable.jsx | 55 +++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/client/src/Pages/ConsolidatedTable.jsx b/client/src/Pages/ConsolidatedTable.jsx index a87acaf..bdea1b0 100644 --- a/client/src/Pages/ConsolidatedTable.jsx +++ b/client/src/Pages/ConsolidatedTable.jsx @@ -7,6 +7,8 @@ import { createExcelBook } from "../api"; const ConsolidatedTable = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); + const [currentPage, setCurrentPage] = useState(1); + const tablesPerPage = 5; useEffect(() => { const fetchData = async () => { @@ -32,6 +34,21 @@ const ConsolidatedTable = () => { // Extract unique faculty names const uniqueTeachers = [...new Set(data.map((row) => row.Name))]; + //pagination + const indexOfLastTable = currentPage * tablesPerPage; + const indexOfFirstTable = indexOfLastTable - tablesPerPage; + const currentTeachers = uniqueTeachers.slice(indexOfFirstTable,indexOfLastTable); + + const totalPages = Math.ceil(uniqueTeachers.length/tablesPerPage); + + const handleNextPage = ()=>{ + if(currentPage < totalPages) setCurrentPage((prevPage)=>prevPage+1); + }; + + const handlePrevPage = ()=>{ + if(currentPage > 1) setCurrentPage((prevPage)=>prevPage-1); + }; + const createExcelFile = (teacherData, teacherName) => { const workbook = createExcelBook(teacherData, teacherName); XLSX.writeFile(workbook, `${teacherName.replace(/\s+/g, "_")}_Table.xlsx`); @@ -129,7 +146,7 @@ const ConsolidatedTable = () => { backgroundColor: "#f9f9f9", }} > - {uniqueTeachers.map((teacher, index) => { + {currentTeachers.map((teacher, index) => { const teacherData = data.filter((row) => row.Name === teacher); return (