Guest User

Untitled

a guest
May 21st, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <table id="parentTable">
  3. <thead>
  4. <tr class="category">
  5. <th>Unique ID</th>
  6. <th>Name</th>
  7. <th>Email</th>
  8. <th>Price</th>
  9. <th>Hours</th>
  10. </tr>
  11. </thead>
  12.  
  13. <tbody id="parentTableBody">
  14. </tbody>
  15. </table>
  16.  
  17. $(document).ready(function() {
  18. var arr1 = generateItem();
  19. if (arr1) {
  20. var arr2 = [].concat(arr1);
  21. var tr;
  22. $.each(arr2, function(i, e) {
  23. tr = $('<tr>');
  24. //TODO only add <a> tag if there is going to be a child row!
  25. tr.append("<td>" + "<a href='#'>" + (e.UniqueId || "") + "</a>" + "</td>");
  26. tr.append("<td>" + (e.Name || "") + "</td>");
  27. tr.append("<td>" + (e.Email || "") + "</td>");
  28. tr.append("<td>" + (e.Price || "") + "</td>");
  29. tr.append("<td>" + (e.Hours || "") + "</td>");
  30. $('#parentTableBody').append(tr);
  31.  
  32. tr = $('<tr class="child-row">');
  33. tr.append("<th>" + "Description" + "</th>");
  34. tr.append("<th>" + "Arrival" + "</th>");
  35. tr.append("<td>" + (e.Description || "") + "</td>");
  36. tr.append("<td>" + (e.Arrival || "") + "</td>");
  37. $('#parentTableBody').append(tr);
  38. tr.hide();
  39.  
  40. });
  41.  
  42. }
  43. });
  44.  
  45. function generateItem() {
  46. var item = [{
  47. UniqueId: "0",
  48. Name: "Alex",
  49. Email: "alex@email.com",
  50. Price: "$20.00",
  51. Hours: "1",
  52. Description: "N/A",
  53. Arrival: "Noon"
  54. },
  55. {
  56. UniqueId: "1",
  57. Name: "Jay",
  58. Email: "jay@email.com",
  59. Price: "$12.00",
  60. Hours: "0.2",
  61. Description: "Small",
  62. Arrival: "test"
  63. },
  64. {
  65. UniqueId: "2",
  66. Name: "Dylan",
  67. Email: "dylan@email.com",
  68. Price: "$32.00",
  69. Hours: "2.2",
  70. Description: "Heavy"
  71. }
  72. ];
  73.  
  74. return item;
  75. }
  76.  
  77. $(function() {
  78. $('#parentTable tbody a').on("click", function(event) {
  79. $baseRow = $(event.target).closest("tr");
  80. next = $baseRow.next("tr");
  81. if (next.is(".child-row")){
  82. next.toggle();
  83. }
  84.  
  85. });
  86. });
Add Comment
Please, Sign In to add comment