Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- <table id="parentTable">
- <thead>
- <tr class="category">
- <th>Unique ID</th>
- <th>Name</th>
- <th>Email</th>
- <th>Price</th>
- <th>Hours</th>
- </tr>
- </thead>
- <tbody id="parentTableBody">
- </tbody>
- </table>
- $(document).ready(function() {
- var arr1 = generateItem();
- if (arr1) {
- var arr2 = [].concat(arr1);
- var tr;
- $.each(arr2, function(i, e) {
- tr = $('<tr>');
- //TODO only add <a> tag if there is going to be a child row!
- tr.append("<td>" + "<a href='#'>" + (e.UniqueId || "") + "</a>" + "</td>");
- tr.append("<td>" + (e.Name || "") + "</td>");
- tr.append("<td>" + (e.Email || "") + "</td>");
- tr.append("<td>" + (e.Price || "") + "</td>");
- tr.append("<td>" + (e.Hours || "") + "</td>");
- $('#parentTableBody').append(tr);
- tr = $('<tr class="child-row">');
- tr.append("<th>" + "Description" + "</th>");
- tr.append("<th>" + "Arrival" + "</th>");
- tr.append("<td>" + (e.Description || "") + "</td>");
- tr.append("<td>" + (e.Arrival || "") + "</td>");
- $('#parentTableBody').append(tr);
- tr.hide();
- });
- }
- });
- function generateItem() {
- var item = [{
- UniqueId: "0",
- Name: "Alex",
- Email: "alex@email.com",
- Price: "$20.00",
- Hours: "1",
- Description: "N/A",
- Arrival: "Noon"
- },
- {
- UniqueId: "1",
- Name: "Jay",
- Email: "jay@email.com",
- Price: "$12.00",
- Hours: "0.2",
- Description: "Small",
- Arrival: "test"
- },
- {
- UniqueId: "2",
- Name: "Dylan",
- Email: "dylan@email.com",
- Price: "$32.00",
- Hours: "2.2",
- Description: "Heavy"
- }
- ];
- return item;
- }
- $(function() {
- $('#parentTable tbody a').on("click", function(event) {
- $baseRow = $(event.target).closest("tr");
- next = $baseRow.next("tr");
- if (next.is(".child-row")){
- next.toggle();
- }
- });
- });
Add Comment
Please, Sign In to add comment