Food Ordering App: Filters and Search Implementation
Search and filters are key features. Here is how to implement them efficiently.
Food Ordering App: Filters and Search Implementation
Search and filters are key features of the food ordering app. Here is how to implement them.
Search
let searchQuery = ""; document.getElementById("search").addEventListener("input", (e) => { searchQuery = e.target.value; renderMenu(); }); `` Filter items by name (case-insensitive): ```js if (searchQuery && !item.name.toLowerCase().includes(searchQuery.toLowerCase())) return false; `` ### Filters ```js let filters = { veg: null, // true (veg only), false (non-veg only), null (all) category: "all", // "all", "italian", "fastfood", "healthy" maxPrice: Infinity, }; `` #### Veg/Non-Veg Toggle ```js function toggleVeg() { filters.veg = filters.veg === null ? true : filters.veg === true ? false : null; renderMenu(); } `` #### Category Dropdown ```js document.getElementById("category").addEventListener("change", (e) => { filters.category = e.target.value; renderMenu(); }); `` #### Price Range ```js document.getElementById("maxPrice").addEventListener("input", (e) => { filters.maxPrice = parseInt(e.target.value) || Infinity; renderMenu(); }); `` ### Combined Filter Function ```js function getFilteredItems() { return menu.filter((item) => { if (searchQuery && !item.name.toLowerCase().includes(searchQuery.toLowerCase())) return false; if (filters.veg !== null && item.veg !== filters.veg) return false; if (filters.category !== "all" && item.category !== filters.category) return false; if (item.price > filters.maxPrice) return false; return true; }); } `` ### Debounce the Search ```js const debouncedSearch = debounce((query) => { searchQuery = query; renderMenu(); }, 300); document.getElementById("search").addEventListener("input", (e) => { debouncedSearch(e.target.value); }); `` ### No Results State ```js function renderMenu() { const items = getFilteredItems(); if (items.length === 0) { list.innerHTML = "<p>No items found</p>"; return; } // render items } `` ### The Takeaway Implement search (case-insensitive name filter), filters (veg/non-veg toggle, category dropdown, price range), combined filter function, debounced search (300ms), and no results state. Re-render the menu on every filter change. Show 'No items found' when empty.
Add an input listener. Filter items by name (case-insensitive): item.name.toLowerCase().includes(query.toLowerCase()). Re-render the menu on every input. Debounce the search (300ms) to avoid excessive re-renders.
Store filter state (veg: true/false/null, category: string, maxPrice: number). In the filter function, check each condition. Re-render on every filter change. Combine search and filters in one getFilteredItems function.
Yes. Debounce the search input (300ms) to avoid re-rendering the menu on every keystroke. This improves performance, especially with large menus or when the search triggers an API call.
Check if the filtered items array is empty. If so, show a message like 'No items found' or 'No items match your filters' instead of the item list. This is better than showing a blank screen.
Write a single getFilteredItems function that applies all conditions: search query, veg/non-veg, category, and price range. Return menu.filter(item => all conditions pass). Re-render on any filter or search change.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

