Advertisement
Guest User

Untitled

a guest
May 24th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.     const tBodyEl = $('tbody');
  3.  
  4.  
  5.     //Load categories to combobox for add
  6.     const options = $("#selectCategory");
  7.     let changeCategory = "";
  8.  
  9.     //GET ALL CATEGORIES
  10.     var getCategories = function() {
  11.         return $.getJSON("http://localhost:8090/categories", function (result) {
  12.             $.each(result, function () {
  13.                 options.append($("<option />").text(this.name));
  14.             });
  15.             changeCategory = options.html();
  16.         });
  17.     }
  18.     // GET ALL PRODUCTS AFTER CATEGORIES
  19.     var getProducts = function() {
  20.         return $.ajax({
  21.             url: "http://localhost:8090/products",
  22.             contentType: 'application/json',
  23.             success: function (result) {
  24.                 console.log("==================================")
  25.                 console.log(options.html());
  26.                 console.log("==================================")
  27.                 $.each(result, function (i, product) {
  28.                     var productRow = '<tr>' +
  29.                         '<td class="id">' + product.id + '</td>' +
  30.                         '<td class="name"><input id="nameInput" type="text" value="' + product.name.charAt(0).toUpperCase() + product.name.slice(1) + '"></td>' +
  31.                         '<td class="category"><select id="changeCategory">' + changeCategory + '</select></td>' +
  32.                         '<td><button class="update-button">UPDATE</button>' +
  33.                         '<button class="delete-button" >DELETE</button></td>'
  34.                     '</tr>';
  35.                     tBodyEl.append(productRow);
  36.                     //$('select').append(options.html());
  37.                     //tBodyEl.find("tr:last").find('#changeCategory').append(options.html());
  38.                     tBodyEl.find("tr:last").find('#changeCategory').val(product.category.name);
  39.                 });
  40.             },
  41.             error: function (e) {
  42.                 alert("ERROR: ", e);
  43.                 console.log("ERROR: ", e);
  44.             }
  45.         })
  46.     }
  47.  
  48.     getCategories().done(getProducts());
  49.     // POST
  50.     $('#create-form').on('submit', function (event) {
  51.         event.preventDefault();
  52.         const createName = $('#create-name');
  53.         const selected = $("#selectCategory :selected").text();
  54.         console.log(selected);
  55.         $.ajax({
  56.             url: "http://localhost:8090/products",
  57.             method: 'POST',
  58.             contentType: 'application/json',
  59.             data: JSON.stringify({
  60.                 name: createName.val(),
  61.                 category: selected
  62.             }),
  63.             success: function (product) {
  64.                 createName.val("");
  65.  
  66.                 var productRow = '<tr>' +
  67.                     '<td class="id">' + product.id + '</td>' +
  68.                     '<td class="name"><input id="nameInput" type="text" value="' + product.name.charAt(0).toUpperCase() + product.name.slice(1) + '"></td>' +
  69.                     '<td class="category"><select id="changeCategory">' + changeCategory + '</select></td>' +
  70.                     '<td><button class="update-button">UPDATE</button>' +
  71.                     '<button class="delete-button" >DELETE</button></td>'
  72.                 '</tr>';
  73.  
  74.                 tBodyEl.append(productRow);
  75.                 tBodyEl.find("tr:last").find('#changeCategory').val(product.category.name);
  76.             }
  77.         })
  78.     });
  79.  
  80.     //UPDATE
  81.     $('#productTable').on('click', '.update-button', function () {
  82.         const rowEl = $(this).closest('tr');
  83.         const id = rowEl.find('.id').text();
  84.         const newName = rowEl.find("#nameInput").val();
  85.         const newCategory = rowEl.find("#changeCategory").val();
  86.         $.ajax({
  87.             url: "http://localhost:8090/products/" + id,
  88.             method: 'PUT',
  89.             contentType: 'application/json',
  90.             data: JSON.stringify({
  91.                 id: id,
  92.                 name: newName,
  93.                 category: newCategory
  94.             }),
  95.             success: function (product) {
  96.                 console.log(product);
  97.                 rowEl.find('#nameInput').val(product.name.charAt(0).toUpperCase() + product.name.slice(1));
  98.                 rowEl.find('#changeCategory').val(product.category.name);
  99.  
  100.  
  101.             }
  102.         })
  103.     });
  104.  
  105.     //DELETE
  106.     $('#productTable').on('click', '.delete-button', function () {
  107.         const rowEl = $(this).closest('tr');
  108.         const id = rowEl.find('.id').text();
  109.         console.log(id);
  110.         $.ajax({
  111.             url: "http://localhost:8090/products/" + id,
  112.             method: 'DELETE',
  113.             contentType: 'application/json',
  114.             success: function (response) {
  115.                 console.log(response);
  116.                 rowEl.remove();
  117.             }
  118.         })
  119.     });
  120.  
  121.     //SEARCH
  122.     $("#search").keyup(function () {
  123.         var value = this.value.toUpperCase();
  124.         $("table tr").each(function (index) {
  125.             if (!index) return;
  126.             $(this).find("input").each(function () {
  127.                 const name = $(this).val().toUpperCase();
  128.                 console.log(name);
  129.                 var not_found = (name.indexOf(value) == -1);
  130.                 $(this).closest('tr').toggle(!not_found);
  131.                 return not_found;
  132.             });
  133.         });
  134.     });
  135. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement