simonradev

Table

Feb 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initializeTable() {
  2.     $('#createLink').on('click', onCreateLinkClicked);
  3.    
  4.     function onCreateLinkClicked(event) {  
  5.         const inputCountry = $('#newCountryText');
  6.         const inputCapital = $('#newCapitalText');
  7.  
  8.         const country = inputCountry.val();
  9.         const capital = inputCapital.val();
  10.  
  11.         inputCountry.val('');
  12.         inputCapital.val('');
  13.        
  14.         const elementCountry = $('<td></td>').text(country);
  15.         const elementCapital = $('<td></td>').text(capital);
  16.  
  17.         const elementLinks = $('<td></td>');
  18.         $createATag('[Up]', onUpClicked, elementLinks);
  19.         $createATag('[Down]', onDownClicked, elementLinks);
  20.         $createATag('[Delete]', onDeleteClicked, elementLinks);
  21.  
  22.         const elementRow = $('<tr></tr>')
  23.                              .append(elementCountry)
  24.                              .append(elementCapital)
  25.                              .append(elementLinks)
  26.                              .css('display', 'none');
  27.  
  28.         $('#countriesTable').append(elementRow);
  29.  
  30.         $fixLinks();
  31.  
  32.         elementRow.fadeIn();
  33.     }
  34.  
  35.     function $createATag(text, eventHandler, appendTo) {
  36.         $('<a href="#"></a>')
  37.                         .text(text)
  38.                         .on('click', eventHandler)
  39.                         .appendTo(appendTo);
  40.     }
  41.  
  42.     function onUpClicked(event) {
  43.         const targetRow = $getParentRow(event.target);
  44.         targetRow.fadeOut(function () {
  45.             targetRow.prev().insertAfter(targetRow);
  46.             $fixLinks();
  47.             targetRow.fadeIn();
  48.         });
  49.     }
  50.  
  51.     function onDownClicked(event) {
  52.         const targetRow = $getParentRow(event.target);
  53.         targetRow.fadeOut(function () {
  54.             targetRow.next().insertBefore(targetRow);
  55.             $fixLinks();
  56.             targetRow.fadeIn();
  57.         });
  58.     }
  59.  
  60.     function onDeleteClicked(event) {
  61.         const targetRow = $getParentRow(event.target);
  62.         targetRow.fadeOut(function () {
  63.             targetRow.remove();
  64.             $fixLinks();
  65.         });
  66.     }
  67.  
  68.     function $getParentRow(element) {
  69.         return $(element)
  70.                     .parent()
  71.                     .parent();
  72.     }
  73.  
  74.     function $fixLinks() {
  75.         $('#countriesTable a').css('display', 'inline');
  76.  
  77.         $('#countriesTable tr:nth-child(3) a:contains("Up")').css('display', 'none');
  78.         $('#countriesTable tr:last-child a:contains("Down")').css('display', 'none');
  79.     }
  80. }
Add Comment
Please, Sign In to add comment