Advertisement
Guest User

JS Arbetsförmedlingen UPDATE

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Denna fil ska innehålla er lösning till projektuppgiften.
  2.  
  3. "use strict";
  4.  
  5. // Variables
  6.  
  7. var content = document.getElementById("info"); // where the job advertisements will show
  8.  
  9. var searchbuttonEl = document.getElementById("searchbutton");
  10. var leftmenu = document.getElementById("mainnavlist"); // left-hand menu where all län will appear
  11. var selectbox = document.getElementById("searchlan"); // select box where all län will appear
  12.  
  13. var onlyitJob = "";  // empty variable needed for the function showJob. In global scope because otherwise this variable cannot be found if the variable
  14.                      // is only in the local scope of function showJob
  15.  
  16.  
  17. var i; // used later in loops for iteration
  18. var baseUrl = "http://api.arbetsformedlingen.se/af/v0"; // base url where further arguments are being attached to
  19.  
  20. // Event Listeners
  21.  
  22.  
  23. window.onload = loadLan; // when window is loaded, the function loadData will fire
  24. searchbuttonEl.addEventListener("click", searchJob , false); // click on the search button and the function searchJob will fire
  25.  
  26. selectbox.addEventListener("change", showJob, false); //choose a lan from the select box, then the job advertisements of this lan will show
  27. leftmenu.addEventListener("click", showJob, false); // click on the left-hand menu, then the job advertisements will show
  28.  
  29.  
  30. // Function to get all län to show in the left-hand menu as well as in the select box
  31.  
  32. function loadLan() {
  33.     // AJAX request
  34.     var xhr = new XMLHttpRequest();
  35.    
  36.         xhr.onload = function() {
  37.             // check if server response is OK
  38.             if(xhr.status === 200) {
  39.                 var jsonStr = JSON.parse(xhr.responseText);
  40.                 var allLan = jsonStr.soklista.sokdata; //all län are being stored in this variable
  41.                 //var leftmenu = document.getElementById("mainnavlist"); // left-hand menu where all län will appear
  42.                 //var selectbox = document.getElementById("searchlan"); // select box where all län will appear
  43.  
  44.                 // Loop through all län
  45.  
  46.             for(i=0; i<allLan.length; i++) {
  47.                 // Create new element
  48.                 leftmenu.innerHTML += "<li id='"+ allLan[i].id + "'>" + allLan[i].namn + " (" + allLan[i].antal_ledigajobb + ")"+ "</li>";
  49.                 selectbox.innerHTML += "<option value='"+ allLan[i].id+"'>" + allLan[i].namn + "</option>"; // All län will show in the select box on top of the page
  50.             }
  51.         } else if (xhr.status == 400) {
  52.             alert('Error: 400');
  53.         }
  54.         else {
  55.             alert('Error: Something else than 200 was returned');
  56.         }
  57.     }
  58. // Send AJAX request
  59. xhr.open('GET', baseUrl+"/platsannonser/soklista/lan", true);
  60. xhr.send();
  61. }
  62.  
  63. function showJob() {
  64.     // AJAX request
  65.     var xhr = new XMLHttpRequest();
  66.  
  67.     xhr.onload = function() {
  68.         //check if server response is OK
  69.         if(xhr.status === 200) {
  70.             var jsonStr = JSON.parse(xhr.responseText);
  71.             var allJob = jsonStr.matchningslista.matchningdata; // all job advertisements are being stored in this variable
  72.  
  73.            
  74.            
  75.             var checkitJob = document.getElementById("onlyit").checked; // Checkbox of "show only IT" is checked
  76.             var onlyitJob = "";
  77.  
  78.             if(checkitJob === true) {
  79.                 onlyitJob = "&yrkesomradeid=3"; // extra argument in the AJAX request url if checkbox of "show only IT" is checked
  80.             } else { // if the checkbox of "show only IT" is not checked, onlyitJob has no value
  81.                 onlyitJob = "";
  82.             }
  83.  
  84.  
  85.             for(i=0; i<allJob.length; i++) { // shows
  86.             content.innerHTML += "<article>" + "<h3>" + allJob[i].annonsrubrik + "</h3>" + "<h4>" + allJob[i].yrkesbenamning + "</h4>"
  87.                               + "<p>" + "<strong>" + "Anställningstyp: " + "</strong>" + allJob[i].anstallningstyp + "</p>" + "<br />"
  88.                               + "<p>" + "<strong>" + "Antal platser: " + "</strong>" + allJob[i].antalplatser + "</p>" + "<br />"
  89.                               + "<p>" + "<strong>" + "Publiceringsdatum: " + "</strong>" + allJob[i].publiceraddatum + "</p>" + "<br />"
  90.                               + "<p>" + "<strong>" + "Sista ansökningsdag: " + "</strong>" + allJob[i].sista_ansokningsdag + "</p>" + "<br />"
  91.                               + '<a href="' + allJob[i].annonsurl + '" class="btn" target="_blank">Läs Mer</a>' + "</article>";
  92.  
  93.             }
  94.  
  95.        
  96.         } else if (xhr.status == 400) {
  97.             alert('Error: 400');
  98.         }
  99.         else {
  100.             alert('Error: Something else than 200 was returned');
  101.         }
  102.     }
  103.     // Send AJAX request
  104.     var lanid = document.getElementById("searchlan").value; // to get the ID of the chosen län from the select box
  105.     var numrow = document.getElementById("numrows").value; // the value, i.e. the number, that is shown in "Max antal"
  106.     var url = baseUrl + "/platsannonser/matchning?lanid="+lanid+onlyitJob+"&antalrader="+numrow;
  107.  
  108.     xhr.open('GET', url, true);
  109.     xhr.send();
  110. }
  111.  
  112. function searchJob() {
  113.  
  114.     content.innerHTML=""; // the area where the job advertisements are shown is emptied (otherwise former search results will still be shown)
  115.     var input = document.getElementById("searchText").value; //the value of whatever someone has typed into the search box
  116.     // new AJAX request
  117.     var xhr = new XMLHttpRequest();
  118.  
  119.     xhr.onload = function() {
  120.         //check if server response is OK
  121.         if(xhr.status === 200) {
  122.             var jsonStr = JSON.parse(xhr.responseText);
  123.             var joblist = jsonStr.soklista.sokdata; // all jobs are listed in this variable
  124.  
  125.             for(i=0;i<joblist.length;i++) {
  126.                 content.innerHTML += "<article>" + "<h3>" + joblist[i].namn + "</h3>" + "<br />"
  127.                                   + "<p>" + "Antal platsannonser: " + joblist[i].antal_platsannonser + "</p>" + "<br />"
  128.                                   + "<p>" + "Antal lediga jobb: " + joblist[i].antal_ledigajobb + "</p>" + "</article>";
  129.             }
  130.         } else if (xhr.status == 400) {
  131.             alert('Error: 400');
  132.         }
  133.         else {
  134.             alert('Error: Something else than 200 was returned');
  135.         }
  136.    
  137. }
  138.     xhr.open('GET', baseUrl + "/platsannonser/soklista/yrken"+input,true); //a searchlist will show all the jobs that start with the same letters as those typed into "input"
  139.     xhr.send();
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement