Konwertuj tabelę html na tablicę w javascript
Jak można przekonwertować tabelę HTML na tablicę JavaScript?
<table id="cartGrid">
<thead>
<tr>
<th>Item Description</th>
<th>Qty</th>
<th>Unit Price</th>
<th>Ext Price</th>
</tr>
</thead>
<tbody>
<tr><td>Old Lamp</td><td>1</td><td>107.00</td><td>107.00</td>
<tr><td>Blue POst</td><td>2</td><td>7.00</td><td>14.00</td>
</tbody>
</table>
20
Author: Dave Jarvis, 2012-03-06
3 answers
Oto jeden przykład robienia tego, co chcesz.
var myTableArray = [];
$("table#cartGrid tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
});
alert(myTableArray);
Prawdopodobnie mógłbyś to rozwinąć, używając tekstu TH, aby zamiast tego utworzyć parę klucz-wartość dla każdego TD.
Ponieważ ta implementacja używa wielowymiarowej tablicy, możesz uzyskać dostęp do wiersza i td, wykonując coś takiego:
myTableArray[1][3] // Fourth td of the second tablerow
Edit: oto przykład: http://jsfiddle.net/PKB9j/1/
48
Author: Andreas Eriksson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-06 07:58:42
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-06 07:58:42
Ta funkcja zakrywa tabelę Html (wystarczy podać id) i zwraca tablicę tabeli:
function table_to_array(table_id) {
myData = document.getElementById(table_id).rows
//console.log(myData)
my_liste = []
for (var i = 0; i < myData.length; i++) {
el = myData[i].children
my_el = []
for (var j = 0; j < el.length; j++) {
my_el.push(el[j].innerText);
}
my_liste.push(my_el)
}
return my_liste
}
Mam nadzieję, że ci to pomoże ! 1
Author: Youcef Ali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-16 14:37:11
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-16 14:37:11
Oto rozwiązanie maszynopisu:
function getTableContent(table: HTMLTableElement): string[][] {
return Array.from(table.rows).reduce((acc, row) => {
const columns = Array.from(row.children)
.map(column => column.textContent || '')
return acc.concat(columns);
}, [] as string[][])
}
0
Author: cuddlemeister,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-08 14:01:59
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-08 14:01:59