vueGroup.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://127.0.0.1/api'
  3. method = method.toLocaleUpperCase()
  4. let fullurl = `${apihost}${url}`;
  5. let options = {
  6. method: method,
  7. headers: {
  8. "Content-Type": "application/json",
  9. "Authorization": `Bearer ${token}`,
  10. },
  11. };
  12. switch(method) {
  13. case "PUT":
  14. delete options.headers["Content-Type"];
  15. options.body = data;
  16. break;
  17. case "POST": case "PATCH": case "DELETE":
  18. options.body = JSON.stringify(data);
  19. break;
  20. }
  21. const res = await fetch(fullurl, options);
  22. return await res.json();
  23. };
  24. var vueapp = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. groups:[],
  29. teachers:[],
  30. specialties:[],
  31. selectedRow:{
  32. "ID": "",
  33. "Number":"",
  34. "IDSpecialty": 0,
  35. "IDTeacher": 0,
  36. "IsBudget": "",
  37. "Year": ""
  38. },
  39. },
  40. methods: {
  41. async getGroups(){
  42. this.groups = await requestFunc("/group/", "GET")
  43. },
  44. async getTeachers(){
  45. teachers = await requestFunc("/teacher/", "GET")
  46. for(let i =0; i < teachers.length; i++) {
  47. Vue.set(this.teachers, i, teachers[i]);
  48. }
  49. },
  50. async getSpecilties(){
  51. specialties = await requestFunc("/specialty/", "GET")
  52. for(let i =0; i < specialties.length; i++) {
  53. Vue.set(this.specialties, i, specialties[i]);
  54. }
  55. },
  56. async mountFunc(){
  57. await this.getTeachers()
  58. await this.getGroups()
  59. await this.getSpecilties()
  60. },
  61. selectRow(row, index){
  62. let rows = document.querySelectorAll('.clickHover')
  63. this.selectedRow = row;
  64. if(rows[index].style.backgroundColor != 'red'){
  65. rows[index].style.backgroundColor = 'red'
  66. }
  67. else{
  68. rows[index].style.backgroundColor = 'white'
  69. this.clearSelectedRow()
  70. }
  71. },
  72. clearSelectedRow(){
  73. this.selectedRow = {
  74. "ID": "",
  75. "Number":"",
  76. "IDSpecialty": 0,
  77. "IDTeacher": 0,
  78. "IsBudget": "",
  79. "Year": ""
  80. }
  81. },
  82. changeRequestType(requestType){
  83. this.requestType = requestType
  84. },
  85. modalClick(add, requestType) {
  86. if(add==='yes') {
  87. this.clearSelectedRow();
  88. }
  89. this.changeRequestType(requestType);
  90. },
  91. },
  92. async mounted() {
  93. await this.mountFunc()
  94. console.log(this.teachers)
  95. }
  96. });
  97. addSubject = (requestType) =>{
  98. let name = document.querySelector('.name').value
  99. let patronymic = document.querySelector('.patronymic').value
  100. var select = document.getElementById("selectClassroom");
  101. var value = select.value;
  102. var myHeaders = new Headers();
  103. myHeaders.append("Content-Type", "application/json");
  104. var raw = {"Surname":surname,"Name":name,"IDCLassroom":Number(value),"Patronymic": patronymic};
  105. requestFunc(url="/teacher/", method="POST", data=raw)
  106. alert("Добавлено")
  107. }