Bonsoir,
J'ai mon application qui fonctionne avec CRUD mes j'ai quelques soucis quand je créer ou je fais un update ou delete, sa fonctionne il rajoute bien mes données mes il ne m'affiche pas par exemple le message " data inserted successfully...! " quand je fais un create avez vous une idée ?
index.html :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Electronic Store</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" /> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous" /> <link rel="stylesheet" href="./style.css" /> </head> <body> <main> <div class="container text-center"> <h1 class="bg-light py-4 text-info"> <i class="fas fa-plug"></i> Electronic Store </h1> <div class="d-flex justify-content-center"> <form class="w-50"> <input type="text" id="userid" class="form-control" autocomplete="off" readonly placeholder="ID" /> <input type="text" id="proname" class="form-control" autocomplete="off" placeholder="Product Name" /> <div class="row"> <div class="col"> <input type="text" id="seller" class="form-control m-0" autocomplete="off" placeholder="Seller" /> </div> <div class="col"> <input type="text" id="price" class="form-control m-0" autocomplete="off" placeholder="Price" /> </div> </div> </form> </div> <div class="d-flex justify-content-center"> <button class="btn btn-success" id="btn-create">Create</button> <button class="btn btn-primary" id="btn-read">Read</button> <button class="btn btn-warning" id="btn-update">Update</button> <button class="btn btn-danger" id="btn-delete">Delete All</button> </div> <!-- Table --> <div class="d-flex table-data"> <table class="table table-striped"> <thead> <tr> <th scope="col">ID</th> <th scope="col">Product Name</th> <th scope="col">Seller</th> <th scope="col">Price</th> <th scope="col">Edit</th> <th scope="col">Delete</th> </tr> </thead> <tbody id="tbody"> <!-- <tr> <th scope="row">1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope="row">2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> --> </tbody> </table> </div> <div id="notfound"></div> </div> </main> <!-- Labels --> <!--<div class="w-100 btn btn-success insertmsg">Data Inserted Successfully...!</div> <div class="w-100 btn btn-warning updatemsg">Data Updated..! Refresh Database To See Result</div> <div class="w-100 btn btn-danger deletemsg">Data Deleted..!</div> --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"> </script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"> </script> <!-- Include Dexie --> <script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script> <!-- Custom main file--> <script src="../js/main.js" type="module"></script> </body> </html>
main.js
module.jsCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 import prodb, { bulkcreate, createEle, getData, SortObj } from "./module.js"; let db = prodb("Productdb", { products: `++id, name, seller, price` }); // input tags const userid = document.getElementById("userid"); const proname = document.getElementById("proname"); const seller = document.getElementById("seller"); const price = document.getElementById("price"); // create button const btncreate = document.getElementById("btn-create"); const btnread = document.getElementById("btn-read"); const btnupdate = document.getElementById("btn-update"); const btndelete = document.getElementById("btn-delete"); // user data // event listerner for create button btncreate.onclick = event => { // insert values let flag = bulkcreate(db.products, { name: proname.value, seller: seller.value, price: price.value }); //console.log(flag); // reset textbox values //proname.value = ""; //seller.value = ""; // price.value = ""; proname.value = seller.value = price.value = ""; // set id textbox value getData(db.products, data => { userid.value = data.id + 1 || 1; }); table(); let insertmsg = document.querySelector(".insertmsg"); getMsg(flag, insertmsg); }; // event listerner for create button btnread.onclick = table; // button update btnupdate.onclick = () => { const id = parseInt(userid.value || 0); if (id) { // call dexie update method db.products.update(id, { name: proname.value, seller: seller.value, price: price.value }).then((updated) => { // let get = updated ? `data updated` : `couldn't update data`; let get = updated ? true : false; // display message let updatemsg = document.querySelector(".updatemsg"); getMsg(get, updatemsg); proname.value = seller.value = price.value = ""; //console.log(get); }) } else { console.log(`Please Select id: ${id}`); } } // delete button btndelete.onclick = () => { db.delete(); db = prodb("Productdb", { products: `++id, name, seller, price` }); db.open(); table(); textID(userid); // display message let deletemsg = document.querySelector(".deletemsg"); getMsg(true, deletemsg); } window.onload = event => { // set id textbox value textID(userid); }; // create dynamic table function table() { const tbody = document.getElementById("tbody"); const notfound = document.getElementById("notfound"); notfound.textContent = ""; // remove all childs from the dom first while (tbody.hasChildNodes()) { tbody.removeChild(tbody.firstChild); } getData(db.products, (data, index) => { if (data) { createEle("tr", tbody, tr => { for (const value in data) { createEle("td", tr, td => { td.textContent = data.price === data[value] ? `$ ${data[value]}` : data[value]; }); } createEle("td", tr, td => { createEle("i", td, i => { i.className += "fas fa-edit btnedit"; i.setAttribute(`data-id`, data.id); // store number of edit buttons i.onclick = editbtn; }); }) createEle("td", tr, td => { createEle("i", td, i => { i.className += "fas fa-trash-alt btndelete"; i.setAttribute(`data-id`, data.id); // store number of edit buttons i.onclick = deletebtn; }); }) }); } else { notfound.textContent = "No record found in the database...!"; } }); } const editbtn = (event) => { let id = parseInt(event.target.dataset.id); db.products.get(id, function (data) { let newdata = SortObj(data); userid.value = newdata.id || 0; proname.value = newdata.name || ""; seller.value = newdata.seller || ""; price.value = newdata.price || ""; }); } // delete icon remove element const deletebtn = event => { let id = parseInt(event.target.dataset.id); db.products.delete(id); table(); } // textbox id function textID(textboxid) { getData(db.products, data => { textboxid.value = data.id + 1 || 1; }); } // function msg function getMsg(flag, element) { if (flag) { // call msg element.className += " movedown"; setTimeout(() => { element.classList.forEach(classname => { classname == "movedown" ? undefined : element.classList.remove('movedown'); }) }, 4000); } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 const productsdb = (dbname, table) => { const db = new Dexie(dbname); db.version(1).stores(table); db.open(); return db; /** * const db = new Dexie('myDb'); db.version(1).stores({ friends: `name, age` }); */ }; const bulkcreate = (dbtable, data) => { let flag = empty(data); if (flag) { dbtable.bulkAdd([data]); console.log("data inserted successfully...!"); } else { console.log("Please provide data...!"); } return flag; }; // create dynamic elements const createEle = (tagname, appendTo, fn) => { const element = document.createElement(tagname); if (appendTo) appendTo.appendChild(element); if (fn) fn(element); }; // check textbox validation const empty = object => { let flag = false; for (const value in object) { if (object[value] != "" && object.hasOwnProperty(value)) { flag = true; } else { flag = false; } } return flag; }; // getData from the database const getData = (dbname, fn) => { let index = 0; let obj = {}; dbname.count(count => { // count rows in the table using count method if (count) { dbname.each(table => { // table => return the table object data // to arrange order we are going to create for in loop obj = SortObj(table); fn(obj, index++); // call function with data argument }); } else { fn(0); } }); }; const SortObj = (sortobj) => { let obj = {}; obj = { id: sortobj.id, name: sortobj.name, seller: sortobj.seller, price: sortobj.price }; return obj; } export default productsdb; export { bulkcreate, createEle, getData, SortObj };