Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function concatTables() {
- var max_cells_per_row = {};
- var no_rows = 0;
- $("table.tag").each(function (index, value) {
- $(this).children().children("tr").each(function (index, value) {
- let no_cells = $(this).children("td").length;
- if (typeof max_cells_per_row[index] === "undefined") {
- max_cells_per_row[index] = no_cells;
- } else {
- max_cells_per_row[index] = Math.max(max_cells_per_row[index], no_cells);
- }
- });
- no_rows = Math.max(no_rows, $(this).children().children("tr").length);
- });
- var table = $("<table>");
- var tableHeader = $("<thead>");
- var tableRow = $("<tr>");
- for (let i = 0; i < max_cells_per_row[0]; i += 1)
- {
- var tableCell = $("<td>");
- var textContent = "";
- $("table.tag").each(function (index, value) {
- $(this).children().children("tr").each(function (index, value) {
- if (index === 0) {
- $(this).children("td").each(function (index, value) {
- if (index === i) {
- textContent += $(this).text();
- }
- });
- }
- });
- });
- tableCell.text(textContent);
- tableRow.append(tableCell);
- }
- tableHeader.append(tableRow);
- table.append(tableHeader);
- var tableBody = $("<tbody>");
- for (let i = 1; i < no_rows; i += 1) {
- let tableRow = $("<tr>");
- for (let j = 0; j < max_cells_per_row[i]; j += 1) {
- let tableCell = $("<td>");
- let textContent = "";
- $("table.tag").each(function (index, value) {
- $(this).children().children("tr").each(function (index, value) {
- if (index === i) {
- $(this).children("td").each(function (index, value) {
- if (index === j) {
- textContent += $(this).text();
- }
- });
- }
- });
- });
- tableCell.text(textContent);
- tableRow.append(tableCell);
- }
- tableBody.append(tableRow);
- }
- table.append(tableBody);
- console.log(table.html());
- console.log(no_rows);
- console.table(max_cells_per_row);
- $("button").after(table);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement