Home >Web Front-end >Front-end Q&A >jquery converts table rows to columns
In front-end development, the processing and display of table data are often involved. Sometimes we need to convert table rows into columns for better display or data processing needs. jQuery is a lightweight JavaScript library that provides convenient DOM manipulation and event handling methods, making this table data conversion operation simple and easy. This article will introduce how to use jQuery to convert table rows to columns.
In actual development, sometimes we need to display and process table data in more detail. For example, in a table, one row represents a product, and each column represents the attributes of the product. If we want to classify or compare all products based on their attributes, we need to convert the table rows into columns. The converted table can more clearly display product attributes or differences between attributes, making it easier for users or developers to process and analyze data.
To convert table rows to columns, we need to read the data in the table through JavaScript/jQuery and assemble it into a new table. The specific implementation ideas are as follows:
Now, let us implement the operation of converting table rows to columns step by step.
<table id="original"> <tr> <th></th> <th>产品A</th> <th>产品B</th> <th>产品C</th> </tr> <tr> <td>颜色</td> <td>红色</td> <td>蓝色</td> <td>绿色</td> </tr> <tr> <td>尺寸</td> <td>大号</td> <td>中号</td> <td>小号</td> </tr> <tr> <td>售价</td> <td>100</td> <td>200</td> <td>150</td> </tr> </table>
var rows = $('#original tr'); var data = []; for (var i = 1; i < rows.length; i++) { var row = rows[i]; var rowData = []; for (var j = 1; j < row.cells.length; j++) { rowData.push(row.cells[j].innerText); } data.push(rowData); }
var rowHeaders = []; var colHeaders = []; var values = []; for (var i = 0; i < data.length; i++) { rowHeaders.push(rows[i + 1].cells[0].innerText); } for (var i = 1; i < rows.length; i++) { var row = rows[i]; for (var j = 1; j < row.cells.length; j++) { if (i === 1) { colHeaders.push(row.cells[j].innerText); } if (!values[j - 1]) { values[j - 1] = []; } values[j - 1].push(row.cells[j].innerText); } }
At this point, we have successfully stored the data in different arrays by row, column, and value. Next, we need to create a new table structure from these arrays.
var newTable = $('<table>'); var headerRow = $('<tr>'); headerRow.append($('<th>').text('')); for (var i = 0; i < colHeaders.length; i++) { headerRow.append($('<th>').text(colHeaders[i])); } newTable.append(headerRow); for (var i = 0; i < rowHeaders.length; i++) { var row = $('<tr>'); row.append($('<td>').text(rowHeaders[i])); for (var j = 0; j < values.length; j++) { row.append($('<td>').text(values[j][i])); } newTable.append(row); } $('#original').after(newTable);
$('#original').remove();
At this point, we have completed the operation of converting table rows to columns. Now, the original table has been removed and the converted table has been inserted into the HTML document.
This article introduces how to use jQuery to convert table rows to columns. The specific implementation is to assemble a new table structure by reading table data and processing the data. Through this operation, the data in the table can be displayed and processed more clearly and intuitively. For developers and data analysts, this has important implications for quickly processing data and analyzing its meaning.
The above is the detailed content of jquery converts table rows to columns. For more information, please follow other related articles on the PHP Chinese website!