Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery Created Nested Table Based on Attribute
- <table>
- <tbody>
- <tr id="10"><td>Parent 1</td></tr>
- <tr id="14"><td>Parent 2</td></tr>
- </tbody>
- </table>
- <table>
- <tbody>
- <tr class="10"><td>Child A</td></tr>
- <tr class="10"><td>Child B</td></tr>
- <tr Class="14"><td>Child X</td></tr>
- </tbody>
- </table>
- $('tbody.csTR_children tr').each(function() {
- probable_parent = $('tbody.csTR_parent tr#' + $(this).attr('class'));
- if (probable_parent.length) {
- if (!probable_parent.find('tbody').length) probable_parent.append('<tbody/>');
- $(this).detach().appendTo(probable_parent.find('tbody'));
- }
- });
- probable_parent.children(":first").append('<table><tbody></tbody></table>');
- $('table#child tr').each(function() {
- var parentId = $(this).data('parentId');
- var parent = $('#' + parentId);
- if (parent) {
- var tbody = $('tbody', parent);
- if (tbody.length == 0) {
- // need to add the table wrapper
- tbody = $('<tbody>');
- var table = $('<table>').append(tbody);
- parent.append(table);
- }
- $(this).detach().appendTo(tbody);
- }
- });
- $('tbody.csTR_children tr').each(function() {
- probable_parent = $('#' + $(this).data('parentID'));
- if (probable_parent.length) {
- if (!probable_parent.find('tbody').length)
- $(this).detach().insertAfter(probable_parent);
- }
- });
Add Comment
Please, Sign In to add comment