iasatan

Untitled

Dec 2nd, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function openBooks(author)
  2. {
  3.     var tbl=document.getElementById("authorsTable");
  4.     tbl.parentNode.removeChild(tbl);
  5.     $.getJSON('books', function(data) {
  6.         var table = $('<table border="1"></table>');
  7.         $(table).append('<tr><th>Id</th><th>Title</th><th>Genre</th><th>Author</th></tr>');
  8.  
  9.         $.each(data, function(key, value) {
  10.             var authorCell=$('<td></td>');
  11.             var actualAuthor;
  12.  
  13.             $.each(value['authors'], function(authorIndex, authorValue){
  14.                 $(authorCell).append(authorValue['name']);
  15.                 actualAuthor=authorValue['name'];
  16.             });
  17.             if(author===actualAuthor){
  18.                 var row = $('<tr></tr>');
  19.                 var idCell = $('<td>' + value['bookId'] + '</td>');
  20.                 var titleCell = $('<td>' + value['title'] + '</td>');
  21.                 var genresCell = $('<td>' + value['genres'] + '</td>');
  22.                 $(row).append(idCell);
  23.                 $(row).append(titleCell);
  24.                 $(row).append(genresCell);
  25.                 $(row).append(authorCell);
  26.                 $(table).append(row);
  27.             }
  28.         });
  29.         $('#content').append(table);
  30.     });
  31. }
  32.  
  33. <script>
  34.     $.getJSON('authors', function(data) {
  35.         var table = $('<table id="authorsTable" border="1"></table>');
  36.         $(table).append(
  37.                 '<tr><th>Name</th><th>Nationality</th><th>Birth date</th></tr>');
  38.         $.each(data, function(key, value) {
  39.             var row = $('<tr></tr>');
  40.             var link
  41.             var nameCell = $('<td id="authorId" onclick="openBooks(' + "'" + value['name'] + "'" + ')">' + value['name'] + '</td>');
  42.             var nationCell = $('<td>' + value['nationality'] + '</td>');
  43.             var birthDateCell = $('<td>' + converDate(value['birthDate']) + '</td>');
  44.             $(row).append(nameCell);
  45.             $(row).append(nationCell);
  46.             $(row).append(birthDateCell);
  47.             $(table).append(row);
  48.         });
  49.         $('#content').append(table);
  50.     });
  51. </script>
Advertisement
Add Comment
Please, Sign In to add comment