1+ var productName = document . getElementById ( 'productName' ) ;
2+ var productCategory = document . getElementById ( 'productCategory' ) ;
3+ var productPrice = document . getElementById ( 'productPrice' ) ;
4+ var productDescription = document . getElementById ( 'ProductDescription' ) ;
5+ var products = [ ] ;
6+ if ( localStorage . getItem ( 'products' , products ) == null )
7+ {
8+ products = [ ] ;
9+ }
10+ else
11+ {
12+ products = JSON . parse ( localStorage . getItem ( 'products' , products ) ) ;
13+ display ( ) ;
14+ }
15+ function getData ( ) {
16+ var product = {
17+ name :productName . value ,
18+ category :productCategory . value ,
19+ price :productPrice . value ,
20+ description :productDescription . value
21+ } ;
22+ products . push ( product ) ;
23+ localStorage . setItem ( 'products' , JSON . stringify ( products ) ) ;
24+ display ( ) ;
25+ }
26+ function display ( ) {
27+ var x = "" ;
28+ for ( var i = 0 ; i < products . length ; i ++ )
29+ {
30+ x += `<tr>
31+ <td>${ i + 1 } </td>
32+ <td>${ products [ i ] . name } </td>
33+ <td>${ products [ i ] . category } </td>
34+ <td>${ products [ i ] . price } </td>
35+ <td>${ products [ i ] . description } </td>
36+ <td><button onclick="update(${ i } )" class="btn bg-info w-100 p-2">Update</button></td>
37+ <td><button onclick="del(${ i } )" class="btn bg-danger w-100 p-2">Delete</button></td>
38+ </tr>` ;
39+ }
40+ document . getElementById ( 'demo' ) . innerHTML = x ;
41+ }
42+
43+ function del ( index ) {
44+ products . splice ( index , 1 ) ;
45+ localStorage . setItem ( 'products' , JSON . stringify ( products ) ) ;
46+ display ( ) ;
47+ }
48+ function clr ( ) {
49+ productName . value = "" ;
50+ productCategory . value = "" ;
51+ productPrice . value = "" ;
52+ productDescription . value = "" ;
53+ }
54+ function update ( index )
55+ {
56+ document . getElementById ( 'add' ) . style . display = "none" ;
57+ document . getElementById ( 'cont' ) . innerHTML = `<button onclick="updateProduct(${ index } )" class="w-100 btn btn-info my-5">Update</button>`
58+ productName . value = products [ index ] . name ;
59+ productCategory . value = products [ index ] . category ;
60+ productPrice . value = products [ index ] . price ;
61+ productDescription . innerHTML = products [ index ] . description ;
62+ }
63+ function updateProduct ( index )
64+ {
65+ products [ index ] . name = productName . value ;
66+ products [ index ] . category = productCategory . value ;
67+ products [ index ] . price = productPrice . value ;
68+ products [ index ] . description = productDescription . value ;
69+ localStorage . setItem ( 'products' , JSON . stringify ( products ) ) ;
70+ display ( ) ;
71+ clr ( ) ;
72+ document . getElementById ( 'add' ) . style . display = "block" ;
73+ document . getElementById ( 'cont' ) . innerHTML = '' ;
74+ }
75+
76+ function search ( searchIn ) {
77+ var searchResult = '' ;
78+ for ( var i = 0 ; i < products . length ; i ++ )
79+ {
80+ if ( products [ i ] . name . toLowerCase ( ) . includes ( searchIn . toLowerCase ( ) ) )
81+ {
82+ searchResult += `<tr>
83+ <td>${ i + 1 } </td>
84+ <td>${ products [ i ] . name . replace ( searchIn , `<span>${ searchIn } </span>` ) } </td>
85+ <td>${ products [ i ] . category } </td>
86+ <td>${ products [ i ] . price } </td>
87+ <td>${ products [ i ] . description } </td>
88+ <td><button onclick="update(${ i } )" class="btn bg-info w-100 p-2">Update</button></td>
89+ <td><button onclick="del(${ i } )" class="btn bg-danger w-100 p-2">Delete</button></td>
90+ </tr>` ;
91+ }
92+ }
93+ document . getElementById ( 'demo' ) . innerHTML = searchResult ;
94+ }
0 commit comments