Advertisement
AlexanderHristov

4. Students

Mar 31st, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function students() {
  2.     const baseUrl = 'https://baas.kinvey.com/';
  3.     const appKey = 'kid_BJXTsSi-e';
  4.     const collection = 'students';
  5.     const username = 'guest';
  6.     const password = 'guest';
  7.     const headers = {
  8.         'Authorization': `Basic ${btoa(username + ':' + password)}`,
  9.     };
  10.  
  11.     let div = $('<div id="buttons">');
  12.     let addBtn = $('<button id="addButton">Add Student</button>');
  13.     let showBtn = $('<button id="showButton">Show Students</button>');
  14.     div.append('<br>', addBtn, showBtn);
  15.     div.appendTo($('body'));
  16.  
  17.     showBtn.on('click', showStudents);
  18.     addBtn.on('click', addStudent);
  19.  
  20.     async function showStudents() {
  21.         try {
  22.             let studentsList = await $.ajax({
  23.                 headers,
  24.                 url: baseUrl + 'appdata/' + appKey + '/' + collection,
  25.                 method: 'GET'
  26.             });
  27.  
  28.             let sortedList = studentsList.sort((a, b) => a.ID - b.ID);
  29.  
  30.             sortedList.forEach((student) => {
  31.                 let newRow = $(`<tr>
  32.                     <th>${student.ID}</th>
  33.                     <th>${student.FirstName}</th>
  34.                     <th>${student.LastName}</th>
  35.                     <th>${student.FacultyNumber}</th>
  36.                     <th>${student.Grade}</th>
  37.                     </tr>`);
  38.                 newRow.appendTo($('#results'))
  39.             })
  40.  
  41.         } catch (err) {
  42.             console.log(err);
  43.         }
  44.     }
  45.     function addStudent() {
  46.  
  47.         let inputRow = $(`<tr>
  48.                     <th><input id="ID" type="text" placeholder="Enter ID"></th>
  49.                     <th><input id="firstName" type="text" placeholder="First Name"></th>
  50.                     <th><input id="lastName" type="text" placeholder="Last Name"></th>
  51.                     <th><input id="number" type="text" placeholder="Faculty Number"></th>
  52.                     <th><input id="grade" type="text" placeholder="Grade"></th>
  53.                     <th><button id="add">Add</button></th>
  54.                     </tr>`);
  55.         inputRow.appendTo($('#buttons'));
  56.  
  57.         $('#add').on('click', add);
  58.  
  59.         async function add() {
  60.  
  61.             let studentObj = {
  62.                 ID: +$('#ID').val(),
  63.                 FirstName: $('#firstName').val(),
  64.                 LastName: $('#lastName').val(),
  65.                 FacultyNumber: $('#number').val(),
  66.                 Grade: +$('#grade').val()
  67.             };
  68.            
  69.             try {
  70.                 await $.ajax({
  71.                     headers,
  72.                     url: baseUrl + 'appdata/' + appKey + '/' + collection,
  73.                     method: 'POST',
  74.                     data: JSON.stringify(studentObj)
  75.                 });
  76.  
  77.                 showStudents();
  78.  
  79.             } catch (err) {
  80.                 console.log(err);
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement