Guest User

Untitled

a guest
Aug 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. jQuery Created Nested Table Based on Attribute
  2. <table>
  3. <tbody>
  4. <tr id="10"><td>Parent 1</td></tr>
  5. <tr id="14"><td>Parent 2</td></tr>
  6. </tbody>
  7. </table>
  8.  
  9. <table>
  10. <tbody>
  11. <tr class="10"><td>Child A</td></tr>
  12. <tr class="10"><td>Child B</td></tr>
  13. <tr Class="14"><td>Child X</td></tr>
  14. </tbody>
  15. </table>
  16.  
  17. $('tbody.csTR_children tr').each(function() {
  18. probable_parent = $('tbody.csTR_parent tr#' + $(this).attr('class'));
  19. if (probable_parent.length) {
  20. if (!probable_parent.find('tbody').length) probable_parent.append('<tbody/>');
  21. $(this).detach().appendTo(probable_parent.find('tbody'));
  22. }
  23. });
  24.  
  25. probable_parent.children(":first").append('<table><tbody></tbody></table>');
  26.  
  27. $('table#child tr').each(function() {
  28. var parentId = $(this).data('parentId');
  29. var parent = $('#' + parentId);
  30. if (parent) {
  31. var tbody = $('tbody', parent);
  32. if (tbody.length == 0) {
  33. // need to add the table wrapper
  34. tbody = $('<tbody>');
  35. var table = $('<table>').append(tbody);
  36. parent.append(table);
  37. }
  38. $(this).detach().appendTo(tbody);
  39. }
  40. });
  41.  
  42. $('tbody.csTR_children tr').each(function() {
  43. probable_parent = $('#' + $(this).data('parentID'));
  44. if (probable_parent.length) {
  45. if (!probable_parent.find('tbody').length)
  46. $(this).detach().insertAfter(probable_parent);
  47. }
  48. });
Add Comment
Please, Sign In to add comment