├── img
├── tab-bar-icon.png
├── no-data-found.png
├── search-not-found.png
└── site-demo-image.png
├── README.md
├── style
└── style.css
├── Untitled Diagram.drawio
├── index.html
└── script
└── script.js
/img/tab-bar-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samarthdadhaniya/CRUD-Operation-in-LocalStorage/HEAD/img/tab-bar-icon.png
--------------------------------------------------------------------------------
/img/no-data-found.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samarthdadhaniya/CRUD-Operation-in-LocalStorage/HEAD/img/no-data-found.png
--------------------------------------------------------------------------------
/img/search-not-found.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samarthdadhaniya/CRUD-Operation-in-LocalStorage/HEAD/img/search-not-found.png
--------------------------------------------------------------------------------
/img/site-demo-image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/samarthdadhaniya/CRUD-Operation-in-LocalStorage/HEAD/img/site-demo-image.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # LocalStorage CRUD Operation
3 |
4 |
5 | - CRUD operation in LocalStorage with search and sort functionality.
6 | - It is a JavaScript program that allows users to create, read, update, and delete items from a productList stored in LocalStorage.
7 | - It also includes search and sort functionality to easily find and organize products.
8 | - Users can add, edit, and delete product details, including name, price, description,and image.
9 | - The program generates an HTML code for each product card and displays them on the webpage.
10 | - This program provides an efficient way to manage and manipulate data in LocalStorage.
11 |
12 |
13 | ## :camera_flash: Screenshots
14 |
15 | 
16 |
17 |
18 |
19 | ## :computer: Tech Stack
20 |
21 | [](https://skillicons.dev)
`;
160 | });
161 | }
162 | document.querySelector("#curd-table").innerHTML = html;
163 | }
164 |
165 | // Load all data when document or page load
166 | showData();
167 |
168 | /**
169 | * @function AddData
170 | *
171 | * @description The function is adds a new product to the localStorage using
172 | * form data, generates a new ID, and reloads the page to display
173 | * the updated data. It also clears the form and displays an alert message.
174 | *
175 | * @param none
176 | */
177 | function AddData() {
178 | if (validateForm() == true) {
179 | let name = document.getElementById("name").value;
180 | let price = document.getElementById("price").value;
181 | let description = document.getElementById("description").value;
182 | let image = document.getElementById("inputGroupFile01");
183 | let reader = new FileReader();
184 |
185 | let productList;
186 | if (localStorage.getItem("productList") == null) {
187 | productList = [];
188 | } else {
189 | productList = JSON.parse(localStorage.getItem("productList"));
190 | }
191 |
192 | // generate new ID by incrementing the highest existing ID
193 | let id = 1;
194 | if (productList.length > 0) {
195 | let ids = productList.map((product) => product.id);
196 | id = Math.max(...ids) + 1;
197 | }
198 |
199 | /**
200 | * @function anonymous-Function (Arrow Function)
201 | *
202 | * @description The function reads the data URL of an image file, and
203 | * adds product details to the productList and stores it
204 | * in localStorage.
205 | *
206 | * @param none
207 | */
208 | reader.readAsDataURL(image.files[0]);
209 | reader.addEventListener("load", () => {
210 | productList.push({
211 | id: id,
212 | name: name,
213 | description: description,
214 | price: price,
215 | image: reader.result,
216 | });
217 | localStorage.setItem("productList", JSON.stringify(productList));
218 | location.reload();
219 | showData();
220 | });
221 |
222 | document.getElementById("name").value = "";
223 | document.getElementById("price").value = "";
224 | document.getElementById("description").value = "";
225 | document.getElementById("inputGroupFile01").value = "";
226 | document.getElementById("close-btn").click();
227 | alert("Data Added Successfully");
228 | }
229 | }
230 |
231 | /**
232 | * @function deleteData
233 | *
234 | * @description This function deletes an item from an array called 'productList'
235 | * and updates local storage. It also displays a confirmation message
236 | * to the user before deleting the item.
237 | *
238 | * @param index
239 | */
240 | function deleteData(index) {
241 | let productList;
242 | if (localStorage.getItem("productList") == null) {
243 | productList = [];
244 | } else {
245 | productList = JSON.parse(localStorage.getItem("productList"));
246 | }
247 |
248 | // Display a confirmation message to the user
249 | if (confirm("Are you sure you want to delete this item?")) {
250 | productList.splice(index, 1);
251 | localStorage.setItem("productList", JSON.stringify(productList));
252 | showData();
253 | location.reload(); // Reload the current page
254 | }
255 | }
256 |
257 | /**
258 | * @function editData
259 | *
260 | * @description -This function edits the details of a product at a specific index. It
261 | * reads the data from local storage and updates the HTML elements with
262 | * the corresponding values. It also adds event handlers for an image and
263 | * a button to update the data.
264 | *
265 | * @param index
266 | */
267 | function editData(index) {
268 | let productList;
269 | if (localStorage.getItem("productList") == null) {
270 | productList = [];
271 | } else {
272 | productList = JSON.parse(localStorage.getItem("productList"));
273 | }
274 |
275 | document.getElementById("id-edit").value = productList[index].id;
276 | document.getElementById("name-edit").value = productList[index].name;
277 | document.getElementById("price-edit").value = productList[index].price;
278 | document.getElementById("description-edit").value =
279 | productList[index].description;
280 |
281 | let imagePreview = document.getElementById("image-div");
282 | imagePreview.src = productList[index].image;
283 | document.getElementById("image-div").innerHTML =
284 | "";
285 |
286 | /**
287 | * @function anonymous-Function (Arrow Function)
288 | *
289 | * @description this function is used When the user selects an image file using the file dialog
290 | * input element, the function reads the contents of the selected file and updates
291 | * the image property of a iten object at a specific index in an array called productList.
292 | *
293 | * @param event
294 | */
295 | let imageEdit = document.getElementById("image-edit");
296 | imageEdit.onchange = function (event) {
297 | let file = event.target.files[0];
298 | let reader = new FileReader();
299 | reader.onload = function () {
300 | productList[index].image = reader.result;
301 | imagePreview.src = reader.result;
302 | };
303 | reader.readAsDataURL(file);
304 | };
305 |
306 | /**
307 | * @function anonymous-Function (Arrow Function)
308 | *
309 | * @description The code defines an event handler function for the onclick event of an
310 | * HTML element with an id of "update". When the user clicks on this element,
311 | * the function performs a series of actions related to updating a item's details,
312 | * and store updated details into localstorage.
313 | *
314 | * @param none
315 | */
316 | document.querySelector("#update").onclick = function () {
317 | productList[index].id = document.getElementById("id-edit").value;
318 | productList[index].name = document.getElementById("name-edit").value;
319 | productList[index].price = document.getElementById("price-edit").value;
320 | productList[index].description = document.getElementById("description-edit").value;
321 | // this line is used to convert the array to a JSON string before it is saved to local storage.
322 | localStorage.setItem("productList", JSON.stringify(productList));
323 | // The is method, which refreshes the page with the updated data.
324 | location.reload();
325 |
326 | showData();
327 | document.getElementById("id-edit").value = "";
328 | document.getElementById("name-edit").value = "";
329 | document.getElementById("price-edit").value = "";
330 | document.getElementById("description-edit").value = "";
331 | document.getElementById("close-btn").click();
332 | alert("Data Updated Successfully");
333 | };
334 | }
335 |
336 | /**
337 | * @function searchBar
338 | *
339 | * @description This function is designed to search for products in a Arraylist
340 | * based on user input. It first gets the search value from an input
341 | * field with an id of "searchProductText". It then give an array of
342 | * item from the local storage using the key "productList".
343 | *
344 | * @param none
345 | */
346 | function searchBar() {
347 | let searchvalue = document.querySelector("#serachProductText").value;
348 | console.log(searchvalue);
349 | let sortedItem = [];
350 | let sortedProduct = JSON.parse(localStorage.getItem("productList")) ?? [];
351 | let regex = new RegExp(searchvalue, "i");
352 | for (let element of sortedProduct) {
353 | let item = element;
354 | if (regex.test(item.name)) {
355 | sortedItem.push(element);
356 | }
357 | }
358 | console.log(sortedItem);
359 | searchProduct(sortedItem);
360 | }
361 |
362 | /**
363 | * @function searchProduct
364 | *
365 | * @description This function is generates HTML code to display search
366 | * results for items. If there are no results, it displays an
367 | * image and a error message. Otherwise, it generates a card
368 | * for each product that matches the search query,
369 | *
370 | * @param sortedItem (a array format)
371 | */
372 | function searchProduct(sortedItem) {
373 | let html = "";
374 | console.log("searchProduct", sortedItem);
375 | if (sortedItem.length === 0) {
376 | // Display an image if the productList array is empty
377 | html += `