vueClassrooms.js 3.2 KB

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