vueSubject.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. subjects:[],
  29. selectedRow: {
  30. "Name": "",
  31. "ID_professionalmodule": 0,
  32. "Shortname": "",
  33. "IDStype":0,
  34. "ID": ""
  35. },
  36. },
  37. methods: {
  38. async getSubjects(){
  39. this.subjects = await requestFunc("/subject/", "GET")
  40. },
  41. selectRow(row, index){
  42. let rows = document.querySelectorAll('.clickHover')
  43. this.selectedRow = row;
  44. if(rows[index].style.backgroundColor != 'red'){
  45. rows[index].style.backgroundColor = 'red'
  46. }
  47. else{
  48. rows[index].style.backgroundColor = 'white'
  49. this.clearSelectedRow()
  50. }
  51. },
  52. clearSelectedRow(){
  53. this.selectedRow = {
  54. "Name": "",
  55. "ID_professionalmodule": 0,
  56. "Shortname": "",
  57. "IDStype":0,
  58. "ID": ""
  59. }
  60. },
  61. changeRequestType(requestType){
  62. this.requestType = requestType
  63. },
  64. modalClick(add, requestType) {
  65. if(add==='yes') {
  66. this.clearSelectedRow();
  67. }
  68. this.changeRequestType(requestType);
  69. }
  70. },
  71. mounted() {
  72. this.getSubjects()
  73. }
  74. });
  75. addSubject = () =>{
  76. let name = document.querySelector('.subjectName').value
  77. let shortName = document.querySelector('.shortName').value
  78. var select = document.getElementById("selectProfmodule");
  79. var value = select.value;
  80. let type = document.querySelector('.type').checked
  81. var myHeaders = new Headers();
  82. myHeaders.append("Content-Type", "application/json");
  83. var raw = {"Name":name,"ID_professionalmodule":Number(value),"Shortname": shortName, "IDStype": Number(type)};
  84. requestFunc(url="/subject/", method="POST", data=raw)
  85. alert("Добавлено")
  86. }