Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. function concatTables() {
  2. var max_cells_per_row = {};
  3. var no_rows = 0;
  4.  
  5.  
  6. $("table.tag").each(function (index, value) {
  7. $(this).children().children("tr").each(function (index, value) {
  8. let no_cells = $(this).children("td").length;
  9. if (typeof max_cells_per_row[index] === "undefined") {
  10. max_cells_per_row[index] = no_cells;
  11. } else {
  12. max_cells_per_row[index] = Math.max(max_cells_per_row[index], no_cells);
  13. }
  14. });
  15. no_rows = Math.max(no_rows, $(this).children().children("tr").length);
  16. });
  17.  
  18. var table = $("<table>");
  19. var tableHeader = $("<thead>");
  20. var tableRow = $("<tr>");
  21. for (let i = 0; i < max_cells_per_row[0]; i += 1)
  22. {
  23. var tableCell = $("<td>");
  24. var textContent = "";
  25. $("table.tag").each(function (index, value) {
  26. $(this).children().children("tr").each(function (index, value) {
  27. if (index === 0) {
  28. $(this).children("td").each(function (index, value) {
  29. if (index === i) {
  30. textContent += $(this).text();
  31. }
  32. });
  33. }
  34. });
  35. });
  36. tableCell.text(textContent);
  37. tableRow.append(tableCell);
  38. }
  39. tableHeader.append(tableRow);
  40. table.append(tableHeader);
  41. var tableBody = $("<tbody>");
  42. for (let i = 1; i < no_rows; i += 1) {
  43. let tableRow = $("<tr>");
  44. for (let j = 0; j < max_cells_per_row[i]; j += 1) {
  45. let tableCell = $("<td>");
  46. let textContent = "";
  47. $("table.tag").each(function (index, value) {
  48. $(this).children().children("tr").each(function (index, value) {
  49. if (index === i) {
  50. $(this).children("td").each(function (index, value) {
  51. if (index === j) {
  52. textContent += $(this).text();
  53. }
  54. });
  55. }
  56. });
  57. });
  58. tableCell.text(textContent);
  59. tableRow.append(tableCell);
  60. }
  61. tableBody.append(tableRow);
  62. }
  63. table.append(tableBody);
  64.  
  65. console.log(table.html());
  66. console.log(no_rows);
  67. console.table(max_cells_per_row);
  68. $("button").after(table);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement