Я хочу иметь возможность перебирать таблицу html, получать количество строк и выводить номер строки каждой строки в крайнем левом поле.Однако у меня есть 2 проблемы: не удается получить количество строк и не получить желаемый вывод.
$(document).ready(function($) {
function create_html_table(tbl_data) {
var tbl = '';
tbl += '<table id ="datarepo">';
tbl += '<thead>';
//Some headers
tbl += '</thead>';
tbl += '<tbody>';
$.each(tbl_data, function(index, val) {
/* This is what I want to use to get the number of rows in the table.
However, uncommenting the following line will cause the whole table to disappear altogether.*/
// var numberrows = document.getElementById("datarepo").rows.length;
// Using static value here instead of numberrows because it is not working.
for (i = 1; i <= 2; i++) {
tbl += '<tr>';
tbl += '<td >' + i + '</td>';
tbl += '<td ><div col_name="filename">' + val['filename'] + '</div></td>';
tbl += '</tr>';
}
});
tbl += '</tbody>';
tbl += '</table>';
}
}
Желаемый вывод:
![](https://i.stack.imgur.com/ph8Mx.png)
Что я получил:
![enter image description here](https://i.stack.imgur.com/zl8AE.png)