Blame view
src/pages/Administrator/bulkImport.vue
17.9 KB
1c69557ac
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<template> <v-container fluid grid-list-md> <v-card flat> <v-toolbar class="fixcolors" dark flat> <v-spacer></v-spacer> <v-toolbar-title>Import</v-toolbar-title> <v-spacer></v-spacer> </v-toolbar> <v-container fluid> <v-layout> <v-flex xs12> <v-layout wrap> <v-flex xs6 sm5 lg2 class="pt-4"> <label class="title right">Add Teacher:</label> </v-flex> <v-flex xs12 sm6 lg3> <v-text-field v-model="teacherFileName" placeholder="Choose file" :disabled="true"></v-text-field> <input type="file" style="display: none" ref="file" |
6fe35f887
|
22 23 |
@change="convertFile" id="fileInput" |
1c69557ac
|
24 25 26 27 28 29 30 31 |
/> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> <v-btn class="grey" dark block @click="pickFile"> <v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
6fe35f887
|
32 |
<v-btn class="grey" dark block @click="importTeacher()" :loading="loading">import</v-btn> |
1c69557ac
|
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
</v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <download-csv :data="teacherData"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </download-csv> </v-flex> </v-layout> </v-flex> </v-layout> <v-layout> <v-flex xs12> <v-layout wrap> <v-flex xs6 sm5 lg2 class="pt-4"> <label class="title right">Add Parent:</label> </v-flex> <v-flex xs12 sm6 lg3> <v-text-field v-model="parentFileName" placeholder="Choose file" :disabled="true"></v-text-field> |
6fe35f887
|
52 53 54 55 56 57 58 |
<input type="file" style="display: none" ref="parentfile" @change="parentFile" id="parentInput" /> |
1c69557ac
|
59 60 |
</v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
6fe35f887
|
61 |
<v-btn class="grey" dark block @click="pickParentFile"> |
1c69557ac
|
62 63 64 |
<v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> |
748acd1a6
|
65 |
<v-flex xs6 sm4 lg2 class="mt-1" @click="importParent" :loading="parentLoading"> |
1c69557ac
|
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<v-btn class="grey" dark block>import</v-btn> </v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <download-csv :data="parentData"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </download-csv> </v-flex> </v-layout> </v-flex> </v-layout> <v-layout> <v-flex xs12> <v-layout wrap> <v-flex xs6 sm5 lg2 class="pt-4"> <label class="title right">Add User:</label> </v-flex> <v-flex xs12 sm6 lg3> |
6fe35f887
|
85 86 87 88 89 90 91 92 |
<v-text-field placeholder="Choose file" v-model="userFileName" :disabled="true"></v-text-field> <input type="file" style="display: none" ref="userfile" @change="userFile" id="userInput" /> |
1c69557ac
|
93 94 |
</v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
6fe35f887
|
95 |
<v-btn class="grey" dark block @click="pickUserFile"> |
1c69557ac
|
96 97 98 99 |
<v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
748acd1a6
|
100 |
<v-btn class="grey" dark block @click="importUser" :loading="userLoading">import</v-btn> |
1c69557ac
|
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
</v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <download-csv :data="userData"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </download-csv> </v-flex> </v-layout> </v-flex> </v-layout> <v-layout> <v-flex xs12> <v-layout wrap> <v-flex xs6 sm5 lg2 class="pt-4"> <label class="title right">Add Book:</label> </v-flex> <v-flex xs12 sm6 lg3> |
6fe35f887
|
119 120 121 122 123 124 125 126 |
<v-text-field placeholder="Choose file" v-model="bookFileName" :disabled="true"></v-text-field> <input type="file" style="display: none" ref="bookfile" @change="bookFile" id="bookInput" /> |
1c69557ac
|
127 128 |
</v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
6fe35f887
|
129 |
<v-btn class="grey" dark block @click="pickBookFile"> |
1c69557ac
|
130 131 132 133 |
<v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
748acd1a6
|
134 |
<v-btn class="grey" dark block @click="importBook" :loading="BookLoading">import</v-btn> |
1c69557ac
|
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
</v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <download-csv :data="bookData"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </download-csv> </v-flex> </v-layout> </v-flex> </v-layout> <v-layout> <v-flex xs12> <v-layout wrap> <v-flex xs6 sm5 lg2 class="pt-4"> <label class="title right">Add Student:</label> </v-flex> <v-flex xs12 sm6 lg3> |
f9b486fe1
|
153 154 155 156 157 158 159 160 |
<v-text-field placeholder="Choose file" v-model="bookStudentName" :disabled="true"></v-text-field> <input type="file" style="display: none" ref="studentfile" @change="studentFile" id="studentInput" /> |
1c69557ac
|
161 162 |
</v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
f9b486fe1
|
163 |
<v-btn class="grey" dark block @click="pickStudentFile"> |
1c69557ac
|
164 165 166 167 |
<v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> |
748acd1a6
|
168 169 170 171 172 173 174 |
<v-btn class="grey" dark block @click="importStudent()" :loading="studentLoading" >import</v-btn> |
1c69557ac
|
175 176 177 178 179 180 181 182 183 184 185 |
</v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <download-csv :data="studentData"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </download-csv> </v-flex> </v-layout> </v-flex> </v-layout> |
f9b486fe1
|
186 |
<!-- <v-layout> |
1c69557ac
|
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
<v-flex xs12> <v-layout wrap> <v-flex xs9 sm5 lg2 class="pt-4"> <label class="title right">Students & Parents:</label> </v-flex> <v-flex xs12 sm6 lg3> <v-text-field placeholder="Choose file" :disabled="true"></v-text-field> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> <v-btn class="grey" dark block> <v-icon small class="pr-1">refresh</v-icon>Upload </v-btn> </v-flex> <v-flex xs6 sm4 lg2 class="mt-1"> <v-btn class="grey" dark block>import</v-btn> </v-flex> <v-flex xs12 sm4 lg2 class="mt-1"> <v-btn class="blue lighten-2" dark block> <v-icon small class="pr-1">archive</v-icon>sample download </v-btn> </v-flex> </v-layout> </v-flex> |
f9b486fe1
|
210 |
</v-layout>--> |
1c69557ac
|
211 212 |
</v-container> </v-card> |
f9b486fe1
|
213 214 215 216 217 218 |
<v-snackbar :timeout="timeout" :top="y === 'top'" :right="x === 'right'" :vertical="mode === 'vertical'" v-model="snackbar" |
748acd1a6
|
219 |
:color="color" |
f9b486fe1
|
220 |
>{{ text }}</v-snackbar> |
1c69557ac
|
221 222 223 224 225 226 227 228 229 230 231 232 |
</v-container> </template> <script> import http from "@/Services/http.js"; import moment from "moment"; import teacherData from "@/script/teachers.js"; import studentData from "@/script/students.js"; import parentData from "@/script/parents.js"; import userData from "@/script/users.js"; import bookData from "@/script/books.js"; |
748acd1a6
|
233 |
import XLSX from "xlsx"; |
1c69557ac
|
234 235 236 |
export default { data: () => ({ |
f9b486fe1
|
237 |
loading: false, |
748acd1a6
|
238 239 240 241 |
parentLoading: false, userLoading: false, BookLoading: false, studentLoading: false, |
f9b486fe1
|
242 243 244 245 246 247 248 |
snackbar: false, y: "top", x: "right", mode: "", timeout: 3000, text: "", color: "", |
1c69557ac
|
249 250 251 252 253 |
teacherData: [], studentData: [], parentData: [], userData: [], bookData: [], |
6fe35f887
|
254 255 256 257 |
teacherFileJson: [], parentFileJson: [], userFileJson: [], bookFileJson: [], |
f9b486fe1
|
258 |
studentFileJson: [], |
1c69557ac
|
259 |
teacherFileName: "", |
1c69557ac
|
260 |
parentFileName: "", |
6fe35f887
|
261 262 |
userFileName: "", bookFileName: "", |
f9b486fe1
|
263 264 |
bookStudentName: "", token: "" |
1c69557ac
|
265 266 267 268 269 |
}), methods: { pickFile() { this.$refs.file.click(); }, |
6fe35f887
|
270 271 272 273 274 275 276 277 278 |
pickParentFile() { this.$refs.parentfile.click(); }, pickUserFile() { this.$refs.userfile.click(); }, pickBookFile() { this.$refs.bookfile.click(); }, |
f9b486fe1
|
279 280 281 |
pickStudentFile() { this.$refs.studentfile.click(); }, |
748acd1a6
|
282 |
convertFile(e) { |
6fe35f887
|
283 |
const input = document.getElementById("fileInput"); |
6fe35f887
|
284 |
this.teacherFileName = input.files[0].name; |
748acd1a6
|
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
// const reader = new FileReader(); // console.log("reader", reader); // reader.onload = () => { // let text = reader.result; // //convert text to json here // console.log("text", text); // var json = this.csvJSON(text); // }; // reader.readAsText(input.files[0]); var files = e.target.files, f = files[0]; var reader = new FileReader(); let array = []; let _this = this; reader.onload = function(e) { var data = new Uint8Array(e.target.result); var workbook = XLSX.read(data, { type: "array" }); let sheetName = workbook.SheetNames[0]; // /* DO SOMETHING WITH workbook HERE */ let worksheet = workbook.Sheets[sheetName]; array = XLSX.utils.sheet_to_json(worksheet); _this.teacherFileJson = array; }; reader.readAsArrayBuffer(f); |
6fe35f887
|
309 |
}, |
748acd1a6
|
310 |
parentFile(e) { |
6fe35f887
|
311 |
const input = document.getElementById("parentInput"); |
6fe35f887
|
312 |
this.parentFileName = input.files[0].name; |
748acd1a6
|
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
console.log("this.parentFileName", this.parentFileName); var files = e.target.files, f = files[0]; var reader = new FileReader(); let array = []; let _this = this; reader.onload = function(e) { var data = new Uint8Array(e.target.result); var workbook = XLSX.read(data, { type: "array" }); let sheetName = workbook.SheetNames[0]; // /* DO SOMETHING WITH workbook HERE */ let worksheet = workbook.Sheets[sheetName]; array = XLSX.utils.sheet_to_json(worksheet); _this.parentFileJson = array; }; reader.readAsArrayBuffer(f); |
6fe35f887
|
329 |
}, |
748acd1a6
|
330 |
userFile(e) { |
6fe35f887
|
331 |
const input = document.getElementById("userInput"); |
6fe35f887
|
332 |
this.userFileName = input.files[0].name; |
748acd1a6
|
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
var files = e.target.files, f = files[0]; var reader = new FileReader(); let array = []; let _this = this; reader.onload = function(e) { var data = new Uint8Array(e.target.result); var workbook = XLSX.read(data, { type: "array" }); let sheetName = workbook.SheetNames[0]; // /* DO SOMETHING WITH workbook HERE */ let worksheet = workbook.Sheets[sheetName]; array = XLSX.utils.sheet_to_json(worksheet); _this.userFileJson = array; }; reader.readAsArrayBuffer(f); |
6fe35f887
|
348 |
}, |
748acd1a6
|
349 |
bookFile(e) { |
6fe35f887
|
350 |
const input = document.getElementById("bookInput"); |
6fe35f887
|
351 |
this.bookFileName = input.files[0].name; |
748acd1a6
|
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
console.log("this.bookFileName", this.bookFileName); var files = e.target.files, f = files[0]; console.log("this.bookFileName", files); var reader = new FileReader(); console.log("reader================", reader); let array = []; let _this = this; reader.onload = function(e) { console.log("data", e.target.result); var data = new Uint8Array(e.target.result); console.log("data", data); var workbook = XLSX.read(data, { type: "array" }); let sheetName = workbook.SheetNames[0]; // /* DO SOMETHING WITH workbook HERE */ let worksheet = workbook.Sheets[sheetName]; array = XLSX.utils.sheet_to_json(worksheet); _this.bookFileJson = array; console.log("this.bookFileJson ", _this.bookFileJson); }; reader.readAsArrayBuffer(f); |
6fe35f887
|
373 |
}, |
748acd1a6
|
374 |
studentFile(e) { |
f9b486fe1
|
375 |
const input = document.getElementById("studentInput"); |
f9b486fe1
|
376 |
this.bookStudentName = input.files[0].name; |
748acd1a6
|
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
var files = e.target.files, f = files[0]; var reader = new FileReader(); let array = []; let _this = this; reader.onload = function(e) { var data = new Uint8Array(e.target.result); var workbook = XLSX.read(data, { type: "array" }); let sheetName = workbook.SheetNames[0]; // /* DO SOMETHING WITH workbook HERE */ let worksheet = workbook.Sheets[sheetName]; array = XLSX.utils.sheet_to_json(worksheet); _this.studentFileJson = array; }; reader.readAsArrayBuffer(f); |
f9b486fe1
|
392 |
}, |
748acd1a6
|
393 394 395 |
// csvJSON(csv) { // var lines = csv.split(" "); |
f9b486fe1
|
396 |
|
748acd1a6
|
397 398 |
// var result = []; // var headers = lines[0].split(","); |
f9b486fe1
|
399 |
|
748acd1a6
|
400 401 402 |
// for (var i = 1; i < lines.length; i++) { // var obj = {}; // var currentline = lines[i].split(","); |
f9b486fe1
|
403 |
|
748acd1a6
|
404 405 406 407 408 409 |
// for (var j = 0; j < headers.length; j++) { // obj[headers[j]] = currentline[j]; // } // this.teacherFileJson.push(obj); // } // }, |
6fe35f887
|
410 411 412 413 414 415 416 417 418 419 420 |
importTeacher() { this.loading = true; var teacherfile = {}; teacherfile.teacherData = this.teacherFileJson; http() .post("/importTeacherData", teacherfile, { headers: { Authorization: "Bearer " + this.token } }) .then(response => { this.snackbar = true; this.text = response.data.message; |
f9b486fe1
|
421 |
this.color = "green"; |
6fe35f887
|
422 423 424 425 426 |
this.loading = false; }) .catch(error => { this.snackbar = true; this.text = error.response.data.message; |
f9b486fe1
|
427 |
this.color = "red"; |
6fe35f887
|
428 429 430 431 |
this.loading = false; }); }, importParent() { |
748acd1a6
|
432 |
this.parentLoading = true; |
6fe35f887
|
433 434 435 436 437 438 439 440 441 |
var parentfile = {}; parentfile.parentData = this.parentFileJson; http() .post("/importParentData", parentfile, { headers: { Authorization: "Bearer " + this.token } }) .then(response => { this.snackbar = true; this.text = response.data.message; |
f9b486fe1
|
442 |
this.color = "green"; |
748acd1a6
|
443 |
this.parentLoading = false; |
6fe35f887
|
444 445 446 447 |
}) .catch(error => { this.snackbar = true; this.text = error.response.data.message; |
748acd1a6
|
448 |
this.parentLoading = false; |
f9b486fe1
|
449 |
this.color = "red"; |
6fe35f887
|
450 451 452 |
}); }, importUser() { |
748acd1a6
|
453 |
this.userLoading = true; |
6fe35f887
|
454 455 456 457 458 459 460 461 462 |
var userfile = {}; userfile.userData = this.userFileJson; http() .post("/importUserData", userfile, { headers: { Authorization: "Bearer " + this.token } }) .then(response => { this.snackbar = true; this.text = response.data.message; |
f9b486fe1
|
463 |
this.color = "green"; |
748acd1a6
|
464 |
this.userLoading = false; |
6fe35f887
|
465 466 467 468 |
}) .catch(error => { this.snackbar = true; this.text = error.response.data.message; |
748acd1a6
|
469 |
this.userLoading = false; |
f9b486fe1
|
470 |
this.color = "red"; |
6fe35f887
|
471 472 473 |
}); }, importBook() { |
748acd1a6
|
474 |
this.BookLoading = true; |
6fe35f887
|
475 476 477 478 479 480 481 482 483 |
var bookfile = {}; bookfile.bookData = this.bookFileJson; http() .post("/importBookData", bookfile, { headers: { Authorization: "Bearer " + this.token } }) .then(response => { this.snackbar = true; this.text = response.data.message; |
f9b486fe1
|
484 |
this.color = "green"; |
748acd1a6
|
485 |
this.BookLoading = false; |
f9b486fe1
|
486 487 488 489 490 |
}) .catch(error => { this.snackbar = true; this.text = error.response.data.message; this.color = "red"; |
748acd1a6
|
491 |
this.BookLoading = false; |
f9b486fe1
|
492 493 494 |
}); }, importStudent() { |
748acd1a6
|
495 |
this.studentLoading = true; |
f9b486fe1
|
496 497 498 499 500 501 502 503 504 505 |
var studentfile = {}; studentfile.studentData = this.studentFileJson; http() .post("/importStudentData", studentfile, { headers: { Authorization: "Bearer " + this.token } }) .then(response => { this.snackbar = true; this.text = response.data.message; this.color = "green"; |
748acd1a6
|
506 |
this.studentLoading = false; |
6fe35f887
|
507 508 509 510 |
}) .catch(error => { this.snackbar = true; this.text = error.response.data.message; |
f9b486fe1
|
511 |
this.color = "red"; |
748acd1a6
|
512 |
this.studentLoading = false; |
6fe35f887
|
513 |
}); |
1c69557ac
|
514 515 516 |
} }, mounted() { |
6fe35f887
|
517 |
this.token = this.$store.state.token; |
f9b486fe1
|
518 |
|
1c69557ac
|
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
/** TEACHERS SAMPLE CSV */ const getTeacherData = teacherData(); this.teacherData = getTeacherData; /** STUDENTS SAMPLE CSV */ const getstudentData = studentData(); this.studentData = getstudentData; /** PARENTS SAMPLE CSV */ const getparentData = parentData(); this.parentData = getparentData; /** USERS SAMPLE CSV */ const getUserData = userData(); this.userData = getUserData; /** BOOKS SAMPLE CSV */ const getBookData = bookData(); this.bookData = getBookData; } }; |
6fe35f887
|
540 |
</script> |