Advertisement
Guest User

Untitled

a guest
May 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8. </head>
  9. <body>
  10.     <div class="wrapper">
  11.             <input id="artist"type="text" name="artist" placeholder="artist">
  12.             <input type="text" placeholder="name" id="name">
  13.             <input type="text" placeholder="label" id="label">
  14.             <input type="number" placeholder="released" id="released">
  15.             <input type="text" placeholder="country" id="country">
  16.             <input type="number" placeholder="quantity" id="quantity">
  17.             <select id="mediaCondition">
  18.                 <option value="Mint (M)">Mint (M)</option>
  19.                 <option value="Near Mint (NM or M-)">Near Mint (NM or M-)</option>
  20.                 <option value="Very Good Plus (VG+)">Very Good Plus (VG+)</option>
  21.                 <option value="Very Good (VG)">Very Good (VG)</option>
  22.                 <option value="Good Plus (G+)">Good Plus (G+)</option>
  23.                 <option value="Good (G)">Good (G)</option>
  24.                 <option value="Fair (F)">Fair (F)</option>
  25.                 <option value="Poor (P)">Poor (P))</option>
  26.             </select>
  27.             <select id="sleeveCondition">
  28.                 <option value="Mint (M)">Mint (M)</option>
  29.                 <option value="Near Mint (NM or M-)">Near Mint (NM or M-)</option>
  30.                 <option value="Very Good Plus (VG+)">Very Good Plus (VG+)</option>
  31.                 <option value="Very Good (VG)">Very Good (VG)</option>
  32.                 <option value="Good Plus (G+)">Good Plus (G+)</option>
  33.                 <option value="Good (G)">Good (G)</option>
  34.                 <option value="Fair (F)">Fair (F)</option>
  35.                 <option value="Poor (P)">Poor (P))</option>
  36.             </select>
  37.             <input type="number" placeholder="price" id="price">
  38.         <input type="submit" id="submit">
  39.     </div>
  40.     <h3>Delete</h3>
  41.         <input type="text" id="deletebyname" placeholder="Name of record you want to delete">
  42.         <button id="delete">Delete</button>
  43.     <h3>Sell</h3>
  44.     <input type="text" id="sellbyname" placeholder="Name of record you want to sell">
  45.     <button id="sell">Sell</button>
  46.     <h3>Kõik tulemused:</h3>
  47.     <button id="refresh">Uuenda:</button>
  48.     <ul id="tulemused"></ul>
  49.     <h3>Kuva tooted:</h3>
  50.     <ul id="tooted"></ul>
  51. </body>
  52. <script>
  53.  
  54.     window.onload = function(){
  55.         //Lühendan d.getElementById:
  56.         const grab = id => document.getElementById(id)
  57.         let buttonId
  58.  
  59.         //Targetin välju..
  60.         let artist=grab("artist"), name=grab("name"), label=grab('label'), released=grab('released'),
  61.             country=grab('country'), mediaCondition=grab('mediaCondition'), sleeveCondition=grab('sleeveCondition')
  62.         //...Ja kuulan submit-i    
  63.         grab('submit').addEventListener("click",function(event){
  64.             event.preventDefault()
  65.             fetch('http://localhost:3456/uus', {
  66.                 method: "post",
  67.                 headers: {
  68.                     'Accept': 'application/json',
  69.                     'Content-Type': 'application/json'
  70.                 },
  71.                 body: JSON.stringify({
  72.                     artist: artist.value,
  73.                     name: name.value,
  74.                     label: label.value,
  75.                     released: released.value,
  76.                     country: country.value,
  77.                     mediaCondition: mediaCondition.value,
  78.                     sleeveCondition: sleeveCondition.value,
  79.                     quantity: quantity.value,
  80.                     price: price.value,
  81.  
  82.                 })
  83.             })
  84.                 .then(res => res.json())
  85.                 .then(data => {alert('lisatud!')})
  86.                 .catch(e => alert(e))
  87.             }
  88.         )
  89.         //displaying all records
  90.         //helper funtions
  91.         function createNode(element) {
  92.             return document.createElement(element); // Create the type of element you pass in the parameters
  93.         }
  94.         function createTextNode(text) {
  95.             return document.createTextNode(text); // Create the button text node
  96.         }
  97.         function append(parent, el) {
  98.             return parent.appendChild(el); // Append the second element to the first one
  99.         }
  100.         const ul = document.getElementById('tooted'); // Get the list where we will place our records
  101.  
  102.             fetch('http://localhost:3456/k6ik')
  103.                 .then(res => res.json())
  104.                 .then(function(data) {
  105.                     let records = data; // Get the results
  106.                     return records.map(function(record) { // Map through the results and for each run the code below
  107.                         let li = createNode('li'), //  Create the elements we need
  108.                             span = createNode('span'),
  109.                             button = createNode('BUTTON'),
  110.  
  111.                             t = createTextNode("Lisa ostukorvi");
  112.                         span.innerHTML = `Artist: ${record.artist} Name: ${record.name} Label: ${record.label} Year: ${record.released}
  113.                                             Country: ${record.country} Quantity: ${record.quantity} Media condition: ${record.mediaCondition}
  114.                                             Sleeve condition: ${record.sleeveCondition} Price: ${record.price} €`; // Make the HTML of our span to be the first and last name of our author
  115.                         button.setAttribute("id",`${record._id}`)//add mongo id to button id
  116.  
  117.                         //button.setAttribute("onclick","idListener(this.id);")
  118.                         button.setAttribute("class","buttonz")
  119.                         append(li, span);
  120.                         append(ul, li);
  121.                         append(li, button);
  122.                         append(button, t);
  123.                        
  124.                         let buttons = document.getElementsByClassName('buttonz');
  125.                         console.log(buttons)
  126.                         for (let i = 0; i < buttons.length; i++) {
  127.                             buttons[i].addEventListener('click', function() {
  128.                                 buttonId=this.id
  129.                                 console.log(buttonId)
  130.  
  131.                                 grab(buttonId).addEventListener('click', function(e) {
  132.                                     console.log(buttonId)
  133.  
  134.                                     fetch("http://localhost:3456/"+buttonId, {
  135.                                         body: JSON.stringify({_id: buttonId}),
  136.                                         method: "post",
  137.                                         headers: {
  138.                                             'Accept': 'application/json',
  139.                                             'Content-Type': 'application/json'
  140.                                         },
  141.                                     })
  142.                                         .then(res => res.json())
  143.                                         .then(data => alert(data))
  144.                                         .catch(e => alert(e))
  145.                                 })
  146.                             });
  147.                         }
  148.  
  149.             })
  150.  
  151.         const ulTulemused = document.getElementById('tulemused'); // Get the list where we will place our records
  152.         grab('refresh').addEventListener('click', function(e) {
  153.             fetch('http://localhost:3456/k6ik')
  154.                 .then(res => res.json())
  155.                 .then(function (data) {
  156.                     let records = data; // Get the results
  157.                     return records.map(function (record) { // Map through the results and for each run the code below
  158.                         let li = createNode('li'), //  Create the elements we need
  159.                             span = createNode('span');
  160.                         span.innerHTML = `Artist: ${record.artist} Name: ${record.name} Label: ${record.label} Year: ${record.released}
  161.                                             Country: ${record.country} Quantity: ${record.quantity} Media condition: ${record.mediaCondition}
  162.                                             Sleeve condition: ${record.sleeveCondition} Price: ${record.price} €`; // Make the HTML of our span to be the first and last name of our author
  163.                         append(li, span);
  164.                         append(ulTulemused, li);
  165.                     })
  166.                 })
  167.         })
  168.         grab("delete").addEventListener("click", function(e){
  169.             fetch("http://localhost:3456/"+grab("deletebyname").value, {
  170.                 body: JSON.stringify({name: grab("deletebyname").value}),
  171.                 method: "delete",
  172.                 headers: {
  173.                     'Accept': 'application/json',
  174.                     'Content-Type': 'application/json'
  175.                 },
  176.             })
  177.                 .then(res => res.json())
  178.                 .then(data => alert(data))
  179.                 .catch(e => alert(e))
  180.         })
  181.  
  182.         grab("sell").addEventListener("click", function(e){
  183.             fetch("http://localhost:3456/"+grab("sellbyname").value, {
  184.                 body: JSON.stringify({name: grab("sellbyname").value}),
  185.                 method: "post",
  186.                 headers: {
  187.                     'Accept': 'application/json',
  188.                     'Content-Type': 'application/json'
  189.                 },
  190.             })
  191.                 .then(res => res.json())
  192.                 .then(data => alert(data))
  193.                 .catch(e => alert(e))
  194.         })
  195.     }
  196.  
  197. </script>
  198. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement