Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var choice ='';
  2. const ul = $('#data');
  3.  
  4.  
  5. const getDataFromServer = function(){
  6.     choice = $(this).attr('value');
  7.     const link = "http://localhost:3000/"+ choice;
  8.     showWindow();
  9.     $.ajax({
  10.         url:link,
  11.         type: 'get'
  12.     }).done(handleChoice);
  13. };
  14.  
  15. const showWindow = function(){
  16.   if(choice == "schools"){$('#editor').fadeIn();}
  17.   else{$('#editor').fadeOut();}
  18. };
  19.  
  20. const handleChoice = function(data){
  21.     ul.html('');
  22.     for(var element in data){
  23.         addLi(data[element]);
  24.         if(choice == "schools"){
  25.             ul.append(
  26.             "<input type='button' value='Update'>" +
  27.             "<input type='button' value='Delete'>");
  28.         }
  29.     }
  30. };
  31.  
  32. const showForm = function () {
  33.     $('#data').slideUp();
  34.     $('#new').slideDown();
  35. };
  36.  
  37. const getDeeper = function (obj) {
  38.     string ='';
  39.     for(var item in obj){string += item +":" + obj[item] + "<br>";}
  40.     return string;
  41. };
  42.  
  43. const addLi = function(element){
  44.     ul.append("<ul>");
  45.     for(var property in element){
  46.         var string = '';
  47.         var obj = element[property];
  48.         if(obj instanceof Object){string = property + getDeeper(obj);}
  49.         else{string = property + ":" + element[property] }
  50.         $('#data').append("<li>"+string+"</li>");
  51.     }
  52.     ul.append("</ul>");
  53. };
  54.  
  55. $(function(){
  56.     test();
  57.     $(".button-collapse").sideNav();
  58.     $("#options").on('change',getDataFromServer);
  59.     $("ul li").on('click',getDataFromServer);
  60.     $('#newSchool').on('click',showForm);
  61.  
  62.  
  63. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement