Content ITV PRO
This is Itvedant Content department
Business Scenario
Hello talented developers!
In the previous lab, Lab 8 — ShopKart Product Details & Dynamic Routing, you enhanced the ShopKart application by allowing users to navigate from the Product Page to individual Product Details pages. Users could view detailed product information, specifications, reviews, select quantities, and add products to the cart.
As the ShopKart product catalog grows, displaying a large number of products on a single page can make it difficult for users to find and compare products. To improve product discovery and navigation, Lab 9 will enhance the existing Product Page by introducing sorting and pagination.
In this lab, students will implement sorting options that allow users to organize products based on criteria such as price, rating, and popularity.
Pagination will divide the products into multiple pages and display only a specific number of products on each page.
Next-Lab Preparation
Module:
1) useState , Array Methods
2) Event handling , Conditional Rendering, List Rendering
git pull origin branchNameGit Pull
Task 1 : Add Sorting Functionality
Create Sorting State
1
const [sortOption, setSortOption] = useState("");useState if it is not already imported and inside the Products component, addAdd the Sort Dropdown
2
<div className="sort-section">
<label htmlFor="sort">Sort By:</label>
<select id="sort" value={sortOption} onChange={(e) => setSortOption(e.target.value)}>
<option value="">Default</option>
<option value="price-low">Price: Low to High</option>
<option value="price-high">Price: High to Low</option>
<option value="rating-high">Rating: High to Low</option>
<option value="rating-low">Rating: Low to High</option>
<option value="popular">Most Popular</option>
</select>
</div>Create a Copy of Filtered Products
3
sort().filteredProducts logic, add:let sortedProducts = [...filteredProducts];Implement Price Sorting
4
sort() method to arrange products according to their price.if (sortOption === "price-low") {
sortedProducts.sort(
(a, b) =>
Number(String(a.price).replace(/,/g, "")) -
Number(String(b.price).replace(/,/g, ""))
);
}
if (sortOption === "price-high") {
sortedProducts.sort(
(a, b) =>
Number(String(b.price).replace(/,/g, "")) -
Number(String(a.price).replace(/,/g, ""))
);
}.sort((a, b) => ...) compares two products at a time.String() and .replace() remove commas from prices like "12,499".Number() converts the price into a number for proper comparison.a - b sorts low to high, while b - a sorts high to low.Implement Rating Sorting
5
if (sortOption === "rating-high") {
sortedProducts.sort( (a, b) => b.rating - a.rating );
}
if (sortOption === "rating-low") {
sortedProducts.sort( (a, b) => a.rating - b.rating );
}Implement Rating Sorting
6
if (sortOption === "popular") {
sortedProducts.sort(
(a, b) => b.reviews - a.reviews
);
}Display the Sorted List
7
filteredProducts.map() with sortedProducts.map() so the Product Page displays the products according to the selected sorting option.filteredProducts.map((product) => (sortedProducts.map((product) => (sortedProducts.length > 0Style the Sort drop-down
8
Task 2 : Add Pagination
Create Pagination State
1
const [currentPage, setCurrentPage] = useState(1);
const productsPerPage = 8;Calculate the Total Number of Pages
2
const totalPages = Math.ceil( sortedProducts.length / productsPerPage);12 products ÷ 8 products per page = 2 pages
sortedProducts.length --> Gives the total number of products after sorting.productsPerPage --> Stores how many products we want to show on each page.Math.ceil() rounds a number up to the next whole number.12 / 8 = 1.5 --> This means we need 1.5 pages, but we cannot have half a page.Calculate the Total Number of Pages
3
slice() array method to display only the products belonging to the selected page.const startIndex = (currentPage - 1) * productsPerPage;
const endIndex = startIndex + productsPerPage;
const currentProducts = sortedProducts.slice(startIndex, endIndex);startIndex finds where the current page's products should start. For example, Page 1 → (1 - 1) × 8 = 0, so it starts at index 0.endIndex finds where the current page's products should end. For Page 1 → 0 + 8 = 8.slice(startIndex, endIndex) takes only that portion of the product list. So Page 1 gets products at indexes 0–7, and Page 2 gets indexes 8–11.Page 1 → indexes 0 – 7 → 8 products
Page 2 → indexes 8 – 15 → 8 products
Page 3 → indexes 16 – 23 → 8 products
For example :
Display Current Page Products
4
sortedProducts.map((product) => (currentProducts.map((product) => (But note one thing...
sortedProducts.length > 0sortedProducts represents the complete product list after filtering and sorting. Pagination only determines which products from this list are displayed on the current page.Create Pagination Buttons
5
{totalPages > 1 && (
<div className="pagination">
<button onClick={() => setCurrentPage((previous) => Math.max(previous - 1, 1))}
disabled={currentPage === 1}> ← </button> {Array.from({ length: totalPages }, (_, index) => (
<button key={index + 1} className={currentPage === index + 1 ? "active" : ""}
onClick={() => setCurrentPage(index + 1)}>
{index + 1}
</button>
))}
<button onClick={() => setCurrentPage((previous) =>
Math.min(previous + 1, totalPages))}
disabled={currentPage === totalPages}>
→
</button>
</div>
)}For 16 products:
← 1 2 3 →Reset Pagination When Sorting Changes
6
useEffect(() => {setCurrentPage(1);}, [sortOption]);Reset Pagination When Filters Change
7
useEffect(() => {setCurrentPage(1);}, [selectedCategories,maxPrice,selectedRating,availability]);This means whenever any of these filters changes:
selectedCategoriesmaxPriceselectedRatingavailabilityStyle the Pagination buttons
8
We are done with this lab. The latest source code has been uploaded to GitHub. You can access the latest commit using the link below:
Great job!
You successfully implemented dynamic product details, routing, cart actions, and a 404 page in ShopKart.
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Module:
1) useState , Array Methods
2) Event handling , Conditional Rendering, List Rendering
By Content ITV