Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <head>
  2. <title>Data Tables</title>
  3. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  4. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">
  5.  
  6. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
  7. <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
  8. <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
  9. </head>
  10. <style>
  11. td.details-control {
  12. background: url('../assets/details_open.png') no-repeat center center;
  13. cursor: pointer;
  14. }
  15. tr.shown td.details-control {
  16. background: url('../assets/details_close.png') no-repeat center center;
  17. }
  18. </style>
  19. <body>
  20. <header>
  21. <nav class="navbar navbar-default">
  22. <div class="container-fluid">
  23. <div class="navbar-header">
  24. <h4 stye="color:grey"> [IF635] Web Programming </h4>
  25. </div>
  26. <ul class="nav navbar-nav navbar-right">
  27. <li class="active"><a href="#">Students</a></li>
  28. </ul>
  29. </div>
  30. </nav>
  31. </header>
  32. <div class="container col-5">
  33. <table id="example" class="table table-striped table-bordered" cellspacing="0">
  34. <thead>
  35. <tr>
  36. <th></th>
  37. <th>Last Name</th>
  38. <th>Title</th>
  39. <th>Extension</th>
  40. <th>Full Name</th>
  41. <th>Birth Date</th>
  42. <th>Address</th>
  43. <th>City</th>
  44. <th>Home Phone</th>
  45. </tr>
  46. </thead>
  47. <tfoot>
  48. <tr>
  49. <th></th>
  50. <th>Last Name</th>
  51. <th>Title</th>
  52. <th>Extension</th>
  53. <th>Full Name</th>
  54. <th>Birth Date</th>
  55. <th>Address</th>
  56. <th>City</th>
  57. <th>Home Phone</th>
  58. </tr>
  59. </tfoot>
  60. <tbody id="tbody">
  61. <?php
  62. $host = "localhost";
  63. $username = "root";
  64. $dbname = "northwind";
  65. $password = "";
  66.  
  67. $conn = new mysqli($host, $username, $password, $dbname);
  68.  
  69. $query = "SELECT * FROM Employees LIMIT 12";
  70. $res = $conn->query($query);
  71.  
  72. while($row = $res->fetch_assoc()){
  73. echo "<tr>";
  74. echo "<td class=\"details-control\"></td>";
  75. echo "<td>" . $row['LastName'] . "</td>";
  76. echo "<td>" . $row['Title'] . "</td>";
  77. echo "<td>" . $row['Extension'] . "</td>";
  78. echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
  79. echo "<td>" . $row['BirthDate'] . "</td>";
  80. echo "<td>" . $row['Address'] . "</td>";
  81. echo "<td>" . $row['City'] . "</td>";
  82. echo "<td>" . $row['HomePhone'] . "</td>";
  83. echo "</tr>";
  84. }
  85.  
  86. mysqli_free_result($res);
  87. mysqli_close($conn)
  88. ?>
  89. </tbody>
  90. </table>
  91. </div>
  92. </body>
  93.  
  94. <script>
  95. function format ( d ) {
  96. // `d` is the original data object for the row
  97. return '<table id="example" class="table table-striped table-bordered" cellspacing="0">'+
  98. '<tr>'+
  99. '<td>Full Name:</td>'+
  100. '<td>'+d.fullName+'</td>'+
  101. '</tr>'+
  102. '<tr>'+
  103. '<td>Birth Date:</td>'+
  104. '<td>'+d.birthDate+'</td>'+
  105. '</tr>'+
  106. '<tr>'+
  107. '<td>Address</td>'+
  108. '<td>'+d.address+'</td>'+
  109. '</tr>'+
  110. '<tr>'+
  111. '<td>City</td>'+
  112. '<td>'+d.city+'</td>'+
  113. '</tr>'+
  114. '<tr>'+
  115. '<td>Home Phone</td>'+
  116. '<td>'+d.homePhone+'</td>'+
  117. '</tr>'+
  118. '</table>';
  119. }
  120.  
  121. $(document).ready(function() {
  122. var table = $('#example').DataTable( {
  123. columnDefs: [
  124. { targets: [0, 1,2,3], visible: true},
  125. { targets: '_all', visible: false }
  126. ],
  127. "columns": [
  128. {
  129. "className": 'details-control',
  130. "orderable": false,
  131. "data": null,
  132. "defaultContent": ''
  133. },
  134. { "data": "lastName" },
  135. { "data": "title" },
  136. { "data": "extension" },
  137. { "data": "fullName" },
  138. { "data": "birthDate" },
  139. { "data": "address" },
  140. { "data": "city" },
  141. { "data": "homePhone" }
  142. ],
  143. "order": [[1, 'asc']]
  144. } );
  145.  
  146. // Add event listener for opening and closing details
  147. $('#example tbody').on('click', 'td.details-control', function () {
  148. var tr = $(this).closest('tr');
  149. var row = table.row( tr );
  150.  
  151. if ( row.child.isShown() ) {
  152. // This row is already open - close it
  153. row.child.hide();
  154. tr.removeClass('shown');
  155. }
  156. else {
  157. // Open this row
  158. row.child( format(row.data()) ).show();
  159. tr.addClass('shown');
  160. }
  161. } );
  162. } );
  163. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement