vueClassrooms.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 app = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. classrooms:[],
  29. buildings: [],
  30. selectedRow: {
  31. "Code": "",
  32. "Name": "",
  33. "IDDuration": 0,
  34. "ID": ""
  35. },
  36. },
  37. methods: {
  38. showClassrooms() {
  39. console.log(this.classrooms)
  40. },
  41. selectRow(row, index){
  42. let rows = document.querySelectorAll('.clickHover')
  43. console.log(index)
  44. this.selectedRow = row;
  45. // for(let i = 0; i < row.length; i++) {
  46. // rows[i].style.backgroundColor = 'red'
  47. // }
  48. console.log(rows)
  49. console.log(rows[index].style.backgroundColor)
  50. // empty string
  51. if(rows[index].style.backgroundColor != 'red'){
  52. rows[index].style.backgroundColor = 'red'
  53. }
  54. else{
  55. rows[index].style.backgroundColor = 'white'
  56. this.clearSelectedRow()
  57. }
  58. console.log(row)
  59. },
  60. clearSelectedRow(){
  61. this.selectedRow = {
  62. "Code": "",
  63. "Name": "",
  64. "IDDuration": 0
  65. }
  66. },
  67. async getClassrooms(){
  68. this.classrooms = await requestFunc("/classroom", "GET")
  69. },
  70. async getBuildings() {
  71. const buildings = await requestFunc("/building/", "GET")
  72. .then(response => {
  73. console.log(response)
  74. this.buildings = response
  75. })
  76. },
  77. },
  78. mounted() {
  79. this.getClassrooms()
  80. this.getBuildings()
  81. }
  82. });
  83. addClassroom = () =>{
  84. let number = document.querySelector('.numberCab').value
  85. let placeQuantity = document.querySelector('.placeQuantity').value
  86. let building = document.querySelectorAll('.building-radio');
  87. let radioValue
  88. for(let i=0; i<building.length; i++ ){
  89. if (building[i].checked) {
  90. radioValue = building[i].value
  91. }
  92. }
  93. let isComputer = document.querySelector('.computerclass').checked
  94. var myHeaders = new Headers();
  95. myHeaders.append("Content-Type", "application/json");
  96. var raw = {"PlaceQuantity":Number(placeQuantity),"IsComputer":isComputer,"IDBuilding":Number(radioValue),"Name": number};
  97. requestFunc(url="/classroom/", method="POST", data=raw)
  98. alert("Добавлено")
  99. }