First Option for expanding/collapsing HTML table row

A common UI will have an HTML table of data rows. When we click on "Expand", it shows a detailed breakdown of "child" rows below the "parent" row. In a parent row, click on the “+” sign; it expands the child row with detailed information. At the same time, the parent sign toggles to “- “.

Once we click on “- “sign, then it will collapse child rows with parent sign “+”.

The requirements are,

  1. Put a class of "parent" on each parent row (tr).
  2. Give each parent row (tr) an attribute ”data-toggle="toggle"”.
  3. Give each child row cover under <tbody> a class=hideTr.

Below is the sample of the table structure.

  1. <table>
  2. <tr data-toggle="toggle">
  3. <td >
  4. <p id="Technology" >
  5. <b>
  6. <span class="plusminusTechnology">+</span>
  7. <span lang="EN-IN">Technology </span>
  8. </b>
  9. </p>
  10. </td>
  11. <td ></td>
  12. <td ></td>
  13. </tr>
  14. <tbody class="hideTr">
  15. <tr >
  16. <td "></td>
  17. <td >
  18. <img height="28" src="clip_image001.png" v:shapes="Picture_x0020_5" width="106" />
  19. </td>
  20. <td
  21. <span lang="EN-IN">4</span>
  22. </td>
  23. <td
  24. </td>
  25. </tr>
  26. </tbody>
  27. </table>

Script Code

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function () {
  4. debugger;
  5. $('.hideTr').slideUp(600);
  6. $('[data-toggle="toggle"]').click(function () {
  7. if ($(this).parents().next(".hideTr").is(':visible')) {
  8. $(this).parents().next('.hideTr').slideUp(600);
  9. $(".plusminus" + $(this).children().children().attr("id")).text('+');
  10. $(this).css('background-color', 'white');
  11. }
  12. else {
  13. $(this).parents().next('.hideTr').slideDown(600);
  14. $(".plusminus" + $(this).children().children().attr("id")).text('- ');
  15. $(this).css('background-color', '#c1eaff ');
  16. }
  17. });
  18. });
  19. </script>

Expand/Collapse Table Rows With jQuery

Expand/Collapse Table Rows With jQuery
Expand/Collapse Table Rows With jQuery

Second option

A common UI which will have an HTML table of record rows, in which when we click on "Expand", it shows a detailed breakdown of "child" rows below the "parent" row.

The requirements are:

  1. Add a class of "parent" on each parent row (tr).
  2. Give each parent row (tr) an id.
  3. Give each child row a class of "child-ID" where ID is the id of the parent tr that it belongs to.
  1. <table id="detail_table" class="detail">
  2. <thead>
  3. <tr>
  4. <th>ID</th>
  5. <th colspan="2">Name</th>
  6. <th>Total</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr class="parent" id="row123" title="Click to expand/collapse" style="cursor: pointer;">
  11. <td>123</td>
  12. <td colspan="2">Bill Gates</td>
  13. <td>100</td>
  14. </tr>
  15. <tr class="child-row123" style="display: table-row;">
  16. <td> </td>
  17. <td>2018-01-02</td>
  18. <td>A short description of Microsoft revenue </td>
  19. <td>15</td>
  20. </tr>
  21. <tr class="child-row123" style="display: table-row;">
  22. <td> </td>
  23. <td>2018-02-03</td>
  24. <td>Another New Project description</td>
  25. <td>45</td>
  26. </tr>
  27. <tr class="child-row123" style="display: table-row;">
  28. <td> </td>
  29. <td>2010-03-04</td>
  30. <td>More New Stuff</td>
  31. <td>40</td>
  32. </tr>
  33. <tr class="parent" id="row456" title="Click to expand/collapse" style="cursor: pointer;">
  34. <td>456</td>
  35. <td colspan="2">Bill Brasky</td>
  36. <td>50</td>
  37. </tr>
  38. <tr class="child-row456" style="display: none;">
  39. <td> </td>
  40. <td>2009-07-02</td>
  41. <td>A short Two Describe a Third description</td>
  42. <td>10</td>
  43. </tr>
  44. <tr class="child-row456" style="display: none;">
  45. <td> </td>
  46. <td>2008-02-03</td>
  47. <td>Another New story description</td>
  48. <td>20</td>
  49. </tr>
  50. <tr class="child-row456" style="display: none;">
  51. <td> </td>
  52. <td>2009-03-04</td>
  53. <td>More story Stuff</td>
  54. <td>20</td>
  55. </tr>
  56. <tr class="parent" id="row789" title="Click to expand/collapse" style="cursor: pointer;">
  57. <td>789</td>
  58. <td colspan="2">Phil Upspace</td>
  59. <td>75</td>
  60. </tr>
  61. <tr class="child-row789" style="display: none;">
  62. <td> </td>
  63. <td>2008-01-02</td>
  64. <td>A short New Games description</td>
  65. <td>33</td>
  66. </tr>
  67. <tr class="child-row789" style="display: none;">
  68. <td> </td>
  69. <td>20011-04-03</td>
  70. <td>Another Games description</td>
  71. <td>22</td>
  72. </tr>
  73. <tr class="child-row789" style="display: none;">
  74. <td> </td>
  75. <td>20011-04-04</td>
  76. <td>More Games Stuff</td>
  77. <td>20</td>
  78. </tr>
  79. </tbody>
  80. </table>

Script Code

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function () {
  4. $('tr.parent')
  5. .css("cursor", "pointer")
  6. .attr("title", "Click to expand/collapse")
  7. .click(function () {
  8. $(this).siblings('.child-' + this.id).toggle();
  9. });
  10. $('tr[@class^=child-]').hide().children('td');
  11. });
  12. </script>

Expand/Collapse Table Rows With jQuery

Expand/Collapse Table Rows With jQuery